feat/BE/US-76/TASK-122,133,121,120 Recording add create delete edit

This commit is contained in:
ragilap
2025-10-26 15:27:19 +07:00
parent cedd5365d8
commit 8ae614540f
13 changed files with 639 additions and 579 deletions
@@ -2,6 +2,7 @@ package repository
import (
"context"
"errors"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
@@ -18,6 +19,8 @@ type KandangRepository interface {
GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.Kandang, error)
HasActiveKandangForProjectFlock(ctx context.Context, projectFlockID uint, excludeID *uint) (bool, error)
UpdateStatusByProjectFlockID(ctx context.Context, projectFlockID uint, status utils.KandangStatus) error
UpsertProjectFlockKandang(ctx context.Context, projectFlockID, kandangID uint) error
}
type KandangRepositoryImpl struct {
@@ -58,14 +61,15 @@ func (r *KandangRepositoryImpl) ProjectFlockExists(ctx context.Context, projectF
func (r *KandangRepositoryImpl) HasActiveKandangForProjectFlock(ctx context.Context, projectFlockID uint, excludeID *uint) (bool, error) {
var count int64
q := r.db.WithContext(ctx).
Model(&entity.Kandang{}).
Where("project_flock_id = ?", projectFlockID).
Where("status = ?", utils.KandangStatusActive).
Where("deleted_at IS NULL")
if excludeID != nil {
q = q.Where("id <> ?", *excludeID)
}
q := r.db.WithContext(ctx).
Table("kandangs k").
Joins("JOIN project_flock_kandangs pfk ON pfk.kandang_id = k.id").
Where("pfk.project_flock_id = ?", projectFlockID).
Where("k.status = ?", utils.KandangStatusActive).
Where("k.deleted_at IS NULL")
if excludeID != nil {
q = q.Where("k.id <> ?", *excludeID)
}
if err := q.Count(&count).Error; err != nil {
return false, err
}
@@ -74,18 +78,49 @@ func (r *KandangRepositoryImpl) HasActiveKandangForProjectFlock(ctx context.Cont
func (r *KandangRepositoryImpl) GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.Kandang, error) {
kandang := new(entity.Kandang)
err := r.db.WithContext(ctx).
Where("project_flock_id = ?", projectFlockID).
First(kandang).Error
if err != nil {
return nil, err
}
err := r.db.WithContext(ctx).
Table("kandangs k").
Joins("JOIN project_flock_kandangs pfk ON pfk.kandang_id = k.id").
Where("pfk.project_flock_id = ?", projectFlockID).
Where("k.deleted_at IS NULL").
Order("k.id ASC").
Limit(1).
Find(kandang).Error
if err != nil {
return nil, err
}
if kandang.Id == 0 {
return nil, gorm.ErrRecordNotFound
}
return kandang, nil
}
func (r *KandangRepositoryImpl) UpdateStatusByProjectFlockID(ctx context.Context, projectFlockID uint, status utils.KandangStatus) error {
return r.db.WithContext(ctx).
Model(&entity.Kandang{}).
Where("project_flock_id = ?", projectFlockID).
Update("status", string(status)).Error
sub := r.db.WithContext(ctx).
Table("project_flock_kandangs").
Select("kandang_id").
Where("project_flock_id = ?", projectFlockID)
return r.db.WithContext(ctx).
Model(&entity.Kandang{}).
Where("id IN (?)", sub).
Where("deleted_at IS NULL").
Update("status", string(status)).Error
}
func (r *KandangRepositoryImpl) UpsertProjectFlockKandang(ctx context.Context, projectFlockID, kandangID uint) error {
var link entity.ProjectFlockKandang
err := r.db.WithContext(ctx).
Where("project_flock_id = ? AND kandang_id = ?", projectFlockID, kandangID).
First(&link).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
link = entity.ProjectFlockKandang{
ProjectFlockId: projectFlockID,
KandangId: kandangID,
}
return r.db.WithContext(ctx).Create(&link).Error
}
return err
}
@@ -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)