mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
26bf7f165e
This reverts commit dff9e73ab1.
99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-stocks/validations"
|
|
productRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/repositories"
|
|
"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 ProductStockService interface {
|
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Product, int64, error)
|
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.Product, error)
|
|
}
|
|
|
|
type productStockService struct {
|
|
Log *logrus.Logger
|
|
Validate *validator.Validate
|
|
ProductRepository productRepository.ProductRepository
|
|
}
|
|
|
|
func NewProductStockService(
|
|
productRepo productRepository.ProductRepository,
|
|
validate *validator.Validate,
|
|
) ProductStockService {
|
|
return &productStockService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
ProductRepository: productRepo,
|
|
}
|
|
}
|
|
|
|
func (s productStockService) withRelations(db *gorm.DB) *gorm.DB {
|
|
return db.
|
|
Preload("CreatedUser").
|
|
Preload("Uom").
|
|
Preload("ProductCategory").
|
|
Preload("Flags").
|
|
Preload("ProductWarehouses").
|
|
Preload("ProductWarehouses.Warehouse").
|
|
Preload("ProductWarehouses.Warehouse.Location").
|
|
Preload("ProductWarehouses.Warehouse.Location.Area").
|
|
Preload("ProductWarehouses.StockLogs", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("created_at ASC")
|
|
}).
|
|
Preload("ProductWarehouses.StockLogs.CreatedUser").
|
|
Preload("ProductSuppliers").
|
|
Preload("ProductSuppliers.Supplier", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("suppliers.name ASC")
|
|
})
|
|
}
|
|
|
|
func (s productStockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Product, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
|
|
productStocks, total, err := s.ProductRepository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
|
db = db.Where(`EXISTS (
|
|
SELECT 1
|
|
FROM product_warehouses pw
|
|
WHERE pw.product_id = products.id
|
|
AND pw.qty > 0
|
|
)`)
|
|
|
|
db = s.withRelations(db)
|
|
if params.Search != "" {
|
|
db = db.Where("products.name ILIKE ?", "%"+params.Search+"%")
|
|
}
|
|
return db.Order("products.created_at DESC").Order("products.updated_at DESC")
|
|
})
|
|
|
|
if err != nil {
|
|
s.Log.Errorf("Failed to get productStocks: %+v", err)
|
|
return nil, 0, err
|
|
}
|
|
return productStocks, total, nil
|
|
}
|
|
|
|
func (s productStockService) GetOne(c *fiber.Ctx, id uint) (*entity.Product, error) {
|
|
product, err := s.ProductRepository.GetByID(c.Context(), id, s.withRelations)
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Product not found")
|
|
}
|
|
if err != nil {
|
|
s.Log.Errorf("Failed get product by id: %+v", err)
|
|
return nil, err
|
|
}
|
|
return product, nil
|
|
}
|