mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat(BE-36,37,38,39): master area, customer, kandang, location, warehouse
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type WarehouseController struct {
|
||||
WarehouseService service.WarehouseService
|
||||
}
|
||||
|
||||
func NewWarehouseController(warehouseService service.WarehouseService) *WarehouseController {
|
||||
return &WarehouseController{
|
||||
WarehouseService: warehouseService,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *WarehouseController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
result, totalResults, err := u.WarehouseService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.WarehouseListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all warehouses successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToWarehouseListDTOs(result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *WarehouseController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.WarehouseService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get warehouse successfully",
|
||||
Data: dto.ToWarehouseListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *WarehouseController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.WarehouseService.CreateOne(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create warehouse successfully",
|
||||
Data: dto.ToWarehouseListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *WarehouseController) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.WarehouseService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update warehouse successfully",
|
||||
Data: dto.ToWarehouseListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *WarehouseController) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.WarehouseService.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete warehouse successfully",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
||||
locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type WarehouseBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Area *areaDTO.AreaBaseDTO `json:"area"`
|
||||
Location *locationDTO.LocationBaseDTO `json:"location"`
|
||||
Kandang *kandangDTO.KandangBaseDTO `json:"kandang"`
|
||||
}
|
||||
|
||||
type WarehouseListDTO struct {
|
||||
WarehouseBaseDTO
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type WarehouseDetailDTO struct {
|
||||
WarehouseListDTO
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToWarehouseBaseDTO(e entity.Warehouse) WarehouseBaseDTO {
|
||||
var area *areaDTO.AreaBaseDTO
|
||||
if e.Area.Id != 0 {
|
||||
mapped := areaDTO.ToAreaBaseDTO(e.Area)
|
||||
area = &mapped
|
||||
}
|
||||
|
||||
var location *locationDTO.LocationBaseDTO
|
||||
if e.Location != nil && e.Location.Id != 0 {
|
||||
mapped := locationDTO.ToLocationBaseDTO(*e.Location)
|
||||
location = &mapped
|
||||
}
|
||||
|
||||
var kandang *kandangDTO.KandangBaseDTO
|
||||
if e.Kandang != nil && e.Kandang.Id != 0 {
|
||||
mapped := kandangDTO.ToKandangBaseDTO(*e.Kandang)
|
||||
kandang = &mapped
|
||||
}
|
||||
|
||||
return WarehouseBaseDTO{
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
Type: e.Type,
|
||||
Area: area,
|
||||
Location: location,
|
||||
Kandang: kandang,
|
||||
}
|
||||
}
|
||||
|
||||
func ToWarehouseListDTO(e entity.Warehouse) WarehouseListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
return WarehouseListDTO{
|
||||
WarehouseBaseDTO: ToWarehouseBaseDTO(e),
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
}
|
||||
}
|
||||
|
||||
func ToWarehouseListDTOs(e []entity.Warehouse) []WarehouseListDTO {
|
||||
result := make([]WarehouseListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToWarehouseListDTO(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package warehouses
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
sWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
|
||||
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
)
|
||||
|
||||
type WarehouseModule struct{}
|
||||
|
||||
func (WarehouseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
|
||||
warehouseService := sWarehouse.NewWarehouseService(warehouseRepo, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
WarehouseRoutes(router, userService, warehouseService)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type WarehouseRepository interface {
|
||||
repository.BaseRepository[entity.Warehouse]
|
||||
AreaExists(ctx context.Context, areaId uint) (bool, error)
|
||||
LocationExists(ctx context.Context, locationId uint) (bool, error)
|
||||
KandangExists(ctx context.Context, kandangId uint) (bool, error)
|
||||
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
|
||||
}
|
||||
|
||||
type WarehouseRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.Warehouse]
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewWarehouseRepository(db *gorm.DB) WarehouseRepository {
|
||||
return &WarehouseRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.Warehouse](db),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *WarehouseRepositoryImpl) AreaExists(ctx context.Context, areaId uint) (bool, error) {
|
||||
return repository.Exists[entity.Area](ctx, r.db, areaId)
|
||||
}
|
||||
|
||||
func (r *WarehouseRepositoryImpl) LocationExists(ctx context.Context, locationId uint) (bool, error) {
|
||||
return repository.Exists[entity.Location](ctx, r.db, locationId)
|
||||
}
|
||||
|
||||
func (r *WarehouseRepositoryImpl) KandangExists(ctx context.Context, kandangId uint) (bool, error) {
|
||||
return repository.Exists[entity.Kandang](ctx, r.db, kandangId)
|
||||
}
|
||||
|
||||
func (r *WarehouseRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
|
||||
return repository.ExistsByName[entity.Warehouse](ctx, r.db, name, excludeID)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package warehouses
|
||||
|
||||
import (
|
||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/controllers"
|
||||
warehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
|
||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func WarehouseRoutes(v1 fiber.Router, u user.UserService, s warehouse.WarehouseService) {
|
||||
ctrl := controller.NewWarehouseController(s)
|
||||
|
||||
route := v1.Group("/warehouses")
|
||||
|
||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Get("/", ctrl.GetAll)
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Get("/:id", ctrl.GetOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/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 WarehouseService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Warehouse, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.Warehouse, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Warehouse, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Warehouse, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type warehouseService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.WarehouseRepository
|
||||
}
|
||||
|
||||
func NewWarehouseService(repo repository.WarehouseRepository, validate *validator.Validate) WarehouseService {
|
||||
return &warehouseService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s warehouseService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Area").Preload("Location").Preload("Kandang")
|
||||
}
|
||||
|
||||
func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Warehouse, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
warehouses, 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+"%")
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get warehouses: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
return warehouses, total, nil
|
||||
}
|
||||
|
||||
func (s warehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.Warehouse, error) {
|
||||
warehouse, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get warehouse by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return warehouse, nil
|
||||
}
|
||||
|
||||
func (s *warehouseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Warehouse, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if exists, err := s.Repository.NameExists(c.Context(), req.Name, nil); err != nil {
|
||||
s.Log.Errorf("Failed to check warehouse name: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check warehouse name")
|
||||
} else if exists {
|
||||
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Warehouse with name %s already exists", req.Name))
|
||||
}
|
||||
|
||||
typ := strings.ToUpper(req.Type)
|
||||
if err := validateWarehouseTypeRequirements(typ, &req.AreaId, req.LocationId, req.KandangId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//? Check relation area, location, and kandang
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
common.RelationCheck{Name: "Area", ID: &req.AreaId, Exists: s.Repository.AreaExists},
|
||||
common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists},
|
||||
common.RelationCheck{Name: "Kandang", ID: req.KandangId, Exists: s.Repository.KandangExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//TODO: created by dummy
|
||||
createBody := &entity.Warehouse{
|
||||
Name: req.Name,
|
||||
Type: typ,
|
||||
AreaId: req.AreaId,
|
||||
CreatedBy: 1,
|
||||
}
|
||||
if req.LocationId != nil {
|
||||
createBody.LocationId = req.LocationId
|
||||
}
|
||||
if req.KandangId != nil {
|
||||
createBody.KandangId = req.KandangId
|
||||
}
|
||||
|
||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create warehouse: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
|
||||
}
|
||||
|
||||
func (s warehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Warehouse, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
existing, err := s.GetOne(c, id)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get warehouse for update: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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 warehouse name: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check warehouse name")
|
||||
} else if exists {
|
||||
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Warehouse with name %s already exists", *req.Name))
|
||||
}
|
||||
updateBody["name"] = *req.Name
|
||||
}
|
||||
if req.Type != nil {
|
||||
normalizedType := strings.ToUpper(*req.Type)
|
||||
updateBody["type"] = normalizedType
|
||||
req.Type = &normalizedType
|
||||
}
|
||||
if req.AreaId != nil {
|
||||
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Area", ID: req.AreaId, Exists: s.Repository.AreaExists}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updateBody["area_id"] = *req.AreaId
|
||||
}
|
||||
if req.LocationId != nil {
|
||||
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updateBody["location_id"] = req.LocationId
|
||||
}
|
||||
if req.KandangId != nil {
|
||||
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Kandang", ID: req.KandangId, Exists: s.Repository.KandangExists}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updateBody["kandang_id"] = req.KandangId
|
||||
}
|
||||
|
||||
finalType := strings.ToUpper(existing.Type)
|
||||
if req.Type != nil {
|
||||
finalType = *req.Type
|
||||
}
|
||||
finalAreaId := existing.AreaId
|
||||
if req.AreaId != nil {
|
||||
finalAreaId = *req.AreaId
|
||||
}
|
||||
finalLocationId := existing.LocationId
|
||||
if req.LocationId != nil {
|
||||
finalLocationId = req.LocationId
|
||||
}
|
||||
finalKandangId := existing.KandangId
|
||||
if req.KandangId != nil {
|
||||
finalKandangId = req.KandangId
|
||||
}
|
||||
|
||||
if err := validateWarehouseTypeRequirements(finalType, &finalAreaId, finalLocationId, finalKandangId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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, "Warehouse not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to update warehouse: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
func (s warehouseService) 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, "Warehouse not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete warehouse: %+v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateWarehouseTypeRequirements(typ string, areaID *uint, locationID *uint, kandangID *uint) error {
|
||||
switch utils.WarehouseType(typ) {
|
||||
case utils.WarehouseTypeArea:
|
||||
if areaID == nil || *areaID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is AREA")
|
||||
}
|
||||
if locationID != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "location_id must not be provided when type is AREA")
|
||||
}
|
||||
if kandangID != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is AREA")
|
||||
}
|
||||
return nil
|
||||
case utils.WarehouseTypeLokasi:
|
||||
if areaID == nil || *areaID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is LOCATION")
|
||||
}
|
||||
if locationID == nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "location_id is required when type is LOCATION")
|
||||
}
|
||||
if *locationID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is LOCATION")
|
||||
}
|
||||
if kandangID != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is LOCATION")
|
||||
}
|
||||
return nil
|
||||
case utils.WarehouseTypeKandang:
|
||||
if areaID == nil || *areaID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is KANDANG")
|
||||
}
|
||||
if locationID == nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "location_id is required when type is KANDANG")
|
||||
}
|
||||
if *locationID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is KANDANG")
|
||||
}
|
||||
if kandangID == nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_id is required when type is KANDANG")
|
||||
}
|
||||
if *kandangID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must be greater than 0 when type is KANDANG")
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid warehouse type")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package validation
|
||||
|
||||
type Create struct {
|
||||
Name string `json:"name" validate:"required_strict,min=3"`
|
||||
Type string `json:"type" validate:"required_strict"`
|
||||
AreaId uint `json:"area_id" validate:"required_strict,number,gt=0"`
|
||||
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||
KandangId *uint `json:"kandang_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
|
||||
Type *string `json:"type,omitempty" validate:"omitempty"`
|
||||
AreaId *uint `json:"area_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||
KandangId *uint `json:"kandang_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
}
|
||||
Reference in New Issue
Block a user