-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.go
98 lines (72 loc) · 2.05 KB
/
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
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
package main
import (
"database/sql"
"encoding/json"
"fmt"
)
type recipe struct {
ID int `json:"id"`
Name string `json:"name"`
Author string `json:"author"`
ImageUrl string `json:"image_url"`
SourceUrl string `json:"source_url"`
}
type recipeError struct {
Errors map[string]string
}
func getRecipes(db *sql.DB, start, count int) ([]recipe, error) {
rows, err := db.Query("SELECT id, name, author, image_url, source_url FROM recipes LIMIT $1 OFFSET $2", count, start)
if err != nil {
return nil, err
}
defer rows.Close()
recipes := []recipe{}
for rows.Next() {
var r recipe
if err := rows.Scan(&r.ID, &r.Name, &r.Author, &r.ImageUrl, &r.SourceUrl); err != nil {
return nil, err
}
recipes = append(recipes, r)
}
return recipes, nil
}
func (r *recipe) getRecipe(db *sql.DB) error {
return db.QueryRow("SELECT id, name, author, image_url, source_url FROM recipes WHERE id=$1", r.ID).Scan(&r.ID, &r.Name, &r.Author, &r.ImageUrl, &r.SourceUrl)
}
func (r *recipe) createRecipe(db *sql.DB) error {
re := &recipeError{Errors: map[string]string{}}
if r.Name == "" {
re.Errors["name"] = "name is required"
}
if len(re.Errors) > 0 {
return re
}
err := db.QueryRow(
"INSERT INTO recipes(name, author, image_url, source_url) VALUES($1, $2, $3, $4) RETURNING id", r.Name, r.Author, r.ImageUrl, r.SourceUrl).Scan(&r.ID)
if err != nil {
return err
}
return nil
}
func (r *recipe) updateRecipe(db *sql.DB) error {
_, err :=
db.Exec("UPDATE recipes SET name=$1, author=$2, image_url=$3, source_url=$4 WHERE id=$5",
r.Name, r.Author, r.ImageUrl, r.SourceUrl, r.ID)
return err
}
func (r *recipe) deleteRecipe(db *sql.DB) error {
_, err := db.Exec("DELETE FROM recipes WHERE id=$1", r.ID)
return err
}
func (re *recipeError) Error() string {
errStr, _ := json.Marshal(re.Errors)
return string(errStr)
}
func (re *recipeError) MarshalJSON() ([]byte, error) {
aux := re.Errors
jsonStr, err := json.Marshal(aux)
if err != nil {
return []byte{}, fmt.Errorf("decode recipeError: %v", err)
}
return jsonStr, nil
}