/
todo_list.go
205 lines (170 loc) · 5.11 KB
/
todo_list.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package handlers
import (
"time"
"github.com/gin-gonic/gin"
"github.com/kgantsov/todogo/pkg/models"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
)
func OptionsTodoList(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Methods", "DELETE,POST,PUT")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Auth-Token")
c.Next()
}
// Create godoc
// @Summary Create a TODO list
// @Schemes
// @Description Returns an newly created TODO list
// @Tags TODO list
// @Accept json
// @Produce json
// @Param body body models.TodoList true "TODO list"
// @Success 200 {object} models.TodoList
// @Failure 401 {object} ErrorSchema
// @Failure 500 {object} ErrorSchema
// @Security HttpBearer
// @Router /list/ [post]
func CreateTodoList(c *gin.Context) {
db, ok := c.MustGet("db").(gorm.DB)
if !ok {
c.JSON(500, gin.H{"error": "No connection to DB"})
return
}
var todoList models.TodoList
e := c.BindJSON(&todoList)
if e == nil {
currentUser := c.MustGet("CurrentUser").(models.User)
todoList.ID = uuid.NewV4()
todoList.UserID = currentUser.ID
db.Create(&todoList)
c.JSON(201, todoList)
} else {
c.JSON(422, gin.H{"error": e})
}
}
// Create godoc
// @Summary Get a TODO lists
// @Schemes
// @Description Returns TODO lists
// @Tags TODO list
// @Accept json
// @Produce json
// @Success 200 {object} []models.TodoList
// @Failure 401 {object} ErrorSchema
// @Failure 500 {object} ErrorSchema
// @Security HttpBearer
// @Router /list/ [get]
func GetTodoLists(c *gin.Context) {
db, ok := c.MustGet("db").(gorm.DB)
if !ok {
c.JSON(500, gin.H{"error": "No connection to DB"})
return
}
var todoLists []models.TodoList
currentUser := c.MustGet("CurrentUser").(models.User)
db.Order("created_at asc").Where("user_id = ?", currentUser.ID).Find(&todoLists)
c.JSON(200, todoLists)
}
// Create godoc
// @Summary Get a TODO list
// @Schemes
// @Description Returns a particular TODO list by its ID
// @Tags TODO list
// @Accept json
// @Produce json
// @Param listID path string true "ID of a TODO list"
// @Success 200 {object} models.TodoList
// @Failure 401 {object} ErrorSchema
// @Failure 500 {object} ErrorSchema
// @Security HttpBearer
// @Router /list/{listID}/ [get]
func GetTodoList(c *gin.Context) {
db, ok := c.MustGet("db").(gorm.DB)
if !ok {
c.JSON(500, gin.H{"error": "No connection to DB"})
return
}
listID := c.Params.ByName("listID")
var todoList models.TodoList
currentUser := c.MustGet("CurrentUser").(models.User)
db.Where("id = ? AND user_id = ?", listID, currentUser.ID).First(&todoList)
if todoList.ID != uuid.FromStringOrNil("00000000-0000-0000-0000-000000000000") {
c.JSON(200, todoList)
} else {
c.JSON(404, gin.H{"error": "TODO list not found"})
}
}
// Create godoc
// @Summary Update a TODO list
// @Schemes
// @Description Updates a particular TODO list by its ID
// @Tags TODO list
// @Accept json
// @Produce json
// @Param listID path string true "ID of a TODO list"
// @Param body body models.TodoList true "TODO list"
// @Success 200 {object} models.TodoList
// @Failure 401 {object} ErrorSchema
// @Failure 500 {object} ErrorSchema
// @Security HttpBearer
// @Router /list/{listID}/ [put]
func UpdateTodoList(c *gin.Context) {
db, ok := c.MustGet("db").(gorm.DB)
if !ok {
c.JSON(500, gin.H{"error": "No connection to DB"})
return
}
listID := c.Params.ByName("listID")
var newTodoList models.TodoList
e := c.BindJSON(&newTodoList)
if e != nil {
c.JSON(422, e)
return
}
var todoList models.TodoList
currentUser := c.MustGet("CurrentUser").(models.User)
db.Where("id = ? AND user_id = ?", listID, currentUser.ID).First(&todoList)
if todoList.ID != uuid.FromStringOrNil("00000000-0000-0000-0000-000000000000") {
updatedAt := time.Now()
todoList = models.TodoList{
ID: todoList.ID,
Title: newTodoList.Title,
CreatedAt: todoList.CreatedAt,
UpdatedAt: &updatedAt,
}
db.Save(&todoList)
c.JSON(200, todoList)
} else {
c.JSON(404, gin.H{"error": "Todo List not found"})
}
}
// Create godoc
// @Summary Delte a TODO list
// @Schemes
// @Description Deletes a particular TODO list by its ID
// @Tags TODO list
// @Accept json
// @Produce json
// @Param listID path string true "ID of a TODO list"
// @Success 200 {object} models.TodoList
// @Failure 401 {object} ErrorSchema
// @Failure 500 {object} ErrorSchema
// @Security HttpBearer
// @Router /list/{listID}/ [delete]
func DeleteTodoList(c *gin.Context) {
db, ok := c.MustGet("db").(gorm.DB)
if !ok {
c.JSON(500, gin.H{"error": "No connection to DB"})
return
}
listID := c.Params.ByName("listID")
var todoList models.TodoList
currentUser := c.MustGet("CurrentUser").(models.User)
db.Where("id = ? AND user_id = ?", listID, currentUser.ID).First(&todoList)
if todoList.ID != uuid.FromStringOrNil("00000000-0000-0000-0000-000000000000") {
db.Delete(&todoList)
c.Writer.WriteHeader(204)
} else {
c.JSON(404, gin.H{"error": "Todo List not found"})
}
}