refactor: introduce internal/domain/ layer, rename model.go → entity.go
Move all business-logic packages from internal/ root into internal/domain/: auth, cuisine, diary, dish, home, ingredient, language, menu, product, recipe, recognition, recommendation, savedrecipe, tag, units, user Rename model.go → entity.go in packages that hold domain entities: diary, dish, home, ingredient, menu, product, recipe, savedrecipe, user Update all import paths accordingly (adapters, infra/server, cmd/server, tests). No logic changes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
49
backend/internal/domain/recipe/entity.go
Normal file
49
backend/internal/domain/recipe/entity.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package recipe
|
||||
|
||||
import "time"
|
||||
|
||||
// Recipe is a cooking variant of a Dish in the catalog.
|
||||
// It links to a Dish for all presentational data (name, image, cuisine, tags).
|
||||
type Recipe struct {
|
||||
ID string `json:"id"`
|
||||
DishID string `json:"dish_id"`
|
||||
Source string `json:"source"` // ai | user | spoonacular
|
||||
|
||||
Difficulty *string `json:"difficulty"`
|
||||
PrepTimeMin *int `json:"prep_time_min"`
|
||||
CookTimeMin *int `json:"cook_time_min"`
|
||||
Servings *int `json:"servings"`
|
||||
|
||||
CaloriesPerServing *float64 `json:"calories_per_serving"`
|
||||
ProteinPerServing *float64 `json:"protein_per_serving"`
|
||||
FatPerServing *float64 `json:"fat_per_serving"`
|
||||
CarbsPerServing *float64 `json:"carbs_per_serving"`
|
||||
FiberPerServing *float64 `json:"fiber_per_serving"`
|
||||
|
||||
Ingredients []RecipeIngredient `json:"ingredients"`
|
||||
Steps []RecipeStep `json:"steps"`
|
||||
Notes *string `json:"notes,omitempty"`
|
||||
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// RecipeIngredient is a single ingredient row from recipe_ingredients.
|
||||
type RecipeIngredient struct {
|
||||
ID string `json:"id"`
|
||||
IngredientID *string `json:"ingredient_id"`
|
||||
Name string `json:"name"`
|
||||
Amount float64 `json:"amount"`
|
||||
UnitCode *string `json:"unit_code"`
|
||||
IsOptional bool `json:"is_optional"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
// RecipeStep is a single step row from recipe_steps.
|
||||
type RecipeStep struct {
|
||||
ID string `json:"id"`
|
||||
StepNumber int `json:"step_number"`
|
||||
Description string `json:"description"`
|
||||
TimerSeconds *int `json:"timer_seconds"`
|
||||
ImageURL *string `json:"image_url"`
|
||||
}
|
||||
58
backend/internal/domain/recipe/handler.go
Normal file
58
backend/internal/domain/recipe/handler.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package recipe
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/food-ai/backend/internal/infra/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// Handler handles HTTP requests for recipes.
|
||||
type Handler struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
// NewHandler creates a new Handler.
|
||||
func NewHandler(repo *Repository) *Handler {
|
||||
return &Handler{repo: repo}
|
||||
}
|
||||
|
||||
// GetByID handles GET /recipes/{id}.
|
||||
func (h *Handler) GetByID(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.UserIDFromCtx(r.Context())
|
||||
if userID == "" {
|
||||
writeErrorJSON(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
id := chi.URLParam(r, "id")
|
||||
rec, err := h.repo.GetByID(r.Context(), id)
|
||||
if err != nil {
|
||||
slog.Error("get recipe", "id", id, "err", err)
|
||||
writeErrorJSON(w, http.StatusInternalServerError, "failed to get recipe")
|
||||
return
|
||||
}
|
||||
if rec == nil {
|
||||
writeErrorJSON(w, http.StatusNotFound, "recipe not found")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, rec)
|
||||
}
|
||||
|
||||
type errorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func writeErrorJSON(w http.ResponseWriter, status int, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(errorResponse{Error: msg})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
142
backend/internal/domain/recipe/repository.go
Normal file
142
backend/internal/domain/recipe/repository.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package recipe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/food-ai/backend/internal/infra/locale"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// Repository handles persistence for recipes and their relational sub-tables.
|
||||
type Repository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewRepository creates a new Repository.
|
||||
func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
return &Repository{pool: pool}
|
||||
}
|
||||
|
||||
// GetByID returns a recipe with its ingredients and steps.
|
||||
// Text is resolved for the language stored in ctx (English fallback).
|
||||
// Returns nil, nil if not found.
|
||||
func (r *Repository) GetByID(ctx context.Context, id string) (*Recipe, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
|
||||
const q = `
|
||||
SELECT r.id, r.dish_id, r.source, r.difficulty,
|
||||
r.prep_time_min, r.cook_time_min, r.servings,
|
||||
r.calories_per_serving, r.protein_per_serving, r.fat_per_serving,
|
||||
r.carbs_per_serving, r.fiber_per_serving,
|
||||
rt.notes,
|
||||
r.created_at, r.updated_at
|
||||
FROM recipes r
|
||||
LEFT JOIN recipe_translations rt ON rt.recipe_id = r.id AND rt.lang = $2
|
||||
WHERE r.id = $1`
|
||||
|
||||
row := r.pool.QueryRow(ctx, q, id, lang)
|
||||
rec, err := scanRecipe(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get recipe %s: %w", id, err)
|
||||
}
|
||||
|
||||
if err := r.loadIngredients(ctx, rec, lang); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.loadSteps(ctx, rec, lang); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rec, nil
|
||||
}
|
||||
|
||||
// Count returns the total number of recipes.
|
||||
func (r *Repository) Count(ctx context.Context) (int, error) {
|
||||
var n int
|
||||
if err := r.pool.QueryRow(ctx, `SELECT count(*) FROM recipes`).Scan(&n); err != nil {
|
||||
return 0, fmt.Errorf("count recipes: %w", err)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// loadIngredients fills rec.Ingredients from recipe_ingredients.
|
||||
func (r *Repository) loadIngredients(ctx context.Context, rec *Recipe, lang string) error {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT ri.id, ri.ingredient_id,
|
||||
COALESCE(rit.name, ri.name) AS name,
|
||||
ri.amount, ri.unit_code, ri.is_optional, ri.sort_order
|
||||
FROM recipe_ingredients ri
|
||||
LEFT JOIN recipe_ingredient_translations rit
|
||||
ON rit.ri_id = ri.id AND rit.lang = $2
|
||||
WHERE ri.recipe_id = $1
|
||||
ORDER BY ri.sort_order`, rec.ID, lang)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load ingredients for recipe %s: %w", rec.ID, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var ing RecipeIngredient
|
||||
if err := rows.Scan(
|
||||
&ing.ID, &ing.IngredientID, &ing.Name,
|
||||
&ing.Amount, &ing.UnitCode, &ing.IsOptional, &ing.SortOrder,
|
||||
); err != nil {
|
||||
return fmt.Errorf("scan ingredient: %w", err)
|
||||
}
|
||||
rec.Ingredients = append(rec.Ingredients, ing)
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// loadSteps fills rec.Steps from recipe_steps.
|
||||
func (r *Repository) loadSteps(ctx context.Context, rec *Recipe, lang string) error {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT rs.id, rs.step_number,
|
||||
COALESCE(rst.description, rs.description) AS description,
|
||||
rs.timer_seconds, rs.image_url
|
||||
FROM recipe_steps rs
|
||||
LEFT JOIN recipe_step_translations rst
|
||||
ON rst.step_id = rs.id AND rst.lang = $2
|
||||
WHERE rs.recipe_id = $1
|
||||
ORDER BY rs.step_number`, rec.ID, lang)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load steps for recipe %s: %w", rec.ID, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var s RecipeStep
|
||||
if err := rows.Scan(
|
||||
&s.ID, &s.StepNumber, &s.Description, &s.TimerSeconds, &s.ImageURL,
|
||||
); err != nil {
|
||||
return fmt.Errorf("scan step: %w", err)
|
||||
}
|
||||
rec.Steps = append(rec.Steps, s)
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// --- scan helpers ---
|
||||
|
||||
func scanRecipe(row pgx.Row) (*Recipe, error) {
|
||||
var rec Recipe
|
||||
err := row.Scan(
|
||||
&rec.ID, &rec.DishID, &rec.Source, &rec.Difficulty,
|
||||
&rec.PrepTimeMin, &rec.CookTimeMin, &rec.Servings,
|
||||
&rec.CaloriesPerServing, &rec.ProteinPerServing, &rec.FatPerServing,
|
||||
&rec.CarbsPerServing, &rec.FiberPerServing,
|
||||
&rec.Notes,
|
||||
&rec.CreatedAt, &rec.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rec.Ingredients = []RecipeIngredient{}
|
||||
rec.Steps = []RecipeStep{}
|
||||
return &rec, nil
|
||||
}
|
||||
Reference in New Issue
Block a user