This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caption.go
102 lines (82 loc) · 2.85 KB
/
caption.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
package pkg
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
)
func (a *API) getAllCaptions(w http.ResponseWriter, r *http.Request) {
var captions []Caption
if err := a.Options.DB.Find(&captions).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
respond(w, jsonResponse(http.StatusNotFound, "Unable to retrieve all captions"))
return
}
respond(w, jsonResponse(http.StatusInternalServerError, "Database connection error"))
return
}
respond(w, captions)
}
func (a *API) getCaption(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
captionID := params["captionId"]
var caption Caption
if err := a.Options.DB.Table("captions").Where("id = ?", captionID).First(&caption).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
respond(w, jsonResponse(http.StatusNotFound, "Caption not found"))
return
}
respond(w, jsonResponse(http.StatusInternalServerError, "Database connection error"))
return
}
respond(w, caption)
}
// TODO add validation
func (a *API) createCaption(w http.ResponseWriter, r *http.Request) {
var caption Caption
if err := json.NewDecoder(r.Body).Decode(&caption); err != nil {
respond(w, jsonResponse(http.StatusBadRequest, "Bad request"))
return
}
if err := a.Options.DB.Create(&caption).Error; err != nil {
respond(w, jsonResponse(http.StatusInternalServerError, "Unable to create caption"))
return
}
respond(w, caption)
}
func (a *API) updateCaption(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
captionID := params["captionId"]
var caption Caption
if err := json.NewDecoder(r.Body).Decode(&caption); err != nil {
respond(w, jsonResponse(http.StatusBadRequest, "Bad request"))
return
}
if err := a.Options.DB.Table("captions").Where("id = ?", captionID).First(&caption).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
respond(w, jsonResponse(http.StatusNotFound, "Caption not found"))
return
}
respond(w, jsonResponse(http.StatusInternalServerError, "Database connection error"))
return
}
if err := a.Options.DB.Save(&caption).Error; err != nil {
respond(w, jsonResponse(http.StatusInternalServerError, "Unable to update caption"))
return
}
respond(w, jsonResponse(http.StatusOK, fmt.Sprintf("Caption %v successfully updated", captionID)))
}
func (a *API) deleteCaption(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
captionID := params["captionId"]
var caption Caption
if err := a.Options.DB.Table("captions").Where("id = ?", captionID).Delete(&caption).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
respond(w, jsonResponse(http.StatusNotFound, "Image not found"))
return
}
respond(w, jsonResponse(http.StatusInternalServerError, "Database connection error"))
return
}
respond(w, jsonResponse(http.StatusOK, fmt.Sprintf("Image %v successfully deleted", captionID)))
}