From d695bedc970ca4e686198909ff786088eed877b1 Mon Sep 17 00:00:00 2001 From: efrilm Date: Thu, 16 Apr 2026 17:14:18 +0700 Subject: [PATCH] update product response --- internal/entities/ingredient.go | 2 +- internal/repository/product_recipe_repository.go | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/entities/ingredient.go b/internal/entities/ingredient.go index 8a57d93..eadd025 100644 --- a/internal/entities/ingredient.go +++ b/internal/entities/ingredient.go @@ -19,5 +19,5 @@ type Ingredient struct { Metadata Metadata `gorm:"type:jsonb;default:'{}'" json:"metadata"` CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"` UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"` - Unit *Unit `gorm:"foreignKey:UnitID" json:"unit,omitempty"` + Unit *Unit `gorm:"foreignKey:UnitID;references:ID" json:"unit,omitempty"` } diff --git a/internal/repository/product_recipe_repository.go b/internal/repository/product_recipe_repository.go index ca955c6..0d706d2 100644 --- a/internal/repository/product_recipe_repository.go +++ b/internal/repository/product_recipe_repository.go @@ -27,6 +27,7 @@ func (r *ProductRecipeRepository) GetByID(ctx context.Context, id, organizationI Preload("Product"). Preload("ProductVariant"). Preload("Ingredient"). + Preload("Ingredient.Unit"). Where("id = ? AND organization_id = ?", id, organizationID). First(&productRecipe).Error if err != nil { @@ -41,6 +42,7 @@ func (r *ProductRecipeRepository) GetByProductID(ctx context.Context, productID, Preload("Product"). Preload("ProductVariant"). Preload("Ingredient"). + Preload("Ingredient.Unit"). Where("product_id = ? AND organization_id = ?", productID, organizationID). Order("created_at DESC"). Find(&productRecipes).Error @@ -56,6 +58,7 @@ func (r *ProductRecipeRepository) GetByProductAndVariantID(ctx context.Context, Preload("Product"). Preload("ProductVariant"). Preload("Ingredient"). + Preload("Ingredient.Unit"). Where("product_id = ? AND organization_id = ?", productID, organizationID) if variantID != nil { @@ -77,6 +80,7 @@ func (r *ProductRecipeRepository) GetByIngredientID(ctx context.Context, ingredi Preload("Product"). Preload("ProductVariant"). Preload("Ingredient"). + Preload("Ingredient.Unit"). Where("ingredient_id = ? AND organization_id = ?", ingredientID, organizationID). Order("created_at DESC"). Find(&productRecipes).Error @@ -96,15 +100,15 @@ func (r *ProductRecipeRepository) Delete(ctx context.Context, id, organizationID result := r.db.WithContext(ctx). Where("id = ? AND organization_id = ?", id, organizationID). Delete(&entities.ProductRecipe{}) - + if result.Error != nil { return result.Error } - + if result.RowsAffected == 0 { return gorm.ErrRecordNotFound } - + return nil } @@ -116,12 +120,12 @@ func (r *ProductRecipeRepository) DeleteByProductID(ctx context.Context, product func (r *ProductRecipeRepository) DeleteByProductAndVariantID(ctx context.Context, productID uuid.UUID, variantID *uuid.UUID, organizationID uuid.UUID) error { query := r.db.WithContext(ctx).Where("product_id = ? AND organization_id = ?", productID, organizationID) - + if variantID != nil { query = query.Where("variant_id = ?", *variantID) } else { query = query.Where("variant_id IS NULL") } - + return query.Delete(&entities.ProductRecipe{}).Error -} \ No newline at end of file +}