-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
151 lines (127 loc) · 4.17 KB
/
handlers.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
/*
Copyright 2017 Masaru Hoshi.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import(
"encoding/json"
"log"
"net/http"
// Third party packages
"github.com/julienschmidt/httprouter"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func PalindromeListHandler(dao *Dao) func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
return func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
instance := dao.GetInstance()
defer instance.Close()
c := instance.Database().C("palindromes")
var palindromes []Palindrome
err := c.Find(bson.M{}).All(&palindromes)
if err != nil {
JSONError(w, "Database error", http.StatusInternalServerError)
log.Println("[palindromes] List fail: ", err)
return
}
JSONResponse(w, palindromes, http.StatusOK)
}
}
func PalindromeAddHandler(dao *Dao) func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var palindrome Palindrome
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&palindrome)
if err != nil {
JSONError(w, "Invalid request", http.StatusBadRequest)
log.Println("[palindromes] Invalid request: ", err)
return
}
err = palindrome.Validate()
if err != nil {
JSONError(w, "Invalid palindrome", http.StatusBadRequest)
log.Println("[palindromes] Validation: ", err)
return
}
// assing id to new palindrome
palindrome.ID = bson.NewObjectId()
instance := dao.GetInstance()
defer instance.Close()
c := instance.Database().C("palindromes")
err = c.Insert(palindrome)
if err != nil {
if mgo.IsDup(err) {
JSONError(w, "Palindrome already exists", http.StatusAlreadyReported)
log.Println("[palindromes] Duplicate: ", err)
return
}
JSONError(w, "Database error", http.StatusInternalServerError)
log.Println("[palindromes] Failed insert: ", err)
return
}
JSONResponse(w, palindrome, http.StatusCreated)
}
}
func PalindromeGetHandler(dao *Dao) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
if !bson.IsObjectIdHex(id) {
JSONError(w, "Invalid id", http.StatusPreconditionFailed)
log.Println("[palindrome] Invalid id: ", id)
return
}
instance := dao.GetInstance()
defer instance.Close()
c := instance.Database().C("palindromes")
var palindrome Palindrome
err := c.FindId(bson.ObjectIdHex(id)).One(&palindrome)
if err != nil {
switch err {
default:
JSONError(w, "Database error", http.StatusInternalServerError)
log.Println("[palindrome] Failed get: ", err)
return
case mgo.ErrNotFound:
JSONError(w, "Palindrome not found", http.StatusNotFound)
log.Println("[palindrome] Not found: ", err)
return
}
}
JSONResponse(w, palindrome, http.StatusOK)
}
}
func PalindromeDeleteHandler(dao *Dao) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
if !bson.IsObjectIdHex(id) {
JSONError(w, "Invalid id", http.StatusPreconditionFailed)
log.Println("[palindrome] Invalid id: ", id)
return
}
instance := dao.GetInstance()
defer instance.Close()
c := instance.Database().C("palindromes")
err := c.RemoveId(bson.ObjectIdHex(id))
if err != nil {
switch err {
default:
JSONError(w, "Database error", http.StatusInternalServerError)
log.Println("[palindrome] Failed delete: ", err)
return
case mgo.ErrNotFound:
JSONError(w, "Palindrome not found", http.StatusNotFound)
log.Println("[palindrome] Not found: ", err)
return
}
}
w.WriteHeader(http.StatusAccepted)
}
}