/
controller.go
76 lines (61 loc) · 1.43 KB
/
controller.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
package datasets
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)
type Controller struct {
DatasetsX *Service
}
type ListsDto struct {
Name string `query:"name"`
}
func (x *Controller) Lists(ctx context.Context, c *app.RequestContext) {
var dto ListsDto
if err := c.BindAndValidate(&dto); err != nil {
c.Error(err)
return
}
r, err := x.DatasetsX.Lists(ctx, dto.Name)
if err != nil {
c.Error(err)
return
}
c.JSON(200, r)
}
type CreateDto struct {
Name string `json:"name" vd:"required"`
Kind string `json:"kind" vd:"oneof='collection' 'timeseries'"`
Option *CreateOptionDto `json:"option" vd:"required_if=kind 'timeseries'"`
}
type CreateOptionDto struct {
Time string `json:"time" vd:"required"`
Meta string `json:"meta" vd:"required"`
Expire *int64 `json:"expire" vd:"required"`
}
func (x *Controller) Create(ctx context.Context, c *app.RequestContext) {
var dto CreateDto
if err := c.BindAndValidate(&dto); err != nil {
c.Error(err)
return
}
if err := x.DatasetsX.Create(ctx, dto); err != nil {
c.Error(err)
return
}
c.Status(201)
}
type DeleteDto struct {
Name string `path:"name" vd:"required"`
}
func (x *Controller) Delete(ctx context.Context, c *app.RequestContext) {
var dto DeleteDto
if err := c.BindAndValidate(&dto); err != nil {
c.Error(err)
return
}
if err := x.DatasetsX.Delete(ctx, dto.Name); err != nil {
c.Error(err)
return
}
c.Status(204)
}