forked from shijuvar/go-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskController.go
208 lines (194 loc) · 4.53 KB
/
taskController.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
206
207
208
package controllers
import (
"encoding/json"
"net/http"
httpcontext "github.com/gorilla/context"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/shijuvar/go-web/taskmanager/common"
"github.com/shijuvar/go-web/taskmanager/data"
)
// CreateTask insert a new Task document
// Handler for HTTP Post - "/tasks
func CreateTask(w http.ResponseWriter, r *http.Request) {
var dataResource TaskResource
// Decode the incoming Task json
err := json.NewDecoder(r.Body).Decode(&dataResource)
if err != nil {
common.DisplayAppError(
w,
err,
"Invalid Task data",
500,
)
return
}
task := &dataResource.Data
context := NewContext()
defer context.Close()
if val, ok := httpcontext.GetOk(r, "user"); ok {
context.User = val.(string)
}
task.CreatedBy = context.User
col := context.DbCollection("tasks")
repo := &data.TaskRepository{C: col}
// Insert a task document
repo.Create(task)
j, err := json.Marshal(TaskResource{Data: *task})
if err != nil {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(j)
}
// GetTasks returns all Task document
// Handler for HTTP Get - "/tasks"
func GetTasks(w http.ResponseWriter, r *http.Request) {
context := NewContext()
defer context.Close()
col := context.DbCollection("tasks")
repo := &data.TaskRepository{C: col}
tasks := repo.GetAll()
j, err := json.Marshal(TasksResource{Data: tasks})
if err != nil {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
// GetTaskByID returns a single Task document by id
// Handler for HTTP Get - "/tasks/{id}"
func GetTaskByID(w http.ResponseWriter, r *http.Request) {
// Get id from the incoming url
vars := mux.Vars(r)
id := vars["id"]
context := NewContext()
defer context.Close()
col := context.DbCollection("tasks")
repo := &data.TaskRepository{C: col}
task, err := repo.GetById(id)
if err != nil {
if err == mgo.ErrNotFound {
w.WriteHeader(http.StatusNoContent)
} else {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
}
return
}
j, err := json.Marshal(task)
if err != nil {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
// GetTasksByUser returns all Tasks created by a User
// Handler for HTTP Get - "/tasks/users/{id}"
func GetTasksByUser(w http.ResponseWriter, r *http.Request) {
// Get id from the incoming url
vars := mux.Vars(r)
user := vars["id"]
context := NewContext()
defer context.Close()
col := context.DbCollection("tasks")
repo := &data.TaskRepository{C: col}
tasks := repo.GetByUser(user)
j, err := json.Marshal(TasksResource{Data: tasks})
if err != nil {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
// UpdateTask update an existing Task document
// Handler for HTTP Put - "/tasks/{id}"
func UpdateTask(w http.ResponseWriter, r *http.Request) {
// Get id from the incoming url
vars := mux.Vars(r)
id := bson.ObjectIdHex(vars["id"])
var dataResource TaskResource
// Decode the incoming Task json
err := json.NewDecoder(r.Body).Decode(&dataResource)
if err != nil {
common.DisplayAppError(
w,
err,
"Invalid Task data",
500,
)
return
}
task := &dataResource.Data
task.Id = id
context := NewContext()
defer context.Close()
col := context.DbCollection("tasks")
repo := &data.TaskRepository{C: col}
// Update an existing Task document
if err := repo.Update(task); err != nil {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
return
}
w.WriteHeader(http.StatusNoContent)
}
// DeleteTask deelete an existing Task document
// Handler for HTTP Delete - "/tasks/{id}"
func DeleteTask(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
context := NewContext()
defer context.Close()
col := context.DbCollection("tasks")
repo := &data.TaskRepository{C: col}
// Delete an existing Task document
err := repo.Delete(id)
if err != nil {
common.DisplayAppError(
w,
err,
"An unexpected error has occurred",
500,
)
return
}
w.WriteHeader(http.StatusNoContent)
}