133 lines
3.8 KiB
Go
133 lines
3.8 KiB
Go
package products
|
|
|
|
import (
|
|
"context"
|
|
"enaklo-pos-be/internal/common/logger"
|
|
"enaklo-pos-be/internal/entity"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ProductRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewProductRepository(db *gorm.DB) *ProductRepository {
|
|
return &ProductRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (b *ProductRepository) CreateProduct(ctx context.Context, product *entity.ProductDB) (*entity.ProductDB, error) {
|
|
err := b.db.Create(product).Error
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when create product", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return product, nil
|
|
}
|
|
|
|
func (b *ProductRepository) UpdateProduct(ctx context.Context, product *entity.ProductDB) (*entity.ProductDB, error) {
|
|
if err := b.db.Save(product).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when update product", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return product, nil
|
|
}
|
|
|
|
func (b *ProductRepository) GetProductByID(ctx context.Context, id int64) (*entity.ProductDB, error) {
|
|
product := new(entity.ProductDB)
|
|
if err := b.db.Preload("Category").First(product, id).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when get by id product", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return product, nil
|
|
}
|
|
|
|
func (b *ProductRepository) GetProductByPartnerIDAndSiteID(ctx context.Context, partnerID, siteID int64) (entity.ProductList, error) {
|
|
var products []*entity.ProductDB
|
|
if err := b.db.WithContext(ctx).Where("partner_id = ? AND site_id = ?", partnerID, siteID).Find(&products).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when finding product by partner ID and site id", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return products, nil
|
|
}
|
|
|
|
func (b *ProductRepository) GetProductsBySiteID(ctx context.Context, siteID int64) (entity.ProductList, error) {
|
|
var products []*entity.ProductDB
|
|
if err := b.db.WithContext(ctx).Where("site_id = ?", siteID).Find(&products).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when finding product by partner ID and site id", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return products, nil
|
|
}
|
|
|
|
func (b *ProductRepository) GetAllProducts(ctx context.Context, req entity.ProductSearch) (entity.ProductList, int, error) {
|
|
var products []*entity.ProductDB
|
|
var total int64
|
|
|
|
query := b.db
|
|
query = query.Where("deleted_at is null")
|
|
|
|
if req.Search != "" {
|
|
query = query.Where("name ILIKE ?", "%"+req.Search+"%")
|
|
}
|
|
|
|
if req.Name != "" {
|
|
query = query.Where("name ILIKE ?", "%"+req.Name+"%")
|
|
}
|
|
|
|
if req.Type != "" {
|
|
query = query.Where("type = ? ", req.Type)
|
|
}
|
|
|
|
if req.CategoryID > 0 {
|
|
query = query.Where("category_id = ? ", req.CategoryID)
|
|
}
|
|
|
|
if req.PartnerID > 0 {
|
|
query = query.Where("partner_id = ? ", req.PartnerID)
|
|
}
|
|
|
|
if req.Limit > 0 {
|
|
query = query.Limit(req.Limit)
|
|
}
|
|
|
|
if req.Offset > 0 {
|
|
query = query.Offset(req.Offset)
|
|
}
|
|
|
|
if err := query.Preload("Category").Find(&products).Order("id ASC").Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when get all products", zap.Error(err))
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := b.db.Model(&entity.ProductDB{}).Where(query).Count(&total).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when count products", zap.Error(err))
|
|
return nil, 0, err
|
|
}
|
|
|
|
return products, int(total), nil
|
|
}
|
|
|
|
func (b *ProductRepository) DeleteProduct(ctx context.Context, id int64) error {
|
|
product := new(entity.ProductDB)
|
|
product.ID = id
|
|
if err := b.db.Delete(product).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *ProductRepository) GetProductsByIDs(ctx context.Context, ids []int64, partnerID int64) ([]*entity.ProductDB, error) {
|
|
var products []*entity.ProductDB
|
|
if err := b.db.Where("partner_id = ? AND id IN ?", partnerID, ids).Find(&products).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when getting products by IDs and partner ID", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return products, nil
|
|
}
|