Feat(BE-36,37,38,39): master area, customer, kandang, location, warehouse

This commit is contained in:
Hafizh A. Y
2025-10-02 10:51:15 +07:00
parent dbc1f79a36
commit e8905be856
79 changed files with 3745 additions and 169 deletions
+24 -15
View File
@@ -3,20 +3,22 @@ package dto
import (
"time"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/models"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type UomBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Id uint `json:"id"`
Name string `json:"name"`
}
type UomListDTO struct {
UomBaseDTO
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type UomDetailDTO struct {
@@ -25,24 +27,31 @@ type UomDetailDTO struct {
// === Mapper Functions ===
func ToUomBaseDTO(m model.Uom) UomBaseDTO {
func ToUomBaseDTO(e entity.Uom) UomBaseDTO {
return UomBaseDTO{
Id: m.Id,
Name: m.Name,
Id: e.Id,
Name: e.Name,
}
}
func ToUomListDTO(m model.Uom) UomListDTO {
func ToUomListDTO(e entity.Uom) UomListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return UomListDTO{
UomBaseDTO: ToUomBaseDTO(m),
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
UomBaseDTO: ToUomBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToUomListDTOs(m []model.Uom) []UomListDTO {
result := make([]UomListDTO, len(m))
for i, r := range m {
func ToUomListDTOs(e []entity.Uom) []UomListDTO {
result := make([]UomListDTO, len(e))
for i, r := range e {
result[i] = ToUomListDTO(r)
}
return result
@@ -1,19 +0,0 @@
package model
import (
"time"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/users/models"
"gorm.io/gorm"
)
type Uom struct {
Id uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
CreatedBy int64 `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
CreatedUser model.User `gorm:"foreignKey:CreatedBy;references:Id"`
}
@@ -1,21 +1,30 @@
package repository
import (
model "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/models"
"gitlab.com/mbugroup/lti-api.git/internal/repository"
"context"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gorm.io/gorm"
)
type UomRepository interface {
repository.BaseRepository[model.Uom]
repository.BaseRepository[entity.Uom]
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type UomRepositoryImpl struct {
*repository.BaseRepositoryImpl[model.Uom]
*repository.BaseRepositoryImpl[entity.Uom]
db *gorm.DB
}
func NewUomRepository(db *gorm.DB) UomRepository {
return &UomRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[model.Uom](db),
BaseRepositoryImpl: repository.NewBaseRepository[entity.Uom](db),
db: db,
}
}
func (r *UomRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Uom](ctx, r.db, name, excludeID)
}
@@ -2,8 +2,9 @@ package service
import (
"errors"
"fmt"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/models"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
@@ -15,10 +16,10 @@ import (
)
type UomService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]model.Uom, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*model.Uom, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*model.Uom, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*model.Uom, error)
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Uom, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Uom, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Uom, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Uom, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
@@ -35,7 +36,12 @@ func NewUomService(repo repository.UomRepository, validate *validator.Validate)
Repository: repo,
}
}
func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Uom, int64, error) {
func (s uomService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser")
}
func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Uom, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
@@ -43,6 +49,7 @@ func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Uom,
offset := (params.Page - 1) * params.Limit
uoms, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.Search != "" {
return db.Where("name LIKE ?", "%"+params.Search+"%")
}
@@ -56,8 +63,8 @@ func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Uom,
return uoms, total, nil
}
func (s uomService) GetOne(c *fiber.Ctx, id uint) (*model.Uom, error) {
uom, err := s.Repository.GetByID(c.Context(), id, nil)
func (s uomService) GetOne(c *fiber.Ctx, id uint) (*entity.Uom, error) {
uom, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Uom not found")
}
@@ -68,12 +75,20 @@ func (s uomService) GetOne(c *fiber.Ctx, id uint) (*model.Uom, error) {
return uom, nil
}
func (s *uomService) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.Uom, error) {
func (s *uomService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Uom, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
createBody := &model.Uom{
if exists, err := s.Repository.NameExists(c.Context(), req.Name, nil); err != nil {
s.Log.Errorf("Failed to check uom name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check uom name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Uom with name %s already exists", req.Name))
}
//TODO: created by dummy
createBody := &entity.Uom{
Name: req.Name,
CreatedBy: 1,
}
@@ -83,10 +98,10 @@ func (s *uomService) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.Uom
return nil, err
}
return createBody, nil
return s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
}
func (s uomService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*model.Uom, error) {
func (s uomService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Uom, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
@@ -94,9 +109,19 @@ func (s uomService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*m
updateBody := make(map[string]any)
if req.Name != nil {
if exists, err := s.Repository.NameExists(c.Context(), *req.Name, &id); err != nil {
s.Log.Errorf("Failed to check uom name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check uom name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Uom with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
if len(updateBody) == 0 {
return s.GetOne(c, id)
}
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Uom not found")