/
method.go
108 lines (95 loc) · 2.56 KB
/
method.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
package handler
import (
"github.com/caioeverest/fed-its/internal/config"
"github.com/caioeverest/fed-its/internal/logger"
"github.com/caioeverest/fed-its/model"
"github.com/caioeverest/fed-its/service"
"github.com/labstack/echo/v4"
)
type Method struct {
cfg *config.Config
log *logger.Logger
service service.MethodI
}
func NewMethod(cfg *config.Config, log *logger.Logger, service service.MethodI) *Method {
handler := &Method{
cfg: cfg,
log: log,
service: service,
}
return handler
}
// Create godoc
// @Summary Create a new method
// @Description Create a new method
// @Tags method
// @Accept json
// @Produce json
// @Param payload body model.Method true "payload"
// @Success 200 {object} model.Method
// @Failure 400 {object} itserrors.Error
// @Failure 404 {object} itserrors.Error
// @Failure 500 {object} itserrors.Error
// @Router /method [post]
func (m *Method) Create(pctx echo.Context) (err error) {
var (
payload model.Method
result model.Method
ctx = pctx.Request().Context()
)
if err = pctx.Bind(&payload); err != nil {
m.log.Errorf("Error binding payload: %v", err)
return
}
if result, err = m.service.Create(ctx, payload); err != nil {
m.log.Errorf("Error creating method: %v", err)
return
}
return pctx.JSON(201, result)
}
// List godoc
// @Summary List methods
// @Description List methods
// @Tags method
// @Accept json
// @Produce json
// @Success 200 {array} model.Method
// @Failure 400 {object} itserrors.Error
// @Failure 404 {object} itserrors.Error
// @Failure 500 {object} itserrors.Error
// @Router /method [get]
func (m *Method) List(pctx echo.Context) (err error) {
var (
result []model.Method
ctx = pctx.Request().Context()
)
if result, err = m.service.List(ctx); err != nil {
m.log.Errorf("Error listing methods: %v", err)
return
}
return pctx.JSON(200, result)
}
// Get godoc
// @Summary Get method
// @Description Get method
// @Tags method
// @Accept json
// @Produce json
// @Param method path string true "Method"
// @Success 200 {object} model.Method
// @Failure 400 {object} itserrors.Error
// @Failure 404 {object} itserrors.Error
// @Failure 500 {object} itserrors.Error
// @Router /method/{method} [get]
func (m *Method) Get(pctx echo.Context) (err error) {
var (
result model.Method
method = pctx.Param("method")
ctx = pctx.Request().Context()
)
if result, err = m.service.Get(ctx, method); err != nil {
m.log.Errorf("Error getting method %s: %v", method, err)
return
}
return pctx.JSON(200, result)
}