mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat/BE/US-76/TASK-122,133,121,120 Recording add create delete edit
This commit is contained in:
@@ -40,7 +40,8 @@ func NewKandangService(repo repository.KandangRepository, validate *validator.Va
|
||||
}
|
||||
|
||||
func (s kandangService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Location").Preload("Pic")
|
||||
return db.Preload("CreatedUser").Preload("Location").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock")
|
||||
|
||||
}
|
||||
|
||||
func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Kandang, int64, error) {
|
||||
@@ -110,7 +111,6 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid kandang status")
|
||||
}
|
||||
|
||||
var projectFlockID *uint
|
||||
if req.ProjectFlockId != nil {
|
||||
if exists, err := s.Repository.ProjectFlockExists(c.Context(), *req.ProjectFlockId); err != nil {
|
||||
s.Log.Errorf("Failed to check project flock existence: %+v", err)
|
||||
@@ -128,8 +128,6 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
}
|
||||
}
|
||||
|
||||
idCopy := *req.ProjectFlockId
|
||||
projectFlockID = &idCopy
|
||||
}
|
||||
|
||||
//TODO: created by dummy
|
||||
@@ -138,7 +136,6 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
LocationId: req.LocationId,
|
||||
Status: status,
|
||||
PicId: req.PicId,
|
||||
ProjectFlockId: projectFlockID,
|
||||
CreatedBy: 1,
|
||||
}
|
||||
|
||||
@@ -147,6 +144,12 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.ProjectFlockId != nil {
|
||||
if err := s.Repository.UpsertProjectFlockKandang(c.Context(), *req.ProjectFlockId, createBody.Id); err != nil {
|
||||
s.Log.Errorf("Failed to link kandang to project_flock via pivot: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to link kandang to project flock")
|
||||
}
|
||||
}
|
||||
return s.GetOne(c, createBody.Id)
|
||||
}
|
||||
|
||||
@@ -201,7 +204,6 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
finalStatus = status
|
||||
}
|
||||
|
||||
projectFlockIDToUse := existing.ProjectFlockId
|
||||
if req.ProjectFlockId != nil {
|
||||
if exists, err := s.Repository.ProjectFlockExists(c.Context(), *req.ProjectFlockId); err != nil {
|
||||
s.Log.Errorf("Failed to check project flock existence: %+v", err)
|
||||
@@ -209,30 +211,33 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
} else if !exists {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Project flock with id %d not found", *req.ProjectFlockId))
|
||||
}
|
||||
idCopy := *req.ProjectFlockId
|
||||
projectFlockIDToUse = &idCopy
|
||||
updateBody["project_flock_id"] = idCopy
|
||||
}
|
||||
|
||||
if projectFlockIDToUse != nil && finalStatus == string(utils.KandangStatusActive) {
|
||||
if active, err := s.Repository.HasActiveKandangForProjectFlock(c.Context(), *projectFlockIDToUse, &id); err != nil {
|
||||
s.Log.Errorf("Failed to check kandang activity for project flock %d: %+v", *projectFlockIDToUse, err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check active kandang for project flock")
|
||||
} else if active {
|
||||
return nil, fiber.NewError(fiber.StatusConflict, "Project flock already has an active kandang")
|
||||
// Kalau status jadi ACTIVE, pastikan tidak ada kandang aktif lain pada project flock tsb (hitung via pivot)
|
||||
if finalStatus == string(utils.KandangStatusActive) {
|
||||
if active, err := s.Repository.HasActiveKandangForProjectFlock(c.Context(), *req.ProjectFlockId, &id); err != nil {
|
||||
s.Log.Errorf("Failed to check kandang activity for project flock %d: %+v", *req.ProjectFlockId, err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check active kandang for project flock")
|
||||
} else if active {
|
||||
return nil, fiber.NewError(fiber.StatusConflict, "Project flock already has an active kandang")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
return s.GetOne(c, id)
|
||||
if len(updateBody) > 0 {
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Kandang not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to update kandang: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Kandang not found")
|
||||
if req.ProjectFlockId != nil {
|
||||
if err := s.Repository.UpsertProjectFlockKandang(c.Context(), *req.ProjectFlockId, id); err != nil {
|
||||
s.Log.Errorf("Failed to upsert pivot kandang-project_flock: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to link kandang to project flock")
|
||||
}
|
||||
s.Log.Errorf("Failed to update kandang: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
|
||||
Reference in New Issue
Block a user