-
Notifications
You must be signed in to change notification settings - Fork 2
/
images.go
117 lines (98 loc) · 3 KB
/
images.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
package api
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/chadweimer/gomp/models"
"github.com/julienschmidt/httprouter"
)
func (h apiHandler) getRecipeImages(resp http.ResponseWriter, req *http.Request, p httprouter.Params) {
recipeID, err := strconv.ParseInt(p.ByName("recipeID"), 10, 64)
if err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
images, err := h.model.Images.List(recipeID)
if err != nil {
h.JSON(resp, http.StatusInternalServerError, err.Error())
return
}
h.JSON(resp, http.StatusOK, images)
}
func (h apiHandler) getRecipeMainImage(resp http.ResponseWriter, req *http.Request, p httprouter.Params) {
recipeID, err := strconv.ParseInt(p.ByName("recipeID"), 10, 64)
if err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
image, err := h.model.Images.ReadMainImage(recipeID)
if err == models.ErrNotFound {
h.JSON(resp, http.StatusNotFound, err.Error())
return
}
if err != nil {
h.JSON(resp, http.StatusInternalServerError, err.Error())
return
}
h.JSON(resp, http.StatusOK, image)
}
func (h apiHandler) putRecipeMainImage(resp http.ResponseWriter, req *http.Request, p httprouter.Params) {
recipeID, err := strconv.ParseInt(p.ByName("recipeID"), 10, 64)
if err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
var imageID int64
if err := readJSONFromRequest(req, &imageID); err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
image := models.RecipeImage{ID: imageID, RecipeID: recipeID}
if err := h.model.Images.UpdateMainImage(&image); err != nil {
h.JSON(resp, http.StatusInternalServerError, err.Error())
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (h apiHandler) postImage(resp http.ResponseWriter, req *http.Request, p httprouter.Params) {
recipeID, err := strconv.ParseInt(p.ByName("recipeID"), 10, 64)
if err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
file, fileHeader, err := req.FormFile("file_content")
if err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
defer file.Close()
uploadedFileData, err := ioutil.ReadAll(file)
if err != nil {
h.JSON(resp, http.StatusInternalServerError, err.Error())
return
}
imageInfo := &models.RecipeImage{
RecipeID: recipeID,
Name: fileHeader.Filename,
}
err = h.model.Images.Create(imageInfo, uploadedFileData)
if err != nil {
h.JSON(resp, http.StatusInternalServerError, err.Error())
return
}
resp.Header().Set("Location", fmt.Sprintf("/api/v1/recipes/%d/images/%d", imageInfo.RecipeID, imageInfo.ID))
resp.WriteHeader(http.StatusCreated)
}
func (h apiHandler) deleteImage(resp http.ResponseWriter, req *http.Request, p httprouter.Params) {
imageID, err := strconv.ParseInt(p.ByName("imageID"), 10, 64)
if err != nil {
h.JSON(resp, http.StatusBadRequest, err.Error())
return
}
if err := h.model.Images.Delete(imageID); err != nil {
h.JSON(resp, http.StatusInternalServerError, err.Error())
return
}
resp.WriteHeader(http.StatusOK)
}