-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
42 lines (33 loc) · 846 Bytes
/
api.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
package handler
import (
"net/http"
"github.com/labstack/echo"
"github.com/tomato3713/cw_for_web/model"
)
func GetRecords(c echo.Context) error {
uid := userIDFromToken(c)
if user := model.FindUser(&model.User{ID: uid}); user.ID == 0 {
return echo.ErrNotFound
}
records := model.FindRecords(&model.Record{UID: uid})
return c.JSON(http.StatusOK, records)
}
func AddRecord(c echo.Context) error {
record := new(model.Record)
if err := c.Bind(record); err != nil {
return err
}
if record.Mode == "" {
return &echo.HTTPError{
Code: http.StatusBadRequest,
Message: "invalid point or mode field",
}
}
uid := userIDFromToken(c)
if user := model.FindUser(&model.User{ID: uid}); user.ID == 0 {
return echo.ErrNotFound
}
record.UID = uid
model.CreateRecord(record)
return c.JSON(http.StatusCreated, record)
}