catching errors again
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model/dbmodel"
|
||||
"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"
|
||||
"git.ma-al.com/goc_marek/gormcol"
|
||||
"github.com/WinterYukky/gorm-extra-clause-plugin/exclause"
|
||||
)
|
||||
@@ -20,6 +19,8 @@ type UIProductsRepo interface {
|
||||
Find(id_lang, userID uint, p find.Paging, filt *filters.FiltersList) (find.Found[model.ProductInList], error)
|
||||
AddToFavorites(userID uint, productID uint) error
|
||||
RemoveFromFavorites(userID uint, productID uint) error
|
||||
ExistsInFavorites(userID uint, productID uint) (int64, error)
|
||||
ProductInDatabase(productID uint) (int64, error)
|
||||
}
|
||||
|
||||
type ProductsRepo struct{}
|
||||
@@ -130,29 +131,6 @@ func (repo *ProductsRepo) Find(id_lang, userID uint, p find.Paging, filt *filter
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) AddToFavorites(userID uint, productID uint) error {
|
||||
var count int64
|
||||
err := db.Get().
|
||||
Table(dbmodel.TableNamePsProduct).
|
||||
Where(dbmodel.PsProductCols.IDProduct.Col()+" = ?", productID).
|
||||
Count(&count).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return responseErrors.ErrProductNotFound
|
||||
}
|
||||
|
||||
err = db.Get().
|
||||
Table("b2b_favorites").
|
||||
Where("user_id = ? AND product_id = ?", userID, productID).
|
||||
Count(&count).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return responseErrors.ErrAlreadyInFavorites
|
||||
}
|
||||
|
||||
fav := model.B2bFavorite{
|
||||
UserID: userID,
|
||||
ProductID: productID,
|
||||
@@ -165,3 +143,21 @@ func (repo *ProductsRepo) RemoveFromFavorites(userID uint, productID uint) error
|
||||
Where("user_id = ? AND product_id = ?", userID, productID).
|
||||
Delete(&model.B2bFavorite{}).Error
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) ExistsInFavorites(userID uint, productID uint) (int64, error) {
|
||||
var count int64
|
||||
err := db.Get().
|
||||
Table("b2b_favorites").
|
||||
Where("user_id = ? AND product_id = ?", userID, productID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) ProductInDatabase(productID uint) (int64, error) {
|
||||
var count int64
|
||||
err := db.Get().
|
||||
Table(dbmodel.TableNamePsProduct).
|
||||
Where(dbmodel.PsProductCols.IDProduct.Col()+" = ?", productID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
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 {
|
||||
@@ -34,9 +35,41 @@ func (s *ProductService) Find(id_lang, userID uint, p find.Paging, filters *filt
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ info:
|
||||
|
||||
http:
|
||||
method: POST
|
||||
url: "{{bas_url}}/restricted/product/favorite/51"
|
||||
url: "{{bas_url}}/restricted/product/favorite/53"
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
|
||||
@@ -5,7 +5,7 @@ info:
|
||||
|
||||
http:
|
||||
method: DELETE
|
||||
url: "{{bas_url}}/restricted/product/favorite/1"
|
||||
url: "{{bas_url}}/restricted/product/favorite/51"
|
||||
auth: inherit
|
||||
|
||||
settings:
|
||||
|
||||
Reference in New Issue
Block a user