109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package productDescriptionRepo
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/db"
|
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
|
)
|
|
|
|
type UIProductDescriptionRepo interface {
|
|
GetProductDescription(productID uint, productLangID uint) (*model.ProductDescription, error)
|
|
CreateIfDoesNotExist(productID uint, productLangID uint) error
|
|
UpdateFields(productID uint, productLangID uint, updates map[string]string) error
|
|
GetMeiliProducts(id_lang uint) ([]model.MeiliSearchProduct, error)
|
|
}
|
|
|
|
type ProductDescriptionRepo struct{}
|
|
|
|
func New() UIProductDescriptionRepo {
|
|
return &ProductDescriptionRepo{}
|
|
}
|
|
|
|
// We assume that any user has access to all product descriptions
|
|
func (r *ProductDescriptionRepo) GetProductDescription(productID uint, productLangID uint) (*model.ProductDescription, error) {
|
|
var ProductDescription model.ProductDescription
|
|
|
|
err := db.DB.
|
|
Table("ps_product_lang").
|
|
Where("id_product = ? AND id_shop = ? AND id_lang = ?", productID, constdata.SHOP_ID, productLangID).
|
|
First(&ProductDescription).Error
|
|
if err != nil {
|
|
return nil, fmt.Errorf("database error: %w", err)
|
|
}
|
|
|
|
return &ProductDescription, nil
|
|
}
|
|
|
|
// If it doesn't exist, returns an error.
|
|
func (r *ProductDescriptionRepo) CreateIfDoesNotExist(productID uint, productLangID uint) error {
|
|
record := model.ProductDescription{
|
|
ProductID: productID,
|
|
ShopID: constdata.SHOP_ID,
|
|
LangID: productLangID,
|
|
}
|
|
|
|
err := db.DB.
|
|
Table("ps_product_lang").
|
|
Where("id_product = ? AND id_shop = ? AND id_lang = ?", productID, constdata.SHOP_ID, productLangID).
|
|
FirstOrCreate(&record).Error
|
|
if err != nil {
|
|
return fmt.Errorf("database error: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *ProductDescriptionRepo) UpdateFields(productID uint, productLangID uint, updates map[string]string) error {
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
updatesIface := make(map[string]interface{}, len(updates))
|
|
for k, v := range updates {
|
|
updatesIface[k] = v
|
|
}
|
|
|
|
err := db.DB.
|
|
Table("ps_product_lang").
|
|
Where("id_product = ? AND id_shop = ? AND id_lang = ?", productID, constdata.SHOP_ID, productLangID).
|
|
Updates(updatesIface).Error
|
|
if err != nil {
|
|
return fmt.Errorf("database error: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// We assume that any user has access to all product descriptions
|
|
func (r *ProductDescriptionRepo) GetMeiliProducts(id_lang uint) ([]model.MeiliSearchProduct, error) {
|
|
var products []model.MeiliSearchProduct
|
|
|
|
err := db.DB.
|
|
Select("pl.`usage` AS `usage`").
|
|
Select(`
|
|
ps.id_product AS id_product,
|
|
pl.name AS name,
|
|
ps.active AS active,
|
|
ps.price AS price,
|
|
pl.description AS description,
|
|
pl.description_short AS description_short,
|
|
p.ean13 AS ean13,
|
|
p.reference AS reference,
|
|
p.width AS width,
|
|
p.height AS height,
|
|
p.depth AS depth,
|
|
p.weight AS weight
|
|
`).
|
|
Table("ps_product_shop AS ps").
|
|
Joins("LEFT JOIN ps_product_lang AS pl ON ps.id_product = pl.id_product AND pl.id_shop = ? AND pl.id_lang = ?", constdata.SHOP_ID, id_lang).
|
|
Joins("LEFT JOIN ps_product AS p ON p.id_product = ps.id_product").
|
|
Where("ps.id_shop = ?", constdata.SHOP_ID).
|
|
Scan(&products).Error
|
|
if err != nil {
|
|
return products, fmt.Errorf("database error: %w", err)
|
|
}
|
|
|
|
return products, nil
|
|
}
|