-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
162 lines (137 loc) · 3.34 KB
/
handler.go
File metadata and controls
162 lines (137 loc) · 3.34 KB
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
func init() {
rnd = renderer.New()
sess, err := mgo.Dial(hostName)
checkErr(err)
sess.SetMode(mgo.Monotonic, true)
db = sess.DB(dbName)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
err := rnd.Template(w, http.StatusOK, []string{"static/home.tpl"}, nil)
checkErr(err)
}
func createTodo(w http.ResponseWriter, r *http.Request) {
var t todo
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
rnd.JSON(w, http.StatusProcessing, err)
return
}
// simple validation
if t.Title == "" {
rnd.JSON(w, http.StatusBadRequest, renderer.M{
"message": "The title field is requried",
})
return
}
// if input is okay, create a todo
tm := todoModel{
ID: bson.NewObjectId(),
Title: t.Title,
Completed: false,
CreatedAt: time.Now(),
}
if err := db.C(collectionName).Insert(&tm); err != nil {
rnd.JSON(w, http.StatusProcessing, renderer.M{
"message": "Failed to save todo",
"error": err,
})
return
}
rnd.JSON(w, http.StatusCreated, renderer.M{
"message": "Todo created successfully",
"todo_id": tm.ID.Hex(),
})
}
func updateTodo(w http.ResponseWriter, r *http.Request) {
id := strings.TrimSpace(chi.URLParam(r, "id"))
if !bson.IsObjectIdHex(id) {
rnd.JSON(w, http.StatusBadRequest, renderer.M{
"message": "The id is invalid",
})
return
}
var t todo
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
rnd.JSON(w, http.StatusProcessing, err)
return
}
// simple validation
if t.Title == "" {
rnd.JSON(w, http.StatusBadRequest, renderer.M{
"message": "The title field is requried",
})
return
}
// if input is okay, update a todo
if err := db.C(collectionName).
Update(
bson.M{"_id": bson.ObjectIdHex(id)},
bson.M{"title": t.Title, "completed": t.Completed},
); err != nil {
rnd.JSON(w, http.StatusProcessing, renderer.M{
"message": "Failed to update todo",
"error": err,
})
return
}
rnd.JSON(w, http.StatusOK, renderer.M{
"message": "Todo updated successfully",
})
}
func fetchTodos(w http.ResponseWriter, r *http.Request) {
todos := []todoModel{}
if err := db.C(collectionName).
Find(bson.M{}).
All(&todos); err != nil {
rnd.JSON(w, http.StatusProcessing, renderer.M{
"message": "Failed to fetch todo",
"error": err,
})
return
}
todoList := []todo{}
for _, t := range todos {
todoList = append(todoList, todo{
ID: t.ID.Hex(),
Title: t.Title,
Completed: t.Completed,
CreatedAt: t.CreatedAt,
})
}
rnd.JSON(w, http.StatusOK, renderer.M{
"data": todoList,
})
}
func deleteTodo(w http.ResponseWriter, r *http.Request) {
id := strings.TrimSpace(chi.URLParam(r, "id"))
if !bson.IsObjectIdHex(id) {
rnd.JSON(w, http.StatusBadRequest, renderer.M{
"message": "The id is invalid",
})
return
}
if err := db.C(collectionName).RemoveId(bson.ObjectIdHex(id)); err != nil {
rnd.JSON(w, http.StatusProcessing, renderer.M{
"message": "Failed to delete todo",
"error": err,
})
return
}
rnd.JSON(w, http.StatusOK, renderer.M{
"message": "Todo deleted successfully",
})
}
func todoHandlers() http.Handler {
rg := chi.NewRouter()
rg.Group(func(r chi.Router) {
r.Get("/", fetchTodos)
r.Post("/", createTodo)
r.Put("/{id}", updateTodo)
r.Delete("/{id}", deleteTodo)
})
return rg
}
func checkErr(err error) {
if err != nil {
log.Fatal(err) //respond with error page or message
}
}