-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
108 lines (101 loc) · 3.1 KB
/
handler.go
File metadata and controls
108 lines (101 loc) · 3.1 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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"gopkg.in/macaron.v1"
"gopkg.in/mgo.v2/bson"
"time"
)
func overviewHandler(ctx *macaron.Context) {
db := GetDbSession()
c := db.DB("golang").C("people")
var results []Person
err := c.Find(bson.M{}).All(&results)
if err != nil {
panic(err)
}
mapResults := map[string][]Person{"data": results}
ctx.JSON(200, &mapResults)
}
func detailHandler(ctx *macaron.Context) {
db := GetDbSession()
c := db.DB("golang").C("people")
result := Person{}
err := c.FindId(bson.ObjectIdHex(ctx.Params("id"))).One(&result)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"uri": ctx.Req.RequestURI,
"method": ctx.Req.Method,
}).Error("Error")
ctx.JSON(404, map[string]string{"message": "not found", "id": ctx.Params("id")})
} else {
mapResults := map[string]Person{"data": result}
ctx.JSON(200, &mapResults)
}
}
func createHandler(ctx *macaron.Context, person Person) {
db := GetDbSession()
c := db.DB("golang").C("people")
t := time.Now()
id := bson.NewObjectId()
err := c.Insert(&Person{ID: id,
Name: person.Name,
Age: person.Age,
Time: fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())})
if err != nil {
log.WithFields(log.Fields{
"error": err,
"uri": ctx.Req.RequestURI,
"method": ctx.Req.Method,
}).Error("Error")
ctx.JSON(400, map[string]string{"message": "error"})
} else {
ctx.JSON(201, map[string]string{"message": "Created", "id": id.String()})
}
}
func updateHandler(ctx *macaron.Context, person Person) {
db := GetDbSession()
c := db.DB("golang").C("people")
change := bson.M{"$set": bson.M{"name": person.Name, "age": person.Age, "time": time.Now()}}
err := c.UpdateId(bson.ObjectIdHex(ctx.Params("id")), change)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"uri": ctx.Req.RequestURI,
"method": ctx.Req.Method,
}).Error("Error")
ctx.JSON(404, map[string]string{"message": "not found", "id": ctx.Params("id")})
}
ctx.JSON(201, map[string]string{"message": "updated", "id": ctx.Params("id")})
}
func upgradeHandler(ctx *macaron.Context, person Person) {
db := GetDbSession()
c := db.DB("golang").C("people")
change := bson.M{"$set": bson.M{"name": person.Name, "age": person.Age, "time": time.Now()}}
err := c.UpdateId(bson.ObjectIdHex(ctx.Params("id")), change)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"uri": ctx.Req.RequestURI,
"method": ctx.Req.Method,
}).Error("Error")
ctx.JSON(404, map[string]string{"message": "not found", "id": ctx.Params("id")})
}
ctx.JSON(201, map[string]string{"message": "upgraded", "id": ctx.Params("id")})
}
func deleteHandler(ctx *macaron.Context) {
db := GetDbSession()
c := db.DB("golang").C("people")
err := c.RemoveId(bson.ObjectIdHex(ctx.Params("id")))
if err != nil {
log.WithFields(log.Fields{
"error": err,
"uri": ctx.Req.RequestURI,
"method": ctx.Req.Method,
}).Error("Error")
ctx.JSON(404, map[string]string{"message": "not found", "id": ctx.Params("id")})
} else {
ctx.JSON(204, map[string]string{"message": "deleted", "id": ctx.Params("id")})
}
}