/
controller.go
203 lines (175 loc) · 5.11 KB
/
controller.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
package web
import (
"github.com/Sfeir/golang-200/dao"
"github.com/Sfeir/golang-200/model"
logger "github.com/sirupsen/logrus"
"net/http"
"strconv"
)
const (
prefix = "/tasks"
)
// TaskController is a controller for tasks resource
type TaskController struct {
taskDao dao.TaskDAO
Routes []Route
Prefix string
}
// NewTaskController creates a new task controller to manage tasks
func NewTaskController(taskDAO dao.TaskDAO) *TaskController {
controller := TaskController{
taskDao: taskDAO,
Prefix: prefix,
}
// build routes
routes := []Route{}
// GetAll
routes = append(routes, Route{
Name: "Get all tasks",
Method: http.MethodGet,
Pattern: "",
HandlerFunc: controller.GetAll,
})
// Get
routes = append(routes, Route{
Name: "Get one task",
Method: http.MethodGet,
Pattern: "/{id}",
HandlerFunc: controller.Get,
})
// Create
routes = append(routes, Route{
Name: "Create a task",
Method: http.MethodPost,
Pattern: "",
HandlerFunc: controller.Create,
})
// Update
routes = append(routes, Route{
Name: "Update a task",
Method: http.MethodPut,
Pattern: "/{id}",
HandlerFunc: controller.Update,
})
// Delete
routes = append(routes, Route{
Name: "Delete a task",
Method: http.MethodDelete,
Pattern: "/{id}",
HandlerFunc: controller.Delete,
})
controller.Routes = routes
return &controller
}
// GetAll retrieve all entities with optional paging of items (start / end are item counts 50 to 100 for example)
func (sh *TaskController) GetAll(w http.ResponseWriter, r *http.Request) {
startStr := QueryParamAsString("start", r)
endStr := QueryParamAsString("end", r)
start := dao.NoPaging
end := dao.NoPaging
var err error
if startStr != "" && endStr != "" {
start, err = strconv.Atoi(startStr)
if err != nil {
start = dao.NoPaging
}
end, err = strconv.Atoi(endStr)
if err != nil {
end = dao.NoPaging
}
}
// find all tasks
tasks, err := sh.taskDao.GetAll(start, end)
if err != nil {
if err == dao.ErrNotFound {
logger.WithField("error", err).
WithField("start", start).
WithField("end", end).Warn("unable to retrieve all tasks")
SendJSONNotFound(w)
return
}
logger.WithField("error", err).Warn("unable to retrieve tasks")
SendJSONError(w, "Error while retrieving tasks", http.StatusInternalServerError)
return
}
logger.WithField("tasks", tasks).Debug("tasks found")
SendJSONOk(w, tasks)
}
// Get retrieve an entity by id
func (sh *TaskController) Get(w http.ResponseWriter, r *http.Request) {
// get the task's ID from the URL
taskID := URLParamAsString("id", r)
// find the task
task, err := sh.taskDao.GetByID(taskID)
if err != nil {
if err == dao.ErrNotFound {
logger.WithField("error", err).WithField("task ID", taskID).Warn("unable to retrieve task by ID")
SendJSONNotFound(w)
return
}
logger.WithField("error", err).WithField("task ID", taskID).Warn("unable to retrieve task by ID")
SendJSONError(w, "Error while retrieving task by ID", http.StatusInternalServerError)
return
}
logger.WithField("tasks", task).Debug("task found")
SendJSONOk(w, task)
}
// Create create an entity
func (sh *TaskController) Create(w http.ResponseWriter, r *http.Request) {
// task to be created
task := &model.Task{}
// get the content body
err := GetJSONContent(task, r)
if err != nil {
logger.WithField("error", err).Warn("unable to decode task to create")
SendJSONError(w, "Error while decoding task to create", http.StatusBadRequest)
return
}
// save task
err = sh.taskDao.Save(task)
if err != nil {
logger.WithField("error", err).WithField("task", *task).Warn("unable to create task")
SendJSONError(w, "Error while creating task", http.StatusInternalServerError)
return
}
// send response
SendJSONWithHTTPCode(w, task, http.StatusCreated)
}
// Update update an entity by id
func (sh *TaskController) Update(w http.ResponseWriter, r *http.Request) {
// get the task ID from the URL
taskID := URLParamAsString("id", r)
// task to be created
task := &model.Task{}
// get the content body
err := GetJSONContent(task, r)
if err != nil {
logger.WithField("error", err).Warn("unable to decode task to update")
SendJSONError(w, "Error while decoding task to create", http.StatusBadRequest)
return
}
// save task
task.ID = taskID
_, err = sh.taskDao.Upsert(task)
if err != nil {
logger.WithField("error", err).WithField("task", *task).Warn("unable to create task")
SendJSONError(w, "Error while updating task", http.StatusInternalServerError)
return
}
// send response
SendJSONOk(w, task)
}
// Delete delete an entity by id
func (sh *TaskController) Delete(w http.ResponseWriter, r *http.Request) {
// get the task ID from the URL
taskID := URLParamAsString("id", r)
// find task
err := sh.taskDao.Delete(taskID)
if err != nil {
logger.WithField("error", err).WithField("task ID", taskID).Warn("unable to delete task by ID")
SendJSONError(w, "Error while deleting task by ID", http.StatusInternalServerError)
return
}
logger.WithField("taskID", taskID).Debug("task deleted")
SendJSONWithHTTPCode(w, nil, http.StatusNoContent)
}