-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
192 lines (156 loc) · 3.52 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"errors"
"log"
"net/http"
"strconv"
"github.com/hatobus/OHJIN/PresenterDB"
"gopkg.in/go-playground/validator.v9"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"github.com/hatobus/OHJIN/models"
)
var (
ErrBadParameter = errors.New("Bad Parameter type")
ErrInvalidJSON = errors.New("Couldn't parse JSON")
)
type singleGetParameter struct {
machine string `validate:"required"`
}
type multipleGetParameterWithDevice struct {
machine string `validate:"required"`
reqnum int `validate:"required, max=100,min=1"`
}
type multipleGetParameterWithTime struct {
machine string `validate:"required"`
reqnum int `validate:"required"`
}
func main() {
r := gin.Default()
r.POST("/single", singlePOST)
r.GET("/single/:machine", singleGET)
r.GET("/multiple/device/:machine/:num", multiGETWithDevice)
r.GET("/multiple/time/:machine", multiGETWithTime)
r.Run()
}
func singlePOST(c *gin.Context) {
iot := &models.Iotdata{}
err := c.BindJSON(iot)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"Message": ErrInvalidJSON,
"Data": iot,
})
}
spew.Dump(iot)
_, err = PresenterDB.StoreSingleIoTData(iot)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{
"Message": "InternalServerError",
})
return
}
c.JSON(200, gin.H{
"message": "Correctry stored",
})
}
func singleGET(c *gin.Context) {
v := validator.New()
res := &models.Iotdata{}
s := singleGetParameter{
machine: c.Param("machine"),
}
if err := v.Struct(s); err != nil {
log.Println("Illigal parameter")
c.JSON(http.StatusBadRequest, gin.H{
"Message": ErrBadParameter,
"Data": res,
})
return
}
res, err := PresenterDB.RetSingleIoTData(s.machine)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{
"Message": "DBError",
})
return
}
c.JSON(http.StatusOK, gin.H{
"Data": res,
})
}
func multiGETWithDevice(c *gin.Context) {
v := validator.New()
res := &[]models.Iotdata{}
r, err := strconv.Atoi(c.Param("num"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"Message": ErrBadParameter,
})
return
}
m := multipleGetParameterWithDevice{
machine: c.Param("machine"),
reqnum: r,
}
if err := v.Struct(m); err != nil {
log.Println("Illigal parameter")
c.JSON(http.StatusBadRequest, gin.H{
"Message": ErrBadParameter,
"Data": res,
})
return
}
res, err = PresenterDB.RetMultiIoTData(m.machine, m.reqnum)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"Message": "Your request is not correct.",
"Data": res,
})
return
}
c.JSON(http.StatusOK, gin.H{
"Message": "Found " + c.Param("num") + " records",
"Data": res,
})
}
func multiGETWithTime(c *gin.Context) {
v := validator.New()
res := &[]models.Iotdata{}
rt := &models.ReqestTimestamp{}
err := c.BindJSON(rt)
if err != nil {
log.Println("Couldn't Bind JSON")
c.JSON(http.StatusBadRequest, gin.H{
"Message": ErrInvalidJSON,
"Data": res,
})
return
}
m := multipleGetParameterWithTime{
machine: c.Param("machine"),
reqnum: rt.Limit,
}
if err := v.Struct(m); err != nil {
log.Println("Illigal parameter")
c.JSON(http.StatusBadRequest, gin.H{
"Message": ErrBadParameter,
"Data": res,
})
return
}
res, err = PresenterDB.RetMultiIoTDataFromTime(m.machine, rt)
if err != nil {
log.Println("DB Error")
c.JSON(http.StatusInternalServerError, gin.H{
"Message": err,
"Data": res,
})
}
c.JSON(http.StatusOK, gin.H{
"Message": "Found",
"Data": res,
})
}