/
core.go
148 lines (128 loc) · 3.81 KB
/
core.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
package core
import (
"encoding/hex"
"fmt"
"github.com/Ireoo/API-Core/libs/conf"
"github.com/Ireoo/API-Core/libs/mongo"
"github.com/labstack/echo"
"net/http"
"strconv"
"gopkg.in/mgo.v2/bson"
)
func Core(c echo.Context) (err error) {
e := echo.New()
Input := new(conf.Input)
if error := c.Bind(Input); error != nil {
e.Logger.Print(error)
} else {
e.Logger.Print(Input)
}
Input.Table = c.Param("table")
Input.Mode = c.Param("mode")
Input.Auth = c.Param("auth")
if Input.Auth == "" {
return c.String(http.StatusNonAuthoritativeInfo, "Not Authorization!")
}
app := "api"
if Input.Auth != "94f3eee0-218f-41fc-9318-94cf5430fc7f" {
//var result bson.M
AppInfo := new(conf.AppInfo)
error := mongo.FindOne(app, "apps", bson.M{"secret": Input.Auth}, bson.M{}, &AppInfo)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNonAuthoritativeInfo, "The authorization verification information does not exist. Please verify.")
}
app = hex.EncodeToString([]byte(AppInfo.Id))
//fmt.Println(app)
}
switch Input.Mode {
case "findOne":
var result bson.M
error := mongo.FindOne(app, Input.Table, Input.Where, bson.M{}, &result)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
e.Logger.Print(result)
return c.JSON(http.StatusOK, result)
case "findAll":
var result []bson.M
error := mongo.FindAll(app, Input.Table, Input.Where, bson.M{}, &result)
if error != nil {
fmt.Println(error)
return c.String(http.StatusNotFound, error.Error())
}
e.Logger.Print(result)
return c.JSON(http.StatusOK, result)
case "findPage":
var result []bson.M
error := mongo.FindPage(app, Input.Table, Input.Other.Page, Input.Other.Limit, Input.Where, bson.M{}, &result)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
e.Logger.Print(result)
return c.JSON(http.StatusOK, result)
case "insert":
error := mongo.Insert(app, Input.Table, Input.Data)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
return c.JSON(http.StatusOK, Input.Data)
case "update":
error := mongo.Update(app, Input.Table, bson.M{"$set": Input.Data}, Input.Where)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
return c.JSON(http.StatusOK, Input.Data)
case "updateAll":
error := mongo.UpdateAll(app, Input.Table, bson.M{"$set": Input.Data}, Input.Where)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
return c.JSON(http.StatusOK, Input.Data)
case "upsert":
error := mongo.Upsert(app, Input.Table, bson.M{"$set": Input.Data}, Input.Where)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
return c.JSON(http.StatusOK, Input.Where)
case "remove":
error := mongo.Remove(app, Input.Table, Input.Where)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
return c.JSON(http.StatusOK, Input.Where)
case "removeAll":
error := mongo.RemoveAll(app, Input.Table, Input.Where)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
return c.JSON(http.StatusOK, Input.Where)
case "count":
count, error := mongo.Count(app, Input.Table, Input.Where)
if error != nil {
e.Logger.Print(error)
return c.String(http.StatusNotFound, error.Error())
}
e.Logger.Print(count)
return c.String(http.StatusOK, strconv.Itoa(count))
case "isEmpty":
var r string
if ex := mongo.IsEmpty(app, Input.Table); ex {
r = "true"
} else {
r = "false"
}
e.Logger.Print(r)
return c.String(http.StatusOK, r)
default:
return c.String(http.StatusNotFound, "不存在的操作模式:"+Input.Mode)
}
}