mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
FIX[BE]: period without autoincrement
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type ProjectflockService interface {
|
||||
@@ -30,12 +31,12 @@ type projectflockService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectflockRepository
|
||||
FlockRepo flockRepository.FlockRepository
|
||||
FlockRepo flockRepository.FlockRepository
|
||||
KandangRepo kandangRepository.KandangRepository
|
||||
}
|
||||
|
||||
type FlockPeriodSummary struct {
|
||||
Flock entity.Flock
|
||||
Flock entity.Flock
|
||||
NextPeriod int
|
||||
}
|
||||
|
||||
@@ -49,7 +50,7 @@ func NewProjectflockService(
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
FlockRepo: flockRepo,
|
||||
FlockRepo: flockRepo,
|
||||
KandangRepo: kandangRepo,
|
||||
}
|
||||
}
|
||||
@@ -127,19 +128,33 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to start transaction")
|
||||
}
|
||||
|
||||
var nextPeriod int
|
||||
periodQuery := tx.Model(&entity.ProjectFlock{}).
|
||||
Where("flock_id = ?", req.FlockId).
|
||||
Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
if err := periodQuery.Select("COALESCE(MAX(period), 0)").Scan(&nextPeriod).Error; err != nil {
|
||||
tx.Rollback()
|
||||
s.Log.Errorf("Failed to determine next period for flock %d: %+v", req.FlockId, err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to determine next period")
|
||||
}
|
||||
nextPeriod++
|
||||
|
||||
projectRepo := s.Repository.WithTx(tx)
|
||||
createBody := &entity.ProjectFlock{
|
||||
FlockId: req.FlockId,
|
||||
FlockId: req.FlockId,
|
||||
AreaId: req.AreaId,
|
||||
ProductCategoryId: req.ProductCategoryId,
|
||||
FcrId: req.FcrId,
|
||||
LocationId: req.LocationId,
|
||||
Period: req.Period,
|
||||
Period: nextPeriod,
|
||||
CreatedBy: 1,
|
||||
}
|
||||
|
||||
if err := projectRepo.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
tx.Rollback()
|
||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
return nil, fiber.NewError(fiber.StatusConflict, "Project flock period already exists")
|
||||
}
|
||||
s.Log.Errorf("Failed to create projectflock: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -353,7 +368,7 @@ func (s projectflockService) GetFlockPeriodSummary(c *fiber.Ctx, flockID uint) (
|
||||
}
|
||||
|
||||
return &FlockPeriodSummary{
|
||||
Flock: *flock,
|
||||
Flock: *flock,
|
||||
NextPeriod: maxPeriod + 1,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user