mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
add daily checklist module;adjust master data;adjust migration
This commit is contained in:
@@ -2,8 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
@@ -41,10 +39,8 @@ func NewEmployeesService(repo repository.EmployeesRepository, validate *validato
|
||||
|
||||
func (s employeesService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("EmployeeKandangs.Kandang")
|
||||
// Preload("EmployeeKandangs.Kandang.Location").
|
||||
// Preload("EmployeeKandangs.Kandang.Pic").
|
||||
// Preload("EmployeeKandangs.Kandang.CreatedUser")
|
||||
Preload("EmployeeKandangs.Kandang").
|
||||
Where("employees.deleted_at IS NULL")
|
||||
}
|
||||
|
||||
func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Employees, int64, error) {
|
||||
@@ -98,9 +94,9 @@ func (s *employeesService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "name cannot be empty")
|
||||
}
|
||||
|
||||
kandangIDs, err := parseKandangIDs(req.KandangIDs)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
kandangIDs := normalizeKandangIDs(req.KandangIDs)
|
||||
if len(kandangIDs) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
|
||||
}
|
||||
|
||||
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||
@@ -181,9 +177,9 @@ func (s employeesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
}
|
||||
|
||||
if req.KandangIDs != nil {
|
||||
ids, err := parseKandangIDs(*req.KandangIDs)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
ids := normalizeKandangIDs(*req.KandangIDs)
|
||||
if len(ids) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
|
||||
}
|
||||
|
||||
kandangIDs = ids
|
||||
@@ -248,33 +244,22 @@ func (s employeesService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseKandangIDs(raw string) ([]uint, error) {
|
||||
parts := strings.Split(raw, ",")
|
||||
ids := make([]uint, 0, len(parts))
|
||||
func normalizeKandangIDs(ids []uint) []uint {
|
||||
result := make([]uint, 0, len(ids))
|
||||
seen := make(map[uint]struct{})
|
||||
|
||||
for _, part := range parts {
|
||||
value := strings.TrimSpace(part)
|
||||
if value == "" {
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
parsed, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid kandang id: %s", value)
|
||||
}
|
||||
|
||||
id := uint(parsed)
|
||||
if _, ok := seen[id]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
seen[id] = struct{}{}
|
||||
ids = append(ids, id)
|
||||
result = append(result, id)
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return nil, errors.New("kandang_ids must contain at least one valid id")
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ package validation
|
||||
|
||||
type Create struct {
|
||||
Name string `json:"name" validate:"required_strict,min=3"`
|
||||
KandangIDs string `json:"kandang_ids" validate:"required"`
|
||||
KandangIDs []uint `json:"kandang_ids" validate:"required,min=1,dive,required"`
|
||||
IsActive bool `json:"is_active"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Name *string `json:"name,omitempty" validate:"omitempty"`
|
||||
KandangIDs *string `json:"kandang_ids,omitempty"`
|
||||
KandangIDs *[]uint `json:"kandang_ids,omitempty" validate:"omitempty,min=1,dive,required"`
|
||||
IsActive *bool `json:"is_active,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user