Hayden Hargreaves ced9da5bf2 (DB/FEAT): Recipe tags have been implemented!
This is part of the step towards finishing the creation wizard, all that
is left is the image. However, that is a bigger problem since it
requires a file store and file server. But for now, tags are implemented
and working!
2025-07-12 12:50:26 -07:00

102 lines
2.8 KiB
Go

package domain
import "time"
// RecipeDuration is the duration to prepare recipe. It has JSON tags which allows it to be
// marshaled into a JSON object and stored in the database (JSONB).
type RecipeDuration struct {
Total int `json:"total"`
Prep int `json:"prep"`
Cook int `json:"cook"`
}
// RecipeMeal is the database enum E_MEAL which defines the meal type of a recipe. Postgres enums
// are case sensitive so these must match the values in the database exactly.
type RecipeMeal string
const (
MealBreakfast RecipeMeal = "breakfast"
MealLunch RecipeMeal = "lunch"
MealDinner RecipeMeal = "dinner"
MealDesert RecipeMeal = "dessert"
MealSnack RecipeMeal = "snack"
MealSide RecipeMeal = "side"
MealOther RecipeMeal = "other"
)
// ParseMeal converts an integer value into a meal type (string). Values are 0-indexed.
func ParseMeal(meal int) RecipeMeal {
switch meal {
case 0:
return MealBreakfast
case 1:
return MealLunch
case 2:
return MealDinner
case 3:
return MealDesert
case 4:
return MealSnack
case 5:
return MealSide
case 6:
return MealOther
default:
return MealOther
}
}
// RecipeIngredient is a single ingredients in a recipe. These have JSON tags which allow them
// to be marshaled into a JSON array and stored in the database (JSONB).
type RecipeIngredient struct {
Name string `json:"name"`
Quantity string `json:"quantity"`
}
// Recipe is the database model of a recipe. There is no need to map to a different model so
// this will remain in the domain. The Tags field should be loaded from the external Tags table,
// but is still attached to this domain object.
type Recipe struct {
Id int
Title string
Description string
Instructions []string
Serves int
Difficulty int
Duration RecipeDuration
Category RecipeMeal
Ingredients []RecipeIngredient // Just a list of ingredients
UserId int
Modified *time.Time // Pointer to allow null
Created time.Time
Tags []Tag
}
// SearchFilters is a model which represents the required filters to complete a recipe search.
// The integer values should be provided as bits and used to parse out individual flags. More
// details can be found in the SearchRecipes service function.
type SearchFilters struct {
Search string
MealType int
Time int
Difficulty int
ServingSize int
}
// Tag is a model which represents a single tag in the Tags table. A tag is mapped to a recipe
// using the RecipeTag data model and can be accessed via their ID from the DB.
type Tag struct {
Id int
Name string
Created time.Time
}
// RecipeTag is a model which represents a single mapping in the RecipeTags table. This model
// is a many-to-many mapping for tags to recipes.
type RecipeTag struct {
Id int
RecipeId int
TagId int
Created time.Time
}