mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
feat(BE-281): fixing recording error, fixing limit upload uniformity and purchase, add filter and statistic uniformity
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"mime/multipart"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -21,10 +22,13 @@ type Update struct {
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
ProjectFlockKandangId uint `query:"project_flock_kandang_id" validate:"omitempty,number,min=1"`
|
||||
Week int `query:"week" validate:"omitempty,min=1"`
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
ProjectFlockKandangId uint `query:"project_flock_kandang_id" validate:"omitempty,number,min=1"`
|
||||
Week int `query:"week" validate:"omitempty,min=1"`
|
||||
StartDate string `query:"start_date" validate:"omitempty"`
|
||||
EndDate string `query:"end_date" validate:"omitempty"`
|
||||
WithChart bool `query:"with_chart"`
|
||||
}
|
||||
|
||||
type UploadExcelRequest struct {
|
||||
@@ -37,6 +41,8 @@ type Approve struct {
|
||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||
}
|
||||
|
||||
const maxUniformityUploadBytes = 5 * 1024 * 1024
|
||||
|
||||
func ParseIDParam(c *fiber.Ctx, name string) (uint, error) {
|
||||
raw := strings.TrimSpace(c.Params(name))
|
||||
if raw == "" {
|
||||
@@ -55,15 +61,49 @@ func ParseQuery(c *fiber.Ctx) (*Query, error) {
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
Week: c.QueryInt("week", 0),
|
||||
StartDate: strings.TrimSpace(c.Query("start_date")),
|
||||
EndDate: strings.TrimSpace(c.Query("end_date")),
|
||||
WithChart: c.QueryBool("with_chart", false),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
if _, _, err := ParseDateRange(query.StartDate, query.EndDate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func ParseDateRange(startDate, endDate string) (*time.Time, *time.Time, error) {
|
||||
var startDateValue *time.Time
|
||||
var endDateValue *time.Time
|
||||
|
||||
if startDate != "" {
|
||||
parsed, err := time.Parse("2006-01-02", startDate)
|
||||
if err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "start_date must be in YYYY-MM-DD format")
|
||||
}
|
||||
startDateValue = &parsed
|
||||
}
|
||||
if endDate != "" {
|
||||
parsed, err := time.Parse("2006-01-02", endDate)
|
||||
if err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "end_date must be in YYYY-MM-DD format")
|
||||
}
|
||||
endDateValue = &parsed
|
||||
}
|
||||
if startDateValue != nil && endDateValue != nil {
|
||||
if endDateValue.Before(*startDateValue) {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "end_date must be greater than or equal to start_date")
|
||||
}
|
||||
}
|
||||
|
||||
return startDateValue, endDateValue, nil
|
||||
}
|
||||
|
||||
func ParseCreate(c *fiber.Ctx) (*Create, *multipart.FileHeader, error) {
|
||||
date := strings.TrimSpace(c.FormValue("date"))
|
||||
if date == "" {
|
||||
@@ -94,6 +134,9 @@ func ParseCreate(c *fiber.Ctx) (*Create, *multipart.FileHeader, error) {
|
||||
if err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "document is required")
|
||||
}
|
||||
if err := validateUniformityFileSize(file); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &Create{
|
||||
Date: date,
|
||||
@@ -134,6 +177,8 @@ func ParseUpdate(c *fiber.Ctx) (*Update, *multipart.FileHeader, error) {
|
||||
file, err := c.FormFile("document")
|
||||
if err != nil {
|
||||
file = nil
|
||||
} else if err := validateUniformityFileSize(file); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return req, file, nil
|
||||
@@ -151,6 +196,9 @@ func ParseUploadFiles(c *fiber.Ctx) ([]*multipart.FileHeader, error) {
|
||||
if err != nil || file == nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "document is required")
|
||||
}
|
||||
if err := validateUniformityFileSize(file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*multipart.FileHeader{file}, nil
|
||||
}
|
||||
@@ -162,3 +210,10 @@ func ParseApprove(c *fiber.Ctx) (*Approve, error) {
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func validateUniformityFileSize(file *multipart.FileHeader) error {
|
||||
if file != nil && file.Size > maxUniformityUploadBytes {
|
||||
return fiber.NewError(fiber.StatusRequestEntityTooLarge, "Document size must be <= 5MB")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user