mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-22 14:25:45 +00:00
feat(BE-281): unfinished uniformity and create project flock triger productwarehouse and add new filtering lookup
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"mime/multipart"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type Create struct {
|
||||
Date string `form:"date" validate:"required"`
|
||||
ProjectFlockKandangId uint `form:"project_flock_kandang_id" validate:"required,number,min=1"`
|
||||
Week int `form:"week" validate:"required,min=1"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Date *string `json:"date,omitempty" form:"date" validate:"omitempty"`
|
||||
ProjectFlockKandangId *uint `json:"project_flock_kandang_id,omitempty" form:"project_flock_kandang_id" validate:"omitempty,number,min=1"`
|
||||
Week *int `json:"week,omitempty" form:"week" validate:"omitempty,min=1"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
type UploadExcelRequest struct {
|
||||
Documents []*multipart.FileHeader `form:"documents" json:"documents" validate:"omitempty,dive"`
|
||||
}
|
||||
|
||||
type Approve struct {
|
||||
Action string `json:"action" validate:"required_strict"`
|
||||
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||
}
|
||||
|
||||
func ParseIDParam(c *fiber.Ctx, name string) (uint, error) {
|
||||
raw := strings.TrimSpace(c.Params(name))
|
||||
if raw == "" {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
id, err := strconv.Atoi(raw)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
return uint(id), nil
|
||||
}
|
||||
|
||||
func ParseQuery(c *fiber.Ctx) (*Query, error) {
|
||||
query := &Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
Week: c.QueryInt("week", 0),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func ParseCreate(c *fiber.Ctx) (*Create, *multipart.FileHeader, error) {
|
||||
date := strings.TrimSpace(c.FormValue("date"))
|
||||
if date == "" {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "date is required")
|
||||
}
|
||||
|
||||
projectFlockKandangIDStr := strings.TrimSpace(c.FormValue("project_flock_kandang_id"))
|
||||
if projectFlockKandangIDStr == "" {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
||||
}
|
||||
|
||||
projectFlockKandangID, err := strconv.Atoi(projectFlockKandangIDStr)
|
||||
if err != nil || projectFlockKandangID <= 0 {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
||||
}
|
||||
|
||||
weekStr := strings.TrimSpace(c.FormValue("week"))
|
||||
if weekStr == "" {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "week is required")
|
||||
}
|
||||
|
||||
week, err := strconv.Atoi(weekStr)
|
||||
if err != nil || week <= 0 {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "week is required")
|
||||
}
|
||||
|
||||
file, err := c.FormFile("document")
|
||||
if err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "document is required")
|
||||
}
|
||||
|
||||
return &Create{
|
||||
Date: date,
|
||||
ProjectFlockKandangId: uint(projectFlockKandangID),
|
||||
Week: week,
|
||||
}, file, nil
|
||||
}
|
||||
|
||||
func ParseUpdate(c *fiber.Ctx) (*Update, *multipart.FileHeader, error) {
|
||||
contentType := strings.ToLower(c.Get("Content-Type"))
|
||||
if strings.Contains(contentType, "multipart/form-data") {
|
||||
req := &Update{}
|
||||
|
||||
date := strings.TrimSpace(c.FormValue("date"))
|
||||
if date != "" {
|
||||
req.Date = &date
|
||||
}
|
||||
|
||||
projectFlockKandangIDStr := strings.TrimSpace(c.FormValue("project_flock_kandang_id"))
|
||||
if projectFlockKandangIDStr != "" {
|
||||
projectFlockKandangID, err := strconv.Atoi(projectFlockKandangIDStr)
|
||||
if err != nil || projectFlockKandangID <= 0 {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is invalid")
|
||||
}
|
||||
idCopy := uint(projectFlockKandangID)
|
||||
req.ProjectFlockKandangId = &idCopy
|
||||
}
|
||||
|
||||
weekStr := strings.TrimSpace(c.FormValue("week"))
|
||||
if weekStr != "" {
|
||||
week, err := strconv.Atoi(weekStr)
|
||||
if err != nil || week <= 0 {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "week is invalid")
|
||||
}
|
||||
req.Week = &week
|
||||
}
|
||||
|
||||
file, err := c.FormFile("document")
|
||||
if err != nil {
|
||||
file = nil
|
||||
}
|
||||
|
||||
return req, file, nil
|
||||
}
|
||||
|
||||
req := new(Update)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
return req, nil, nil
|
||||
}
|
||||
|
||||
func ParseUploadFiles(c *fiber.Ctx) ([]*multipart.FileHeader, error) {
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||
}
|
||||
|
||||
files := form.File["documents"]
|
||||
if len(files) == 0 {
|
||||
if file, err := c.FormFile("document"); err == nil && file != nil {
|
||||
files = []*multipart.FileHeader{file}
|
||||
} else {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "documents is required")
|
||||
}
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func ParseApprove(c *fiber.Ctx) (*Approve, error) {
|
||||
req := new(Approve)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
Reference in New Issue
Block a user