76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package productService
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
|
"git.ma-al.com/goc_daniel/b2b/app/repos/productsRepo"
|
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
|
)
|
|
|
|
type ProductService struct {
|
|
productsRepo productsRepo.UIProductsRepo
|
|
}
|
|
|
|
func New() *ProductService {
|
|
return &ProductService{
|
|
productsRepo: productsRepo.New(),
|
|
}
|
|
}
|
|
|
|
func (s *ProductService) GetJSON(p_id_product, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error) {
|
|
products, err := s.productsRepo.GetJSON(p_id_product, constdata.SHOP_ID, p_id_lang, p_id_customer, b2b_id_country, p_quantity)
|
|
if err != nil {
|
|
return products, err
|
|
}
|
|
|
|
return products, nil
|
|
}
|
|
|
|
func (s *ProductService) Find(id_lang, userID uint, p find.Paging, filters *filters.FiltersList) (find.Found[model.ProductInList], error) {
|
|
return s.productsRepo.Find(id_lang, userID, p, filters)
|
|
}
|
|
|
|
func (s *ProductService) AddToFavorites(userID uint, productID uint) error {
|
|
count, err := s.productsRepo.ProductInDatabase(productID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count <= 0 {
|
|
return responseErrors.ErrProductNotFound
|
|
}
|
|
|
|
count, err = s.productsRepo.ExistsInFavorites(userID, productID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count >= 1 {
|
|
return responseErrors.ErrAlreadyInFavorites
|
|
}
|
|
|
|
return s.productsRepo.AddToFavorites(userID, productID)
|
|
}
|
|
|
|
func (s *ProductService) RemoveFromFavorites(userID uint, productID uint) error {
|
|
count, err := s.productsRepo.ProductInDatabase(productID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count <= 0 {
|
|
return responseErrors.ErrProductNotFound
|
|
}
|
|
|
|
count, err = s.productsRepo.ExistsInFavorites(userID, productID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count <= 0 {
|
|
return responseErrors.ErrNotInFavorites
|
|
}
|
|
|
|
return s.productsRepo.RemoveFromFavorites(userID, productID)
|
|
}
|