mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
adjust patch employee
This commit is contained in:
@@ -153,16 +153,80 @@ func (s employeesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateBody := make(map[string]any)
|
updateBody := make(map[string]any)
|
||||||
|
var (
|
||||||
|
kandangIDs []uint
|
||||||
|
needKandangUpdate bool
|
||||||
|
)
|
||||||
|
|
||||||
if req.Name != nil {
|
if req.Name != nil {
|
||||||
updateBody["name"] = *req.Name
|
trimmed := strings.TrimSpace(*req.Name)
|
||||||
|
if trimmed == "" {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "name cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Where("LOWER(name) = ? AND id <> ?", strings.ToLower(trimmed), id)
|
||||||
|
}); err == nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "employee already exists")
|
||||||
|
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
s.Log.Errorf("Failed checking employee uniqueness: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updateBody["name"] = trimmed
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(updateBody) == 0 {
|
if req.IsActive != nil {
|
||||||
|
updateBody["is_active"] = *req.IsActive
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.KandangIDs != nil {
|
||||||
|
ids, err := parseKandangIDs(*req.KandangIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
kandangIDs = ids
|
||||||
|
needKandangUpdate = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(updateBody) == 0 && !needKandangUpdate {
|
||||||
return s.GetOne(c, id)
|
return s.GetOne(c, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
if err := s.Repository.DB().Transaction(func(tx *gorm.DB) error {
|
||||||
|
repoTx := s.Repository.WithTx(tx)
|
||||||
|
|
||||||
|
if len(updateBody) > 0 {
|
||||||
|
if err := repoTx.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if needKandangUpdate {
|
||||||
|
if err := tx.WithContext(c.Context()).
|
||||||
|
Where("employee_id = ?", id).
|
||||||
|
Delete(&entity.EmployeeKandang{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
relations := make([]entity.EmployeeKandang, 0, len(kandangIDs))
|
||||||
|
for _, kandangID := range kandangIDs {
|
||||||
|
relations = append(relations, entity.EmployeeKandang{
|
||||||
|
EmployeeId: id,
|
||||||
|
KandangId: kandangID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(relations) > 0 {
|
||||||
|
if err := tx.WithContext(c.Context()).Create(&relations).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Employees not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Employees not found")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user