Files

84 lines
2.7 KiB
Go
Raw Permalink Normal View History

2025-08-03 23:55:51 +07:00
package repository
import (
"apskel-pos-be/internal/entities"
"context"
2026-04-27 21:17:12 +07:00
"fmt"
2025-08-03 23:55:51 +07:00
"github.com/google/uuid"
2026-04-27 21:17:12 +07:00
"gorm.io/gorm"
2025-08-03 23:55:51 +07:00
)
type IngredientCompositionRepository struct {
2026-04-27 21:17:12 +07:00
db *gorm.DB
2025-08-03 23:55:51 +07:00
}
2026-04-27 21:17:12 +07:00
func NewIngredientCompositionRepository(db *gorm.DB) *IngredientCompositionRepository {
2025-08-03 23:55:51 +07:00
return &IngredientCompositionRepository{db: db}
}
func (r *IngredientCompositionRepository) Create(ctx context.Context, composition *entities.IngredientComposition) error {
2026-04-27 21:17:12 +07:00
return r.db.WithContext(ctx).Create(composition).Error
2025-08-03 23:55:51 +07:00
}
func (r *IngredientCompositionRepository) GetByID(ctx context.Context, id, organizationID uuid.UUID) (*entities.IngredientComposition, error) {
2026-04-27 21:17:12 +07:00
var composition entities.IngredientComposition
err := r.db.WithContext(ctx).
Preload("ChildIngredient.Unit").
Preload("ParentIngredient.Unit").
Preload("ParentIngredient.Compositions.ChildIngredient.Unit").
Where("id = ? AND organization_id = ?", id, organizationID).
First(&composition).Error
2025-08-03 23:55:51 +07:00
if err != nil {
return nil, err
}
2026-04-27 21:17:12 +07:00
return &composition, nil
2025-08-03 23:55:51 +07:00
}
func (r *IngredientCompositionRepository) GetByParentIngredientID(ctx context.Context, parentIngredientID, organizationID uuid.UUID) ([]*entities.IngredientComposition, error) {
2026-04-27 21:17:12 +07:00
var compositions []*entities.IngredientComposition
err := r.db.WithContext(ctx).
Preload("ChildIngredient.Unit").
Where("parent_ingredient_id = ? AND organization_id = ?", parentIngredientID, organizationID).
Order("created_at ASC").
Find(&compositions).Error
2025-08-03 23:55:51 +07:00
if err != nil {
return nil, err
}
return compositions, nil
}
func (r *IngredientCompositionRepository) Update(ctx context.Context, composition *entities.IngredientComposition) error {
2026-04-27 21:17:12 +07:00
result := r.db.WithContext(ctx).
Model(&entities.IngredientComposition{}).
Where("id = ? AND organization_id = ?", composition.ID, composition.OrganizationID).
Select("outlet_id", "quantity", "updated_at").
Updates(composition)
if result.Error != nil {
return result.Error
2025-08-03 23:55:51 +07:00
}
2026-04-27 21:17:12 +07:00
if result.RowsAffected == 0 {
return fmt.Errorf("no rows affected")
2025-08-03 23:55:51 +07:00
}
return nil
}
func (r *IngredientCompositionRepository) Delete(ctx context.Context, id, organizationID uuid.UUID) error {
2026-04-27 21:17:12 +07:00
result := r.db.WithContext(ctx).
Where("id = ? AND organization_id = ?", id, organizationID).
Delete(&entities.IngredientComposition{})
if result.Error != nil {
return result.Error
2025-08-03 23:55:51 +07:00
}
2026-04-27 21:17:12 +07:00
if result.RowsAffected == 0 {
return fmt.Errorf("no rows affected")
2025-08-03 23:55:51 +07:00
}
return nil
}
2026-04-27 21:17:12 +07:00
func (r *IngredientCompositionRepository) DeleteByParentIngredientID(ctx context.Context, parentIngredientID, organizationID uuid.UUID) error {
return r.db.WithContext(ctx).
Where("parent_ingredient_id = ? AND organization_id = ?", parentIngredientID, organizationID).
Delete(&entities.IngredientComposition{}).Error
}