package productDescriptionRepo import ( "errors" "fmt" "git.ma-al.com/goc_daniel/b2b/app/config" "git.ma-al.com/goc_daniel/b2b/app/db" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/model/dbmodel" constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data" "gorm.io/gorm" ) type UIProductDescriptionRepo interface { GetProductDescription(productID uint, productid_lang uint) (*model.ProductDescription, error) CreateIfDoesNotExist(productID uint, productid_lang uint) error UpdateFields(productID uint, productid_lang uint, updates map[string]string) 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, productid_lang uint) (*model.ProductDescription, error) { var ProductDescription model.ProductDescription err := db.Get(). Model(dbmodel.PsProductLang{}). Where(&dbmodel.PsProductLang{ IDProduct: int32(productID), IDShop: int32(constdata.SHOP_ID), IDLang: int32(productid_lang), }). Select(` `+dbmodel.PsProductLangCols.IDProduct.TabCol()+` AS id_product, `+dbmodel.PsProductLangCols.IDShop.TabCol()+` AS id_shop, `+dbmodel.PsProductLangCols.IDLang.TabCol()+` AS id_lang, `+dbmodel.PsProductLangCols.Description.TabCol()+` AS description, `+dbmodel.PsProductLangCols.DescriptionShort.TabCol()+` AS description_short, `+dbmodel.PsProductLangCols.LinkRewrite.TabCol()+` AS link_rewrite, `+dbmodel.PsProductLangCols.MetaDescription.TabCol()+` AS meta_description, `+dbmodel.PsProductLangCols.MetaKeywords.TabCol()+` AS meta_keywords, `+dbmodel.PsProductLangCols.MetaTitle.TabCol()+` AS meta_title, `+dbmodel.PsProductLangCols.Name.TabCol()+` AS name, `+dbmodel.PsProductLangCols.AvailableNow.TabCol()+` AS available_now, `+dbmodel.PsProductLangCols.AvailableLater.TabCol()+` AS available_later, `+dbmodel.PsProductLangCols.DeliveryInStock.TabCol()+` AS delivery_in_stock, `+dbmodel.PsProductLangCols.DeliveryOutStock.TabCol()+` AS delivery_out_stock, `+dbmodel.PsProductLangCols.Usage.TabCol()+` AS `+"`usage`"+`, CONCAT(?, '/', `+dbmodel.PsImageShopCols.IDImage.TabCol()+`, '-large_default/', `+dbmodel.PsProductLangCols.LinkRewrite.TabCol()+`, '.webp') AS image_link `, config.Get().Image.ImagePrefix). Joins("JOIN " + dbmodel.TableNamePsImageShop + " ON " + dbmodel.PsImageShopCols.IDProduct.TabCol() + "=" + dbmodel.PsProductLangCols.IDProduct.TabCol() + " AND " + dbmodel.PsImageShopCols.Cover.TabCol() + " = 1"). First(&ProductDescription).Error if errors.Is(err, gorm.ErrRecordNotFound) { // handle "not found" case only ProductDescription.ExistsInDatabase = false } else if err != nil { return nil, fmt.Errorf("database error: %w", err) } else { ProductDescription.ExistsInDatabase = true } return &ProductDescription, nil } // If it doesn't exist, returns an error. func (r *ProductDescriptionRepo) CreateIfDoesNotExist(productID uint, productid_lang uint) error { record := dbmodel.PsProductLang{ IDProduct: int32(productID), IDShop: int32(constdata.SHOP_ID), IDLang: int32(productid_lang), } err := db.Get(). Model(dbmodel.PsProductLang{}). Where(&dbmodel.PsProductLang{ IDProduct: int32(productID), IDShop: int32(constdata.SHOP_ID), IDLang: int32(productid_lang), }). FirstOrCreate(&record).Error if err != nil { return fmt.Errorf("database error: %w", err) } return nil } func (r *ProductDescriptionRepo) UpdateFields(productID uint, productid_lang 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.Get(). Model(&dbmodel.PsProductLang{}). Where(&dbmodel.PsProductLang{ IDProduct: int32(productID), IDShop: int32(constdata.SHOP_ID), IDLang: int32(productid_lang), }). Updates(updatesIface).Error if err != nil { return fmt.Errorf("database error: %w", err) } return nil }