mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
a0bdc7b23c
✅ DB Schema: product_warehouse entity and migration ✅ Master Data: added filter params to getall APIs 🚧 Pending: stock_logs implementation and adjustment APIs
170 lines
5.1 KiB
Go
170 lines
5.1 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
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/locations/repositories"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/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 LocationService interface {
|
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Location, int64, error)
|
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.Location, error)
|
|
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Location, error)
|
|
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Location, error)
|
|
DeleteOne(ctx *fiber.Ctx, id uint) error
|
|
}
|
|
|
|
type locationService struct {
|
|
Log *logrus.Logger
|
|
Validate *validator.Validate
|
|
Repository repository.LocationRepository
|
|
}
|
|
|
|
func NewLocationService(repo repository.LocationRepository, validate *validator.Validate) LocationService {
|
|
return &locationService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
Repository: repo,
|
|
}
|
|
}
|
|
|
|
func (s locationService) withRelations(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("Area").Preload("CreatedUser")
|
|
}
|
|
|
|
func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Location, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
|
|
locations, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
|
db = s.withRelations(db)
|
|
if params.Search != "" {
|
|
db = db.Where("name LIKE ?", "%"+params.Search+"%")
|
|
}
|
|
if params.AreaId != 0 {
|
|
db = db.Where("area_id = ?", params.AreaId)
|
|
}
|
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
|
})
|
|
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to get locations: %+v", err)
|
|
return nil, 0, err
|
|
}
|
|
return locations, total, nil
|
|
}
|
|
|
|
func (s locationService) GetOne(c *fiber.Ctx, id uint) (*entity.Location, error) {
|
|
location, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Location not found")
|
|
}
|
|
if err != nil {
|
|
s.Log.Errorf("Failed get location by id: %+v", err)
|
|
return nil, err
|
|
}
|
|
return location, nil
|
|
}
|
|
|
|
func (s *locationService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Location, 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 location name: %+v", err)
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check location name")
|
|
} else if exists {
|
|
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Location with name %s already exists", req.Name))
|
|
}
|
|
|
|
if err := common.EnsureRelations(c.Context(),
|
|
common.RelationCheck{Name: "Area", ID: &req.AreaId, Exists: s.Repository.AreaExists},
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
//TODO: created by dummy
|
|
createBody := &entity.Location{
|
|
Name: req.Name,
|
|
Address: req.Address,
|
|
AreaId: req.AreaId,
|
|
CreatedBy: 1,
|
|
}
|
|
|
|
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
|
s.Log.Errorf("Failed to create location: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return s.GetOne(c, createBody.Id)
|
|
}
|
|
|
|
func (s locationService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Location, error) {
|
|
if err := s.Validate.Struct(req); err != nil {
|
|
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 location name: %+v", err)
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check location name")
|
|
} else if exists {
|
|
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Location with name %s already exists", *req.Name))
|
|
}
|
|
updateBody["name"] = *req.Name
|
|
}
|
|
|
|
if req.Address != nil {
|
|
updateBody["address"] = *req.Address
|
|
}
|
|
|
|
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Area", ID: req.AreaId, Exists: s.Repository.AreaExists}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if req.AreaId != nil {
|
|
updateBody["area_id"] = *req.AreaId
|
|
}
|
|
|
|
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, "Location not found")
|
|
}
|
|
s.Log.Errorf("Failed to update location: %+v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return s.GetOne(c, id)
|
|
}
|
|
|
|
func (s locationService) 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, "Location not found")
|
|
}
|
|
s.Log.Errorf("Failed to delete location: %+v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|