-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipes_model.go
187 lines (164 loc) · 5.24 KB
/
recipes_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* MIT License
*
* Copyright (c) 2020 Beate Ottenwälder
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package recipes
import (
"encoding/json"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
// Ingredients of a recipe
type Ingredients struct {
//Name of the ingredient
Name string `json:"name"`
//Amount needed in a recipe of an ingredient
Amount float64 `json:"amount"`
//Unit of the Amount
Unit string `json:"unit"`
}
const (
//NoAmountIngredient is the amount value for ingredients when this field is not used
NoAmountIngredient = -1.0
)
// RecipeID is a data type that provides a unique id for each recipe
type RecipeID string
// String converts a RecipeID to string
func (r RecipeID) String() string {
return string(r)
}
// InvalidRecipeID should not be used for any valid Recipe
func InvalidRecipeID() RecipeID {
return RecipeID(uuid.Nil.String())
}
// NewRecipeID returns a random recipe id
func NewRecipeID() RecipeID {
return RecipeID(uuid.New().String())
}
// NewRecipeIDFromString converts a string to a recipe id and returns this recipe id.
// Returns the InvalidRecipeID iff the recipe id cannot be converted
func NewRecipeIDFromString(recipeID string) (result RecipeID) {
if tmp, err := uuid.Parse(recipeID); err != nil {
result = InvalidRecipeID()
} else {
result = RecipeID(tmp.String())
}
return
}
// Recipe model
type Recipe struct {
ID RecipeID `json:"id"`
Name string `json:"name"`
Ingredients []Ingredients `json:"components"`
Description string `json:"description"`
PictureLink []string `json:"pictureLink"`
Servings int8 `json:"servings"`
}
// RecipePicture model
type RecipePicture struct {
ID RecipeID `json:"id"`
Name string `json:"name"`
Picture string `json:"picture"`
}
// RecipeList models a list of recipes by ID
type RecipeList struct {
Recipes []string `json:"recipes"`
}
// NewInvalidRecipePicture returns an invalid picture
func NewInvalidRecipePicture() *RecipePicture {
return &RecipePicture{
ID: InvalidRecipeID(),
Name: "",
Picture: "",
}
}
// RecipeSearchFilter models a search query to filter recipes
type RecipeSearchFilter struct {
Name string `json:"name"`
Ingredient []string `json:"ingredients"`
Description string `json:"description"`
}
// Recipes interface is an abstraction for the provider of a collection of recipes, i.e., a data-base or a cache
type Recipes interface {
List() []*Recipe
IDs(filterQuery *RecipeSearchFilter) RecipeList
Num() int64
Get(id RecipeID) *Recipe
GetByName(name string) (*Recipe, error)
Picture(id RecipeID, name string) *RecipePicture
Pictures(id RecipeID) map[string]*RecipePicture
Random() *Recipe
Insert(recipe *Recipe) error
Update(id RecipeID, recipe *Recipe) error
AddPicture(pic *RecipePicture) error
Remove(id RecipeID) error
RemoveByName(name string) error
}
const (
//NotSupportedError informs clients about operations that are not supported by a Recipes provider
NotSupportedError = "recipe operation not supported"
)
// NewInvalidRecipe returns an empty Recipe object. The ID of the returned Recipe is InvalidRecipeID.
func NewInvalidRecipe() *Recipe {
return &Recipe{
ID: InvalidRecipeID(),
Name: "No Recipe",
}
}
// NewRecipe creates a new Recipe with a given id
func NewRecipe(id RecipeID) *Recipe {
return &Recipe{
ID: id,
Name: "",
Ingredients: make([]Ingredients, 0),
Description: "",
PictureLink: make([]string, 0),
Servings: 1,
}
}
// JSON returns the encoded version of the recipe. If an error occurs, '{}' is returned.
func (r *Recipe) JSON() []byte {
bytes, err := json.Marshal(r)
if err != nil {
log.WithError(err).Error("Could not convert recipe to bytes!")
return []byte("{}")
}
return bytes
}
// String (JSON) representation of the recipe
func (r *Recipe) String() string {
return string(r.JSON())
}
// ScaleBy a factor (of servings) all ingredients of the recipe
func (r *Recipe) ScaleBy(factor float64) {
for i := range r.Ingredients {
if r.Ingredients[i].Amount > 0 {
r.Ingredients[i].Amount *= factor
}
}
}
// ScaleTo a desired number of servings
func (r *Recipe) ScaleTo(servings int8) {
factor := float64(servings) / float64(r.Servings)
r.Servings = servings
r.ScaleBy(factor)
}