-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_recipe.go
63 lines (52 loc) · 1.76 KB
/
create_recipe.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
package recipes
import (
"fmt"
"net/http"
. "github.com/iplay88keys/my-recipe-library/pkg/helpers"
"github.com/iplay88keys/my-recipe-library/pkg/api"
"github.com/iplay88keys/my-recipe-library/pkg/repositories"
)
type CreateRecipeResponse struct {
RecipeID int64 `json:"recipe_id,omitempty"`
Errors map[string]string `json:"errors,omitempty"`
}
type createRecipe func(recipe *repositories.Recipe, userID int64) (int64, error)
func CreateRecipe(createRecipe createRecipe) *api.Endpoint {
return &api.Endpoint{
Path: "recipes",
Method: http.MethodPost,
Auth: true,
Handle: func(r *api.Request) *api.Response {
var recipe CreateRecipeRequest
if err := r.Decode(&recipe); err != nil {
fmt.Printf("Error decoding json body for add recipe: %s\n", err.Error())
return api.NewResponse(http.StatusBadRequest, nil)
}
validationErrors := recipe.Validate()
if len(validationErrors) > 0 {
resp := &CreateRecipeResponse{
Errors: validationErrors,
}
return api.NewResponse(http.StatusBadRequest, resp)
}
recipeID, err := createRecipe(&repositories.Recipe{
Name: StringPointer(recipe.Name),
Description: StringPointer(recipe.Description),
Servings: IntPointer(recipe.Servings),
PrepTime: StringPointer(recipe.PrepTime),
CookTime: StringPointer(recipe.CookTime),
CoolTime: StringPointer(recipe.CoolTime),
TotalTime: StringPointer(recipe.TotalTime),
Source: StringPointer(recipe.Source),
}, r.UserID)
if err != nil {
fmt.Printf("Error adding recipe: %s\n", err.Error())
return api.NewResponse(http.StatusInternalServerError, nil)
}
resp := &CreateRecipeResponse{
RecipeID: recipeID,
}
return api.NewResponse(http.StatusCreated, resp)
},
}
}