-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecipeRepository.go
242 lines (185 loc) · 5.2 KB
/
RecipeRepository.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package repositories
import (
"errors"
m "github.com/ihulsbus/cookbook/internal/models"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
const (
whereRecipeID = "recipe_id = ?"
)
type RecipeRepository struct {
db *gorm.DB
}
func NewRecipeRepository(db *gorm.DB) *RecipeRepository {
return &RecipeRepository{
db: db,
}
}
// FindAll retrieves all recipes from the database and returns them in a slice
func (r RecipeRepository) FindAll() ([]m.Recipe, error) {
var recipes []m.Recipe
if err := r.db.Preload(clause.Associations).Find(&recipes).Error; err != nil {
return nil, err
}
if len(recipes) <= 0 {
return nil, errors.New("not found")
}
return recipes, nil
}
// Find searches for a specific recipe in the database and returns it when found.
func (r RecipeRepository) FindSingle(recipeID uint) (m.Recipe, error) {
var recipe m.Recipe
recipe.ID = recipeID
result := r.db.Preload(clause.Associations).Where(whereID, recipeID).First(&recipe)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return m.Recipe{}, errors.New("not found")
} else {
return m.Recipe{}, result.Error
}
}
return recipe, nil
}
// Create handles the creation of a recipe and stores the relevant information in the database
func (r RecipeRepository) Create(recipe m.Recipe) (m.Recipe, error) {
if err := r.db.Transaction(func(tx *gorm.DB) error {
var err error
if err = tx.Create(&recipe).Error; err != nil {
return err
}
return nil
}); err != nil {
return recipe, err
}
return recipe, nil
}
func (r RecipeRepository) Update(recipe m.Recipe) (m.Recipe, error) {
if err := r.db.Transaction(func(tx *gorm.DB) error {
var err error
// update the recipe while skipping associations as this is very very
if err = tx.Omit("author_id", "image_name").Updates(&recipe).Error; err != nil {
return err
}
return nil
}); err != nil {
return recipe, err
}
return recipe, nil
}
func (r RecipeRepository) Delete(recipe m.Recipe) error {
if err := r.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Delete(&recipe).Error; err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
func (r RecipeRepository) FindInstruction(recipeID uint) (m.Instruction, error) {
var instruction m.Instruction
instruction.RecipeID = recipeID
result := r.db.Where(whereRecipeID, recipeID).First(&instruction)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return m.Instruction{}, errors.New("not found")
} else {
return m.Instruction{}, result.Error
}
}
return instruction, nil
}
func (r RecipeRepository) CreateInstruction(instruction m.Instruction) (m.Instruction, error) {
if err := r.db.Transaction(func(tx *gorm.DB) error {
var err error
if err = tx.Create(&instruction).Error; err != nil {
return err
}
return nil
}); err != nil {
return instruction, err
}
return instruction, nil
}
func (r RecipeRepository) UpdateInstruction(instruction m.Instruction) (m.Instruction, error) {
if err := r.db.Transaction(func(tx *gorm.DB) error {
var err error
if err = tx.Where(whereRecipeID, &instruction.RecipeID).Updates(&instruction).Error; err != nil {
return err
}
return nil
}); err != nil {
return instruction, err
}
return instruction, nil
}
func (r RecipeRepository) DeleteInstruction(instruction m.Instruction) error {
if err := r.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Where(whereRecipeID, &instruction.RecipeID).Delete(&instruction).Error; err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// FindRecipeIngredients finds all ingredients associated to a recipe and returns them in a slice
func (r RecipeRepository) FindIngredientLink(recipeID uint) ([]m.RecipeIngredient, error) {
var recipeIngredients []m.RecipeIngredient
if err := r.db.Where(whereRecipeID, &recipeID).Joins("Unit").Find(&recipeIngredients).Error; err != nil {
return nil, err
}
if len(recipeIngredients) <= 0 {
return nil, errors.New("not found")
}
return recipeIngredients, nil
}
func (r RecipeRepository) CreateIngredientLink(link []m.RecipeIngredient) ([]m.RecipeIngredient, error) {
if err := r.db.Transaction(func(tx *gorm.DB) error {
var err error
for i := range link {
if err = tx.Create(&link[i]).Error; err != nil {
return err
}
}
return nil
}); err != nil {
return nil, err
}
return link, nil
}
func (r RecipeRepository) UpdateIngredientLink(link []m.RecipeIngredient) ([]m.RecipeIngredient, error) {
if err := r.db.Transaction(func(tx *gorm.DB) error {
for i := range link {
result := tx.Where(whereRecipeID, &link[i].RecipeID).Updates(&link[i])
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
if err := tx.Create(&link[i]).Error; err != nil {
return err
}
}
}
return nil
}); err != nil {
return link, err
}
return link, nil
}
func (r RecipeRepository) DeleteIngredientLink(link []m.RecipeIngredient) error {
if err := r.db.Transaction(func(tx *gorm.DB) error {
for i := range link {
if err := tx.Where(whereRecipeID, &link[i].RecipeID).Delete(&link[i]).Error; err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}