/
gym.go
115 lines (94 loc) · 2.52 KB
/
gym.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
/*
* Revision History:
* Initial: 2018/05/24 Chen Yanchen
*/
package handler
import (
"net/http"
"github.com/labstack/echo"
"gopkg.in/mgo.v2"
"github.com/JonSnow007/Gymnasium-management-system/GMS/common"
"github.com/JonSnow007/Gymnasium-management-system/GMS/model"
)
type groundHandler struct{}
var Ground *groundHandler
// 新建场地
func (*groundHandler) New(c echo.Context) error {
var (
err error
req struct {
Name string `json:"name" validate:"required,min=1,max=20"`
}
)
//req.Name = c.FormValue("name")
if err = c.Bind(&req); err != nil {
c.Logger().Error("[Bind]", err)
return c.JSON(http.StatusOK, Resp(common.ErrParam))
}
if err = c.Validate(&req); err != nil {
c.Logger().Error("[Validate]", err)
return c.JSON(http.StatusOK, Resp(common.ErrValidate))
}
err = model.GymService.New(req.Name)
if err != nil {
c.Logger().Error("[New account]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, nil))
}
// 场地信息
func (*groundHandler) Info(c echo.Context) error {
var (
err error
req struct {
Id int
}
)
//req.Id, err = strconv.Atoi(c.FormValue("id"))
if err = c.Bind(&req); err != nil {
c.Logger().Error("[Bind]", err)
return c.JSON(http.StatusOK, Resp(common.ErrParam))
}
g, err := model.GymService.Info(req.Id)
if err != nil {
if err == mgo.ErrNotFound {
c.Logger().Error("[Info]", err)
return c.JSON(http.StatusOK, Resp(common.ErrNotFound))
}
c.Logger().Error("[Info]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, g))
}
// 场地列表
func (*groundHandler) List(c echo.Context) error {
g, err := model.GymService.List()
if err != nil {
c.Logger().Error("[List]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, g))
}
// 修改场地状态
func (*groundHandler) ModifyState(c echo.Context) error {
var (
err error
req struct {
Id int
}
)
//req.Id, err = strconv.Atoi(c.FormValue("id"))
if err = c.Bind(&req); err != nil {
c.Logger().Error("[Bind]", err)
return c.JSON(http.StatusOK, Resp(common.ErrParam))
}
err = model.GymService.State(req.Id)
if err != nil {
if err == mgo.ErrNotFound {
return c.JSON(http.StatusOK, Resp(common.ErrNotFound))
}
c.Logger().Error("[ModifyState]", err)
return c.JSON(http.StatusOK, Resp(common.ErrMongoDB))
}
return c.JSON(http.StatusOK, Resp(common.RespSuccess, nil))
}