84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"apskel-pos-be/internal/entities"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type IngredientCompositionRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewIngredientCompositionRepository(db *gorm.DB) *IngredientCompositionRepository {
|
|
return &IngredientCompositionRepository{db: db}
|
|
}
|
|
|
|
func (r *IngredientCompositionRepository) Create(ctx context.Context, composition *entities.IngredientComposition) error {
|
|
return r.db.WithContext(ctx).Create(composition).Error
|
|
}
|
|
|
|
func (r *IngredientCompositionRepository) GetByID(ctx context.Context, id, organizationID uuid.UUID) (*entities.IngredientComposition, error) {
|
|
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
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &composition, nil
|
|
}
|
|
|
|
func (r *IngredientCompositionRepository) GetByParentIngredientID(ctx context.Context, parentIngredientID, organizationID uuid.UUID) ([]*entities.IngredientComposition, error) {
|
|
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
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return compositions, nil
|
|
}
|
|
|
|
func (r *IngredientCompositionRepository) Update(ctx context.Context, composition *entities.IngredientComposition) error {
|
|
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
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("no rows affected")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *IngredientCompositionRepository) Delete(ctx context.Context, id, organizationID uuid.UUID) error {
|
|
result := r.db.WithContext(ctx).
|
|
Where("id = ? AND organization_id = ?", id, organizationID).
|
|
Delete(&entities.IngredientComposition{})
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("no rows affected")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|