-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
157 lines (122 loc) · 3.29 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rookie-ninja/rk-boot/v2"
"github.com/rookie-ninja/rk-db/mongodb"
"github.com/rookie-ninja/rk-gin/v2/boot"
"github.com/rs/xid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
)
var (
userCollection *mongo.Collection
)
func createCollection(db *mongo.Database, name string) {
opts := options.CreateCollection()
err := db.CreateCollection(context.TODO(), name, opts)
if err != nil {
fmt.Println("collection exists may be, continue")
}
}
func main() {
boot := rkboot.NewBoot()
boot.Bootstrap(context.TODO())
// Auto migrate database and init global userDb variable
db := rkmongo.GetMongoDB("my-mongo", "users")
createCollection(db, "meta")
userCollection = db.Collection("meta")
// Register APIs
ginEntry := rkgin.GetGinEntry("user-service")
ginEntry.Router.GET("/v1/user", ListUsers)
ginEntry.Router.GET("/v1/user/:id", GetUser)
ginEntry.Router.PUT("/v1/user", CreateUser)
ginEntry.Router.POST("/v1/user/:id", UpdateUser)
ginEntry.Router.DELETE("/v1/user/:id", DeleteUser)
boot.WaitForShutdownSig(context.TODO())
}
// *************************************
// *************** Model ***************
// *************************************
type User struct {
Id string `bson:"id" yaml:"id" json:"id"`
Name string `bson:"name" yaml:"name" json:"name"`
}
func ListUsers(ctx *gin.Context) {
userList := make([]*User, 0)
cursor, err := userCollection.Find(context.Background(), bson.D{})
if err != nil {
ctx.JSON(http.StatusInternalServerError, err)
return
}
if err = cursor.All(context.TODO(), &userList); err != nil {
ctx.JSON(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, userList)
}
func GetUser(ctx *gin.Context) {
res := userCollection.FindOne(context.Background(), bson.M{"id": ctx.Param("id")})
if res.Err() != nil {
ctx.AbortWithError(http.StatusInternalServerError, res.Err())
return
}
user := &User{}
err := res.Decode(user)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, user)
}
func CreateUser(ctx *gin.Context) {
user := &User{
Id: xid.New().String(),
Name: ctx.Query("name"),
}
_, err := userCollection.InsertOne(context.Background(), user)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, user)
}
func UpdateUser(ctx *gin.Context) {
uid := ctx.Param("id")
user := &User{
Id: uid,
Name: ctx.Query("name"),
}
res, err := userCollection.UpdateOne(context.Background(), bson.M{"id": uid}, bson.D{
{"$set", user},
})
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if res.MatchedCount < 1 {
ctx.JSON(http.StatusNotFound, "user not found")
return
}
bytes, _ := json.Marshal(user)
fmt.Println(string(bytes))
ctx.JSON(http.StatusOK, user)
}
func DeleteUser(ctx *gin.Context) {
res, err := userCollection.DeleteOne(context.Background(), bson.M{
"id": ctx.Param("id"),
})
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if res.DeletedCount < 1 {
ctx.JSON(http.StatusNotFound, "user not found")
return
}
ctx.String(http.StatusOK, "success")
}