package product import ( "context" "furtuna-be/internal/common/logger" "furtuna-be/internal/common/mycontext" "furtuna-be/internal/entity" "furtuna-be/internal/repository" "go.uber.org/zap" ) type ProductService struct { repo repository.Product } func NewProductService(repo repository.Product) *ProductService { return &ProductService{ repo: repo, } } func (s *ProductService) Create(ctx mycontext.Context, productReq *entity.Product) (*entity.Product, error) { productDB := productReq.ToProductDB() productDB.CreatedBy = ctx.RequestedBy() productDB, err := s.repo.CreateProduct(ctx, productDB) if err != nil { logger.ContextLogger(ctx).Error("error when create product", zap.Error(err)) return nil, err } return productDB.ToProduct(), nil } func (s *ProductService) Update(ctx mycontext.Context, id int64, productReq *entity.Product) (*entity.Product, error) { existingProduct, err := s.repo.GetProductByID(ctx, id) if err != nil { return nil, err } existingProduct.ToUpdatedProduct(ctx.RequestedBy(), *productReq) updatedProductDB, err := s.repo.UpdateProduct(ctx, existingProduct.ToProductDB()) if err != nil { logger.ContextLogger(ctx).Error("error when update product", zap.Error(err)) return nil, err } return updatedProductDB.ToProduct(), nil } func (s *ProductService) GetByID(ctx context.Context, id int64) (*entity.Product, error) { productDB, err := s.repo.GetProductByID(ctx, id) if err != nil { logger.ContextLogger(ctx).Error("error when get product by id", zap.Error(err)) return nil, err } return productDB.ToProduct(), nil } func (s *ProductService) GetAll(ctx context.Context, search entity.ProductSearch) ([]*entity.Product, int, error) { products, total, err := s.repo.GetAllProducts(ctx, search) if err != nil { logger.ContextLogger(ctx).Error("error when get all products", zap.Error(err)) return nil, 0, err } return products.ToProductList(), total, nil } func (s *ProductService) GetProductPOS(ctx context.Context, search entity.ProductPOS) ([]*entity.Product, error) { products, err := s.repo.GetProductByPartnerIDAndSiteID(ctx, search.PartnerID, search.SiteID) if err != nil { logger.ContextLogger(ctx).Error("error when get all products", zap.Error(err)) return nil, err } return products.ToProductListPOS(), nil } func (s *ProductService) Delete(ctx mycontext.Context, id int64) error { productDB, err := s.repo.GetProductByID(ctx, id) if err != nil { logger.ContextLogger(ctx).Error("error when get product by id", zap.Error(err)) return err } productDB.SetDeleted(ctx.RequestedBy()) _, err = s.repo.UpdateProduct(ctx, productDB) if err != nil { logger.ContextLogger(ctx).Error("error when update product", zap.Error(err)) return err } return nil }