12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package db
- import (
- "context"
- "youngee_b_api/model/gorm_model"
- "gorm.io/gorm"
- )
- func CreateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
- db := GetReadDB(ctx)
- err := db.Create(&product).Error
- if err != nil {
- return nil, err
- }
- return &product.ProductID, nil
- }
- func UpdateProduct(ctx context.Context, product gorm_model.YounggeeProduct) (*int64, error) {
- db := GetReadDB(ctx)
- err := db.Model(&product).Updates(product).Error
- if err != nil {
- return nil, err
- }
- return &product.ProductID, nil
- }
- func FindAllProduct(ctx context.Context, enterpriseID int64) ([]gorm_model.YounggeeProduct, error) {
- db := GetReadDB(ctx)
- products := []gorm_model.YounggeeProduct{}
- err := db.Where("enterprise_id = ?", enterpriseID).Find(&products).Error
- if err != nil {
- return nil, err
- }
- return products, nil
- }
- func FindProductByID(ctx context.Context, productID int64) (*gorm_model.YounggeeProduct, error) {
- db := GetReadDB(ctx)
- product := &gorm_model.YounggeeProduct{}
- err := db.First(&product, productID).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return product, nil
- }
- func FindProductByName(ctx context.Context, brandName string, productName string) (*int64, error) {
- db := GetReadDB(ctx)
- product := &gorm_model.YounggeeProduct{}
- err := db.Where("product_name = ? AND brand_name = ?", productName, brandName).First(&product).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return &product.ProductID, nil
- }
|