mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
feat[BE-127]: inisiate transfer laying for base template API
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type LayingKandangTransfer struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
KandangId uint
|
||||
ProductWarehouseId uint
|
||||
Qty float64 `gorm:"type:numeric(15,3)"`
|
||||
LayingTransferId uint `gorm:"not null"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
|
||||
Kandang *Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
||||
ProductWarehouse *ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||
LayingTransfer *LayingTransfer `gorm:"foreignKey:LayingTransferId;references:Id"`
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type LayingTransfer struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
FromProjectFlockId uint `gorm:"not null"`
|
||||
ToProjectFlockId uint `gorm:"not null"`
|
||||
TotalQty float64 `gorm:"type:numeric(15,3)"`
|
||||
TransferDate time.Time `gorm:"type:date;not null"`
|
||||
Notes string `gorm:"type:text"`
|
||||
CreatedBy uint `gorm:"not null"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
|
||||
FromProjectFlock *ProjectFlock `gorm:"foreignKey:FromProjectFlockId;references:Id"`
|
||||
ToProjectFlock *ProjectFlock `gorm:"foreignKey:ToProjectFlockId;references:Id"`
|
||||
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TransferLaying struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null;uniqueIndex:idx_name,where:deleted_at IS NULL"`
|
||||
CreatedBy uint `gorm:"not null"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
}
|
||||
@@ -27,17 +27,17 @@ type TransferLayingDetailDTO struct {
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToTransferLayingBaseDTO(e entity.TransferLaying) TransferLayingBaseDTO {
|
||||
func ToTransferLayingBaseDTO(e entity.LayingTransfer) TransferLayingBaseDTO {
|
||||
return TransferLayingBaseDTO{
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func ToTransferLayingListDTO(e entity.TransferLaying) TransferLayingListDTO {
|
||||
func ToTransferLayingListDTO(e entity.LayingTransfer) TransferLayingListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
mapped := userDTO.ToUserBaseDTO(*e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func ToTransferLayingListDTO(e entity.TransferLaying) TransferLayingListDTO {
|
||||
}
|
||||
}
|
||||
|
||||
func ToTransferLayingListDTOs(e []entity.TransferLaying) []TransferLayingListDTO {
|
||||
func ToTransferLayingListDTOs(e []entity.LayingTransfer) []TransferLayingListDTO {
|
||||
result := make([]TransferLayingListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToTransferLayingListDTO(r)
|
||||
@@ -57,7 +57,7 @@ func ToTransferLayingListDTOs(e []entity.TransferLaying) []TransferLayingListDTO
|
||||
return result
|
||||
}
|
||||
|
||||
func ToTransferLayingDetailDTO(e entity.TransferLaying) TransferLayingDetailDTO {
|
||||
func ToTransferLayingDetailDTO(e entity.LayingTransfer) TransferLayingDetailDTO {
|
||||
return TransferLayingDetailDTO{
|
||||
TransferLayingListDTO: ToTransferLayingListDTO(e),
|
||||
}
|
||||
|
||||
+5
-4
@@ -1,21 +1,22 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TransferLayingRepository interface {
|
||||
repository.BaseRepository[entity.TransferLaying]
|
||||
repository.BaseRepository[entity.LayingTransfer]
|
||||
|
||||
}
|
||||
|
||||
type TransferLayingRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.TransferLaying]
|
||||
*repository.BaseRepositoryImpl[entity.LayingTransfer]
|
||||
}
|
||||
|
||||
func NewTransferLayingRepository(db *gorm.DB) TransferLayingRepository {
|
||||
return &TransferLayingRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.TransferLaying](db),
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.LayingTransfer](db),
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
|
||||
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
ProjectFlockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -15,24 +17,28 @@ import (
|
||||
)
|
||||
|
||||
type TransferLayingService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.TransferLaying, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.TransferLaying, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.TransferLaying, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.TransferLaying, error)
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.LayingTransfer, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.LayingTransfer, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.LayingTransfer, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.LayingTransfer, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type transferLayingService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.TransferLayingRepository
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.TransferLayingRepository
|
||||
ProjectFlockRepo ProjectFlockRepository.ProjectflockRepository
|
||||
ProjectFlockKandangRepo ProjectFlockRepository.ProjectFlockKandangRepository
|
||||
}
|
||||
|
||||
func NewTransferLayingService(repo repository.TransferLayingRepository, validate *validator.Validate) TransferLayingService {
|
||||
func NewTransferLayingService(repo repository.TransferLayingRepository, projectFlockRepo ProjectFlockRepository.ProjectflockRepository, projectFlockKandangRepo ProjectFlockRepository.ProjectFlockKandangRepository, validate *validator.Validate) TransferLayingService {
|
||||
return &transferLayingService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ProjectFlockRepo: projectFlockRepo,
|
||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +46,7 @@ func (s transferLayingService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser")
|
||||
}
|
||||
|
||||
func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.TransferLaying, int64, error) {
|
||||
func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.LayingTransfer, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -62,7 +68,7 @@ func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
||||
return transferLayings, total, nil
|
||||
}
|
||||
|
||||
func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.TransferLaying, error) {
|
||||
func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.LayingTransfer, error) {
|
||||
transferLaying, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "TransferLaying not found")
|
||||
@@ -74,24 +80,62 @@ func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.TransferLa
|
||||
return transferLaying, nil
|
||||
}
|
||||
|
||||
func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.TransferLaying, error) {
|
||||
func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.LayingTransfer, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createBody := &entity.TransferLaying{
|
||||
Name: req.Name,
|
||||
}
|
||||
|
||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create transferLaying: %+v", err)
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
common.RelationCheck{Name: "Source Project Flock", ID: &req.SourceProjectFlockId, Exists: s.ProjectFlockRepo.IdExists},
|
||||
common.RelationCheck{Name: "Target Project Flock", ID: &req.TargetProjectFlockId, Exists: s.ProjectFlockRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, createBody.Id)
|
||||
for _, detail := range req.Details {
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
common.RelationCheck{Name: "Project Flock Kandang", ID: &detail.SourceProjectFlockKandangId, Exists: s.ProjectFlockKandangRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
transferDate, err := utils.ParseDateString(req.TransferDate)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transfer date format")
|
||||
}
|
||||
|
||||
var totalQty float64
|
||||
for _, item := range req.Details {
|
||||
totalQty += item.Quantity
|
||||
}
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
createBody := &entity.LayingTransfer{
|
||||
Notes: req.Reason,
|
||||
FromProjectFlockId: req.SourceProjectFlockId,
|
||||
ToProjectFlockId: req.TargetProjectFlockId,
|
||||
TransferDate: transferDate,
|
||||
TotalQty: totalQty,
|
||||
CreatedBy: 1, //todo : harus diambil dari auth
|
||||
}
|
||||
|
||||
if err := s.Repository.WithTx(dbTransaction).CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (s transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.TransferLaying, error) {
|
||||
func (s transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.LayingTransfer, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+11
-2
@@ -1,11 +1,20 @@
|
||||
package validation
|
||||
|
||||
type CreateDetail struct {
|
||||
SourceProjectFlockKandangId uint `json:"source_project_flock_kandang_id" validate:"required"`
|
||||
Quantity float64 `json:"quantity" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
type Create struct {
|
||||
Name string `json:"name" validate:"required_strict,min=3"`
|
||||
TransferDate string `json:"transfer_date" validate:"required,datetime=2006-01-02"`
|
||||
SourceProjectFlockId uint `json:"source_project_flock_id" validate:"required"`
|
||||
TargetProjectFlockId uint `json:"target_project_flock_id" validate:"required"`
|
||||
Details []CreateDetail `json:"details" validate:"required,min=1,dive,required"`
|
||||
Reason string `json:"reason" validate:"omitempty,max=1000"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Name *string `json:"name,omitempty" validate:"omitempty"`
|
||||
Name *string `json:"name,omitempty" validate:"omitempty"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
|
||||
Reference in New Issue
Block a user