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
|
package main
import (
"log"
"net/http"
"fmt"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type User struct {
ID string `json:"id" bson:"_id,omitempty"`
Name string `json:"name,omitempty"`
Phone string `json:"phone,omitempty"`
Age int `json:"age,omitempty"`
}
var session *mgo.Session
func init() {
s, err := mgo.Dial("localhost")
if err != nil {
log.Fatal(err)
}
session = s
}
func main() {
defer session.Close()
ensureIndex(session)
session.SetMode(mgo.Monotonic, true)
e := echo.New()
e.Use(middleware.Logger())
e.GET("/users", allUsers)
e.GET("/user/:id", getUser)
e.PUT("/user", updateUser)
e.DELETE("/user/:id", deleteUser)
e.POST("/user", saveUser)
e.Logger.Fatal(e.Start(":1424"))
}
func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()
c := session.DB("store").C("users")
index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
func saveUser(e echo.Context) error {
u := new(User)
if err := e.Bind(u); err != nil {
return e.JSON(http.StatusBadRequest, err)
}
s := session.Copy()
defer s.Close()
c := s.DB("store").C("users")
err := c.Insert(u)
if err != nil {
log.Println("Failed insert user", u)
if mgo.IsDup(err) {
return e.JSON(http.StatusBadRequest, "User with this id alread exists.")
}
return e.JSON(http.StatusInternalServerError, "Database error")
}
return e.JSON(http.StatusCreated, "SUCCESS")
}
func getUser(e echo.Context) error {
s := session.Copy()
defer s.Clone()
c := s.DB("store").C("users")
var u User
id := e.Param("id")
fmt.Println("userid", id)
err := c.Find(bson.M{"_id": id}).One(&u)
if err != nil {
log.Println("Failed get user", err)
return e.JSON(http.StatusNotFound, "Database error")
}
return e.JSON(http.StatusOK, u)
}
func updateUser(e echo.Context) error {
u := new(User)
if err := e.Bind(u); err != nil {
return e.JSON(http.StatusBadRequest, err)
}
s := session.Copy()
defer s.Close()
c := s.DB("store").C("users")
err := c.Update(bson.M{"_id": u.ID}, &u)
if err != nil {
switch err {
default:
log.Fatalln("Failed update user: ", err)
return e.JSON(http.StatusInternalServerError, "Database error")
case mgo.ErrNotFound:
return e.JSON(http.StatusNotFound, "Not found")
}
}
return e.JSON(http.StatusOK, u)
}
func deleteUser(e echo.Context) error {
s := session.Copy()
defer s.Close()
id := e.Param("id")
c := s.DB("store").C("users")
err := c.Remove(bson.M{"_id": id})
if err != nil {
switch err {
default:
e.JSON(http.StatusInternalServerError, "Database error")
log.Fatalln("Failed delete user: ", err)
return err
case mgo.ErrNotFound:
e.JSON(http.StatusInternalServerError, "User not found")
return err
}
}
return e.JSON(http.StatusOK, "Sucess")
}
func allUsers(e echo.Context) error {
s := session.Copy()
defer s.Close()
c := s.DB("store").C("users")
var users []User
err := c.Find(bson.M{}).All(&users)
if err != nil {
e.JSON(http.StatusInternalServerError, "Database Error")
return err
}
return e.JSON(http.StatusOK, users)
}
|