ingredient composition
This commit is contained in:
@@ -3,285 +3,81 @@ package repository
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type IngredientCompositionRepository struct {
|
||||
db *sql.DB
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewIngredientCompositionRepository(db *sql.DB) *IngredientCompositionRepository {
|
||||
func NewIngredientCompositionRepository(db *gorm.DB) *IngredientCompositionRepository {
|
||||
return &IngredientCompositionRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *IngredientCompositionRepository) Create(ctx context.Context, composition *entities.IngredientComposition) error {
|
||||
query := `
|
||||
INSERT INTO ingredient_compositions (id, organization_id, outlet_id, parent_ingredient_id, child_ingredient_id, quantity, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
`
|
||||
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
composition.ID,
|
||||
composition.OrganizationID,
|
||||
composition.OutletID,
|
||||
composition.ParentIngredientID,
|
||||
composition.ChildIngredientID,
|
||||
composition.Quantity,
|
||||
composition.CreatedAt,
|
||||
composition.UpdatedAt,
|
||||
)
|
||||
|
||||
return err
|
||||
return r.db.WithContext(ctx).Create(composition).Error
|
||||
}
|
||||
|
||||
func (r *IngredientCompositionRepository) GetByID(ctx context.Context, id, organizationID uuid.UUID) (*entities.IngredientComposition, error) {
|
||||
query := `
|
||||
SELECT ic.id, ic.organization_id, ic.outlet_id, ic.parent_ingredient_id, ic.child_ingredient_id, ic.quantity, ic.created_at, ic.updated_at,
|
||||
pi.id, pi.organization_id, pi.outlet_id, pi.name, pi.unit_id, pi.cost, pi.stock, pi.is_semi_finished, pi.is_active, pi.metadata, pi.created_at, pi.updated_at,
|
||||
ci.id, ci.organization_id, ci.outlet_id, ci.name, ci.unit_id, ci.cost, ci.stock, ci.is_semi_finished, ci.is_active, ci.metadata, ci.created_at, ci.updated_at
|
||||
FROM ingredient_compositions ic
|
||||
LEFT JOIN ingredients pi ON ic.parent_ingredient_id = pi.id
|
||||
LEFT JOIN ingredients ci ON ic.child_ingredient_id = ci.id
|
||||
WHERE ic.id = $1 AND ic.organization_id = $2
|
||||
`
|
||||
|
||||
composition := &entities.IngredientComposition{}
|
||||
parentIngredient := &entities.Ingredient{}
|
||||
childIngredient := &entities.Ingredient{}
|
||||
|
||||
err := r.db.QueryRowContext(ctx, query, id, organizationID).Scan(
|
||||
&composition.ID,
|
||||
&composition.OrganizationID,
|
||||
&composition.OutletID,
|
||||
&composition.ParentIngredientID,
|
||||
&composition.ChildIngredientID,
|
||||
&composition.Quantity,
|
||||
&composition.CreatedAt,
|
||||
&composition.UpdatedAt,
|
||||
&parentIngredient.ID,
|
||||
&parentIngredient.OrganizationID,
|
||||
&parentIngredient.OutletID,
|
||||
&parentIngredient.Name,
|
||||
&parentIngredient.UnitID,
|
||||
&parentIngredient.Cost,
|
||||
&parentIngredient.Stock,
|
||||
&parentIngredient.IsSemiFinished,
|
||||
&parentIngredient.IsActive,
|
||||
&parentIngredient.Metadata,
|
||||
&parentIngredient.CreatedAt,
|
||||
&parentIngredient.UpdatedAt,
|
||||
&childIngredient.ID,
|
||||
&childIngredient.OrganizationID,
|
||||
&childIngredient.OutletID,
|
||||
&childIngredient.Name,
|
||||
&childIngredient.UnitID,
|
||||
&childIngredient.Cost,
|
||||
&childIngredient.Stock,
|
||||
&childIngredient.IsSemiFinished,
|
||||
&childIngredient.IsActive,
|
||||
&childIngredient.Metadata,
|
||||
&childIngredient.CreatedAt,
|
||||
&childIngredient.UpdatedAt,
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
composition.ParentIngredient = parentIngredient
|
||||
composition.ChildIngredient = childIngredient
|
||||
return composition, nil
|
||||
return &composition, nil
|
||||
}
|
||||
|
||||
func (r *IngredientCompositionRepository) GetByParentIngredientID(ctx context.Context, parentIngredientID, organizationID uuid.UUID) ([]*entities.IngredientComposition, error) {
|
||||
query := `
|
||||
SELECT ic.id, ic.organization_id, ic.outlet_id, ic.parent_ingredient_id, ic.child_ingredient_id, ic.quantity, ic.created_at, ic.updated_at,
|
||||
pi.id, pi.organization_id, pi.outlet_id, pi.name, pi.unit_id, pi.cost, pi.stock, pi.is_semi_finished, pi.is_active, pi.metadata, pi.created_at, pi.updated_at,
|
||||
ci.id, ci.organization_id, ci.outlet_id, ci.name, ci.unit_id, ci.cost, ci.stock, ci.is_semi_finished, ci.is_active, ci.metadata, ci.created_at, ci.updated_at
|
||||
FROM ingredient_compositions ic
|
||||
LEFT JOIN ingredients pi ON ic.parent_ingredient_id = pi.id
|
||||
LEFT JOIN ingredients ci ON ic.child_ingredient_id = ci.id
|
||||
WHERE ic.parent_ingredient_id = $1 AND ic.organization_id = $2
|
||||
ORDER BY ic.created_at DESC
|
||||
`
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, parentIngredientID, organizationID)
|
||||
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
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var compositions []*entities.IngredientComposition
|
||||
for rows.Next() {
|
||||
composition := &entities.IngredientComposition{}
|
||||
parentIngredient := &entities.Ingredient{}
|
||||
childIngredient := &entities.Ingredient{}
|
||||
|
||||
err := rows.Scan(
|
||||
&composition.ID,
|
||||
&composition.OrganizationID,
|
||||
&composition.OutletID,
|
||||
&composition.ParentIngredientID,
|
||||
&composition.ChildIngredientID,
|
||||
&composition.Quantity,
|
||||
&composition.CreatedAt,
|
||||
&composition.UpdatedAt,
|
||||
&parentIngredient.ID,
|
||||
&parentIngredient.OrganizationID,
|
||||
&parentIngredient.OutletID,
|
||||
&parentIngredient.Name,
|
||||
&parentIngredient.UnitID,
|
||||
&parentIngredient.Cost,
|
||||
&parentIngredient.Stock,
|
||||
&parentIngredient.IsSemiFinished,
|
||||
&parentIngredient.IsActive,
|
||||
&parentIngredient.Metadata,
|
||||
&parentIngredient.CreatedAt,
|
||||
&parentIngredient.UpdatedAt,
|
||||
&childIngredient.ID,
|
||||
&childIngredient.OrganizationID,
|
||||
&childIngredient.OutletID,
|
||||
&childIngredient.Name,
|
||||
&childIngredient.UnitID,
|
||||
&childIngredient.Cost,
|
||||
&childIngredient.Stock,
|
||||
&childIngredient.IsSemiFinished,
|
||||
&childIngredient.IsActive,
|
||||
&childIngredient.Metadata,
|
||||
&childIngredient.CreatedAt,
|
||||
&childIngredient.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
composition.ParentIngredient = parentIngredient
|
||||
composition.ChildIngredient = childIngredient
|
||||
compositions = append(compositions, composition)
|
||||
}
|
||||
|
||||
return compositions, nil
|
||||
}
|
||||
|
||||
func (r *IngredientCompositionRepository) GetByChildIngredientID(ctx context.Context, childIngredientID, organizationID uuid.UUID) ([]*entities.IngredientComposition, error) {
|
||||
query := `
|
||||
SELECT ic.id, ic.organization_id, ic.outlet_id, ic.parent_ingredient_id, ic.child_ingredient_id, ic.quantity, ic.created_at, ic.updated_at,
|
||||
pi.id, pi.organization_id, pi.outlet_id, pi.name, pi.unit_id, pi.cost, pi.stock, pi.is_semi_finished, pi.is_active, pi.metadata, pi.created_at, pi.updated_at,
|
||||
ci.id, ci.organization_id, ci.outlet_id, ci.name, ci.unit_id, ci.cost, ci.stock, ci.is_semi_finished, ci.is_active, ci.metadata, ci.created_at, ci.updated_at
|
||||
FROM ingredient_compositions ic
|
||||
LEFT JOIN ingredients pi ON ic.parent_ingredient_id = pi.id
|
||||
LEFT JOIN ingredients ci ON ic.child_ingredient_id = ci.id
|
||||
WHERE ic.child_ingredient_id = $1 AND ic.organization_id = $2
|
||||
ORDER BY ic.created_at DESC
|
||||
`
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, childIngredientID, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var compositions []*entities.IngredientComposition
|
||||
for rows.Next() {
|
||||
composition := &entities.IngredientComposition{}
|
||||
parentIngredient := &entities.Ingredient{}
|
||||
childIngredient := &entities.Ingredient{}
|
||||
|
||||
err := rows.Scan(
|
||||
&composition.ID,
|
||||
&composition.OrganizationID,
|
||||
&composition.OutletID,
|
||||
&composition.ParentIngredientID,
|
||||
&composition.ChildIngredientID,
|
||||
&composition.Quantity,
|
||||
&composition.CreatedAt,
|
||||
&composition.UpdatedAt,
|
||||
&parentIngredient.ID,
|
||||
&parentIngredient.OrganizationID,
|
||||
&parentIngredient.OutletID,
|
||||
&parentIngredient.Name,
|
||||
&parentIngredient.UnitID,
|
||||
&parentIngredient.Cost,
|
||||
&parentIngredient.Stock,
|
||||
&parentIngredient.IsSemiFinished,
|
||||
&parentIngredient.IsActive,
|
||||
&parentIngredient.Metadata,
|
||||
&parentIngredient.CreatedAt,
|
||||
&parentIngredient.UpdatedAt,
|
||||
&childIngredient.ID,
|
||||
&childIngredient.OrganizationID,
|
||||
&childIngredient.OutletID,
|
||||
&childIngredient.Name,
|
||||
&childIngredient.UnitID,
|
||||
&childIngredient.Cost,
|
||||
&childIngredient.Stock,
|
||||
&childIngredient.IsSemiFinished,
|
||||
&childIngredient.IsActive,
|
||||
&childIngredient.Metadata,
|
||||
&childIngredient.CreatedAt,
|
||||
&childIngredient.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
composition.ParentIngredient = parentIngredient
|
||||
composition.ChildIngredient = childIngredient
|
||||
compositions = append(compositions, composition)
|
||||
}
|
||||
|
||||
return compositions, nil
|
||||
}
|
||||
|
||||
func (r *IngredientCompositionRepository) Update(ctx context.Context, composition *entities.IngredientComposition) error {
|
||||
query := `
|
||||
UPDATE ingredient_compositions
|
||||
SET outlet_id = $1, quantity = $2, updated_at = $3
|
||||
WHERE id = $4 AND organization_id = $5
|
||||
`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query,
|
||||
composition.OutletID,
|
||||
composition.Quantity,
|
||||
composition.UpdatedAt,
|
||||
composition.ID,
|
||||
composition.OrganizationID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
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
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("no rows affected")
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *IngredientCompositionRepository) Delete(ctx context.Context, id, organizationID uuid.UUID) error {
|
||||
query := `DELETE FROM ingredient_compositions WHERE id = $1 AND organization_id = $2`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, id, organizationID)
|
||||
if err != nil {
|
||||
return err
|
||||
result := r.db.WithContext(ctx).
|
||||
Where("id = ? AND organization_id = ?", id, organizationID).
|
||||
Delete(&entities.IngredientComposition{})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("no rows affected")
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -24,7 +24,11 @@ func (r *IngredientRepository) Create(ctx context.Context, ingredient *entities.
|
||||
|
||||
func (r *IngredientRepository) GetByID(ctx context.Context, id, organizationID uuid.UUID) (*entities.Ingredient, error) {
|
||||
var ingredient entities.Ingredient
|
||||
err := r.db.WithContext(ctx).Preload("Unit").Where("id = ? AND organization_id = ?", id, organizationID).First(&ingredient).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("Unit").
|
||||
Preload("Compositions.ChildIngredient.Unit").
|
||||
Where("id = ? AND organization_id = ?", id, organizationID).
|
||||
First(&ingredient).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -57,7 +61,13 @@ func (r *IngredientRepository) GetAll(ctx context.Context, organizationID uuid.U
|
||||
|
||||
// Get paginated results with Unit preloaded
|
||||
offset := (page - 1) * limit
|
||||
err := query.Preload("Unit").Order("created_at DESC").Limit(limit).Offset(offset).Find(&ingredients).Error
|
||||
err := query.
|
||||
Preload("Unit").
|
||||
Preload("Compositions.ChildIngredient.Unit").
|
||||
Order("created_at DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&ingredients).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user