init project
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package products
|
||||
|
||||
import (
|
||||
"context"
|
||||
"furtuna-be/internal/common/logger"
|
||||
"furtuna-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.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) 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.BranchID > 0 {
|
||||
query = query.Where("branch_id = ? ", req.BranchID)
|
||||
}
|
||||
|
||||
if req.Available != "" {
|
||||
if req.Available.IsAvailable() {
|
||||
query = query.Where("stock_qty > 0 ")
|
||||
} else if req.Available.IsUnavailable() {
|
||||
query = query.Where("stock_qty < 1 ")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Limit > 0 {
|
||||
query = query.Limit(req.Limit)
|
||||
}
|
||||
|
||||
if req.Offset > 0 {
|
||||
query = query.Offset(req.Offset)
|
||||
}
|
||||
|
||||
if err := query.Find(&products).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
|
||||
}
|
||||
Reference in New Issue
Block a user