1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
"strconv" |
6
|
|
|
|
7
|
|
|
"github.com/gin-gonic/gin" |
8
|
|
|
|
9
|
|
|
"github.com/rfinochi/golang-workshop-todo/pkg/common" |
10
|
|
|
"github.com/rfinochi/golang-workshop-todo/pkg/models" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
// getItemsEndpoint godoc |
14
|
|
|
// @Summary Get all to-do items |
15
|
|
|
// @Description Get all to-do items from the data store |
16
|
|
|
// @Produce json |
17
|
|
|
// @Param X-Todo-API-Authorization-Token header string true "Authorization Token" |
18
|
|
|
// @Success 200 {array} models.Item |
19
|
|
|
// @Header 200 {string} X-Todo-API-Revision "API Revision Number" |
20
|
|
|
// @Header 200 {string} X-Todo-API-Request-ID "API Request Id" |
21
|
|
|
// @Router / [get] |
22
|
|
|
func (app *application) getItemsEndpoint(c *gin.Context) { |
23
|
|
|
items, err := app.itemModel.GetItems() |
24
|
|
|
if err != nil { |
25
|
|
|
app.serverError(c.Writer, err) |
26
|
|
|
} else { |
27
|
|
|
common.RespondOkWithData(c, items) |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// getItemEndpoint godoc |
32
|
|
|
// @Summary Get a to-do item |
33
|
|
|
// @Description Get a to-do item by id from the data store |
34
|
|
|
// @Produce json |
35
|
|
|
// @Param id path int true "To-Do Item Id" |
36
|
|
|
// @Param X-Todo-API-Authorization-Token header string true "Authorization Token" |
37
|
|
|
// @Success 200 {object} models.Item |
38
|
|
|
// @Header 200 {string} X-Todo-API-Revision "API Revision Number" |
39
|
|
|
// @Header 200 {string} X-Todo-API-Request-ID "API Request Id" |
40
|
|
|
// @Router /{id} [get] |
41
|
|
|
func (app *application) getItemEndpoint(c *gin.Context) { |
42
|
|
|
id, err := strconv.Atoi(c.Param("id")) |
43
|
|
|
if err != nil || id < 1 { |
44
|
|
|
app.notFound(c.Writer) |
45
|
|
|
return |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
item, err := app.itemModel.GetItem(id) |
49
|
|
|
if err == models.ErrNoRecord { |
50
|
|
|
app.notFound(c.Writer) |
51
|
|
|
return |
52
|
|
|
} else if err != nil { |
53
|
|
|
app.serverError(c.Writer, err) |
54
|
|
|
return |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
common.RespondOkWithData(c, item) |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// postItemEndpoint godoc |
61
|
|
|
// @Summary Create a to-do item |
62
|
|
|
// @Description Insert a to-do item into the data store |
63
|
|
|
// @Accept json |
64
|
|
|
// @Produce json |
65
|
|
|
// @Param item body models.Item true "To-Do Item" |
66
|
|
|
// @Param X-Todo-API-Authorization-Token header string true "Authorization Token" |
67
|
|
|
// @Success 201 {string} string "{\"message\": \"Ok\"}" |
68
|
|
|
// @Header 201 {string} X-Todo-API-Revision "API Revision Number" |
69
|
|
|
// @Header 201 {string} X-Todo-API-Request-ID "API Request Id" |
70
|
|
|
// @Router / [post] |
71
|
|
|
func (app *application) postItemEndpoint(c *gin.Context) { |
72
|
|
|
var newItem models.Item |
73
|
|
|
err := c.BindJSON(&newItem) |
74
|
|
|
if err != nil { |
75
|
|
|
app.clientError(c.Writer, http.StatusBadRequest) |
76
|
|
|
return |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
err = app.itemModel.CreateItem(newItem) |
80
|
|
|
if err == models.ErrRecordExist { |
81
|
|
|
app.conflict(c.Writer, models.ErrRecordExist.Error()) |
82
|
|
|
return |
83
|
|
|
} else if err != nil { |
84
|
|
|
app.serverError(c.Writer, err) |
85
|
|
|
return |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
common.RespondCreated(c) |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// putItemEndpoint godoc |
92
|
|
|
// @Summary Create a to-do item |
93
|
|
|
// @Description Insert a to-do item into the data store |
94
|
|
|
// @Accept json |
95
|
|
|
// @Produce json |
96
|
|
|
// @Param item body models.Item true "To-Do Item" |
97
|
|
|
// @Param X-Todo-API-Authorization-Token header string true "Authorization Token" |
98
|
|
|
// @Success 201 {string} string "{\"message\": \"Ok\"}" |
99
|
|
|
// @Header 201 {string} X-Todo-API-Revision "API Revision Number" |
100
|
|
|
// @Header 201 {string} X-Todo-API-Request-ID "API Request Id" |
101
|
|
|
// @Router / [put] |
102
|
|
|
func (app *application) putItemEndpoint(c *gin.Context) { |
103
|
|
|
var newItem models.Item |
104
|
|
|
err := c.BindJSON(&newItem) |
105
|
|
|
if err != nil { |
106
|
|
|
app.clientError(c.Writer, http.StatusBadRequest) |
107
|
|
|
return |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
err = app.itemModel.CreateItem(newItem) |
111
|
|
|
if err == models.ErrRecordExist { |
112
|
|
|
app.conflict(c.Writer, models.ErrRecordExist.Error()) |
113
|
|
|
return |
114
|
|
|
} else if err != nil { |
115
|
|
|
app.serverError(c.Writer, err) |
116
|
|
|
return |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
common.RespondCreated(c) |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// updateItemEndpoint godoc |
123
|
|
|
// @Summary Update a to-do item |
124
|
|
|
// @Description Update a to-do item into the data store |
125
|
|
|
// @Accept json |
126
|
|
|
// @Produce json |
127
|
|
|
// @Param id path int true "To-Do Item Id" |
128
|
|
|
// @Param item body models.Item true "To-Do Item" |
129
|
|
|
// @Param X-Todo-API-Authorization-Token header string true "Authorization Token" |
130
|
|
|
// @Success 200 {string} string "{\"message\": \"Ok\"}" |
131
|
|
|
// @Header 200 {string} X-Todo-API-Revision "API Revision Number" |
132
|
|
|
// @Header 200 {string} X-Todo-API-Request-ID "API Request Id" |
133
|
|
|
// @Router /{id} [patch] |
134
|
|
|
func (app *application) updateItemEndpoint(c *gin.Context) { |
135
|
|
|
id, err := strconv.Atoi(c.Param("id")) |
136
|
|
|
if err != nil || id < 1 { |
137
|
|
|
app.notFound(c.Writer) |
138
|
|
|
return |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
var updatedItem models.Item |
142
|
|
|
err = c.BindJSON(&updatedItem) |
143
|
|
|
if err != nil { |
144
|
|
|
app.clientError(c.Writer, http.StatusBadRequest) |
145
|
|
|
return |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
updatedItem.ID = id |
149
|
|
|
err = app.itemModel.UpdateItem(updatedItem) |
150
|
|
|
if err == models.ErrNoRecord { |
151
|
|
|
app.notFound(c.Writer) |
152
|
|
|
return |
153
|
|
|
} else if err != nil { |
154
|
|
|
app.serverError(c.Writer, err) |
155
|
|
|
return |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
common.RespondOk(c) |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// deleteItemEndpoint godoc |
162
|
|
|
// @Summary Delete a to-do item |
163
|
|
|
// @Description Delete a to-do item from the data store |
164
|
|
|
// @Produce json |
165
|
|
|
// @Param id path int true "To-Do Item Id" |
166
|
|
|
// @Param X-Todo-API-Authorization-Token header string true "Authorization Token" |
167
|
|
|
// @Success 200 {string} string "{\"message\": \"Ok\"}" |
168
|
|
|
// @Header 200 {string} X-Todo-API-Revision "API Revision Number" |
169
|
|
|
// @Header 200 {string} X-Todo-API-Request-ID "API Request Id" |
170
|
|
|
// @Router /{id} [delete] |
171
|
|
|
func (app *application) deleteItemEndpoint(c *gin.Context) { |
172
|
|
|
id, err := strconv.Atoi(c.Param("id")) |
173
|
|
|
if err != nil || id < 1 { |
174
|
|
|
app.notFound(c.Writer) |
175
|
|
|
return |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
err = app.itemModel.DeleteItem(id) |
179
|
|
|
if err == models.ErrNoRecord { |
180
|
|
|
app.notFound(c.Writer) |
181
|
|
|
return |
182
|
|
|
} else if err != nil { |
183
|
|
|
app.serverError(c.Writer, err) |
184
|
|
|
return |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
common.RespondOk(c) |
188
|
|
|
} |
189
|
|
|
|