mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
feat[BE#US386]: add production standards module with CRUD operations
- Created database migration for production standards and related tables. - Implemented entities for ProductionStandard, ProductionStandardDetail, and StandardGrowthDetail. - Developed controller for handling production standard requests. - Added DTOs for data transfer between layers. - Implemented service layer for business logic related to production standards. - Created repository interfaces and implementations for data access. - Added validation for production standard requests. - Registered routes for production standards in the main application.
This commit is contained in:
@@ -0,0 +1,302 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProductionStandardService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProductionStandard, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProductionStandard, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ProductionStandard, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProductionStandard, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type productionStandardService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProductionStandardRepository
|
||||
ProductionStandardDetailRepo repository.ProductionStandardDetailRepository
|
||||
StandardGrowthDetailRepo repository.StandardGrowthDetailRepository
|
||||
}
|
||||
|
||||
func NewProductionStandardService(
|
||||
repo repository.ProductionStandardRepository,
|
||||
productionStandardDetailRepo repository.ProductionStandardDetailRepository,
|
||||
standardGrowthDetailRepo repository.StandardGrowthDetailRepository,
|
||||
validate *validator.Validate,
|
||||
) ProductionStandardService {
|
||||
return &productionStandardService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ProductionStandardDetailRepo: productionStandardDetailRepo,
|
||||
StandardGrowthDetailRepo: standardGrowthDetailRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s productionStandardService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("ProductionStandardDetails").
|
||||
Preload("StandardGrowthDetails")
|
||||
}
|
||||
|
||||
func (s productionStandardService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductionStandard, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
productionStandards, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
if params.Search != "" {
|
||||
return db.Where("name LIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
if params.ProjectCategory != "" {
|
||||
return db.Where("project_category = ?", params.ProjectCategory)
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get productionStandards: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
return productionStandards, total, nil
|
||||
}
|
||||
|
||||
func (s productionStandardService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductionStandard, error) {
|
||||
productionStandard, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "ProductionStandard not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get productionStandard by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return productionStandard, nil
|
||||
}
|
||||
|
||||
func (s *productionStandardService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.ProductionStandard, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
actorID, err := m.ActorIDFromContext(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nameExists, err := s.Repository.NameExists(c.Context(), req.Name, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if nameExists {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Production standard with name '%s' already exists", req.Name))
|
||||
}
|
||||
|
||||
var createdStandard *entity.ProductionStandard
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
standardRepoTx := repository.NewProductionStandardRepository(tx)
|
||||
productionStandardDetailRepoTx := repository.NewProductionStandardDetailRepository(tx)
|
||||
standardGrowthDetailRepoTx := repository.NewStandardGrowthDetailRepository(tx)
|
||||
|
||||
newStandard := &entity.ProductionStandard{
|
||||
Name: req.Name,
|
||||
ProjectCategory: req.ProjectCategory,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
if err := standardRepoTx.CreateOne(c.Context(), newStandard, nil); err != nil {
|
||||
return fmt.Errorf("failed to create production standard: %w", err)
|
||||
}
|
||||
|
||||
for _, detailReq := range req.Details {
|
||||
if detailReq.ProductionStandardUniformityDetails == nil {
|
||||
return fmt.Errorf("production_standard_uniformity_details is required in week %d", detailReq.Week)
|
||||
}
|
||||
|
||||
if req.ProjectCategory == string(utils.ProjectFlockCategoryLaying) {
|
||||
if detailReq.ProductionStandardDetails == nil {
|
||||
return fmt.Errorf("production_standard_details is required for LAYING category in week %d", detailReq.Week)
|
||||
}
|
||||
|
||||
productionStandardDetail := &entity.ProductionStandardDetail{
|
||||
ProductionStandardId: newStandard.Id,
|
||||
Week: detailReq.Week,
|
||||
TargetHenDayProduction: detailReq.ProductionStandardDetails.TargetHenDayProduction,
|
||||
TargetHenHouseProduction: detailReq.ProductionStandardDetails.TargetHenHouseProduction,
|
||||
TargetEggWeight: detailReq.ProductionStandardDetails.TargetEggWeight,
|
||||
TargetEggMass: detailReq.ProductionStandardDetails.TargetEggMass,
|
||||
}
|
||||
|
||||
if err := productionStandardDetailRepoTx.CreateOne(c.Context(), productionStandardDetail, nil); err != nil {
|
||||
return fmt.Errorf("failed to create production standard detail for week %d: %w", detailReq.Week, err)
|
||||
}
|
||||
}
|
||||
|
||||
standardGrowthDetail := &entity.StandardGrowthDetail{
|
||||
ProductionStandardId: newStandard.Id,
|
||||
Week: detailReq.Week,
|
||||
TargetMeanBw: detailReq.ProductionStandardUniformityDetails.TargetMeanBw,
|
||||
MaxDepletion: detailReq.ProductionStandardUniformityDetails.MaxDepletion,
|
||||
MinUniformity: detailReq.ProductionStandardUniformityDetails.MinUniformity,
|
||||
FeedIntake: detailReq.ProductionStandardUniformityDetails.FeedIntake,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
if err := standardGrowthDetailRepoTx.CreateOne(c.Context(), standardGrowthDetail, nil); err != nil {
|
||||
return fmt.Errorf("failed to create standard growth detail for week %d: %w", detailReq.Week, err)
|
||||
}
|
||||
}
|
||||
|
||||
createdStandard = newStandard
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create production standard: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, createdStandard.Id)
|
||||
}
|
||||
|
||||
func (s productionStandardService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProductionStandard, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
actorID, err := m.ActorIDFromContext(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var updatedStandard *entity.ProductionStandard
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
standardRepoTx := repository.NewProductionStandardRepository(tx)
|
||||
productionStandardDetailRepoTx := repository.NewProductionStandardDetailRepository(tx)
|
||||
standardGrowthDetailRepoTx := repository.NewStandardGrowthDetailRepository(tx)
|
||||
|
||||
existingStandard, err := standardRepoTx.GetByID(c.Context(), id, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "ProductionStandard not found")
|
||||
}
|
||||
return fmt.Errorf("failed to get production standard: %w", err)
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
if req.Name != nil {
|
||||
|
||||
nameExists, err := s.Repository.NameExists(c.Context(), *req.Name, &id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to check existing production standard: %+v", err)
|
||||
return err
|
||||
}
|
||||
if nameExists {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Production standard with name '%s' already exists", *req.Name))
|
||||
}
|
||||
updateBody["name"] = *req.Name
|
||||
}
|
||||
if req.ProjectCategory != nil {
|
||||
updateBody["project_category"] = *req.ProjectCategory
|
||||
}
|
||||
|
||||
if len(updateBody) > 0 {
|
||||
if err := standardRepoTx.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
return fmt.Errorf("failed to update production standard: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if req.Details != nil && len(req.Details) > 0 {
|
||||
|
||||
projectCategory := existingStandard.ProjectCategory
|
||||
if req.ProjectCategory != nil {
|
||||
projectCategory = *req.ProjectCategory
|
||||
}
|
||||
|
||||
if err := productionStandardDetailRepoTx.DeleteByProductionStandardID(c.Context(), id); err != nil {
|
||||
return fmt.Errorf("failed to delete old production standard details: %w", err)
|
||||
}
|
||||
if err := standardGrowthDetailRepoTx.DeleteByProductionStandardID(c.Context(), id); err != nil {
|
||||
return fmt.Errorf("failed to delete old standard growth details: %w", err)
|
||||
}
|
||||
|
||||
for _, detailReq := range req.Details {
|
||||
if detailReq.ProductionStandardUniformityDetails == nil {
|
||||
return fmt.Errorf("production_standard_uniformity_details is required in week %d", detailReq.Week)
|
||||
}
|
||||
|
||||
if projectCategory == "LAYING" {
|
||||
if detailReq.ProductionStandardDetails == nil {
|
||||
return fmt.Errorf("production_standard_details is required for LAYING category in week %d", detailReq.Week)
|
||||
}
|
||||
|
||||
productionStandardDetail := &entity.ProductionStandardDetail{
|
||||
ProductionStandardId: id,
|
||||
Week: detailReq.Week,
|
||||
TargetHenDayProduction: detailReq.ProductionStandardDetails.TargetHenDayProduction,
|
||||
TargetHenHouseProduction: detailReq.ProductionStandardDetails.TargetHenHouseProduction,
|
||||
TargetEggWeight: detailReq.ProductionStandardDetails.TargetEggWeight,
|
||||
TargetEggMass: detailReq.ProductionStandardDetails.TargetEggMass,
|
||||
}
|
||||
|
||||
if err := productionStandardDetailRepoTx.CreateOne(c.Context(), productionStandardDetail, nil); err != nil {
|
||||
return fmt.Errorf("failed to create production standard detail for week %d: %w", detailReq.Week, err)
|
||||
}
|
||||
}
|
||||
|
||||
standardGrowthDetail := &entity.StandardGrowthDetail{
|
||||
ProductionStandardId: id,
|
||||
Week: detailReq.Week,
|
||||
TargetMeanBw: detailReq.ProductionStandardUniformityDetails.TargetMeanBw,
|
||||
MaxDepletion: detailReq.ProductionStandardUniformityDetails.MaxDepletion,
|
||||
MinUniformity: detailReq.ProductionStandardUniformityDetails.MinUniformity,
|
||||
FeedIntake: detailReq.ProductionStandardUniformityDetails.FeedIntake,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
if err := standardGrowthDetailRepoTx.CreateOne(c.Context(), standardGrowthDetail, nil); err != nil {
|
||||
return fmt.Errorf("failed to create standard growth detail for week %d: %w", detailReq.Week, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updatedStandard = existingStandard
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update production standard: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, updatedStandard.Id)
|
||||
}
|
||||
|
||||
func (s productionStandardService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "ProductionStandard not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete productionStandard: %+v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user