/
handler.go
118 lines (99 loc) · 2.49 KB
/
handler.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
package core
import (
"errors"
"net/http"
echo "github.com/labstack/echo/v4"
)
type Handler struct {
Creator interface {
Create(c any) (any, error)
}
Reader interface {
Read(id any) (any, error)
ReadAll(limit, marker any) ([]any, error)
}
Updater interface {
Update(r any) (any, error)
}
Deleter interface {
Delete(id any) error
}
Executor interface {
Execute(e any) (any, error)
Action() string
}
}
// BasicCheck - basic health check response
func (h Handler) BasicCheck(c echo.Context) error {
return c.NoContent(http.StatusOK)
}
// Create - creater lambgo
func (h Handler) Create(c echo.Context) error {
var lambgo any
if err := c.Bind(&lambgo); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
l, err := h.Creator.Create(lambgo)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, l)
}
// Read - readr lambgo
func (h Handler) Read(c echo.Context) error {
id := c.Param("id")
if id == "" {
return c.JSON(http.StatusBadRequest, errors.New("id param not present"))
}
l, err := h.Reader.Read(id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, l)
}
// ReadAll - read all lambgo
func (h Handler) ReadAll(c echo.Context) error {
limit := c.QueryParam("limit")
marker := c.QueryParam("marker")
l, err := h.Reader.ReadAll(limit, marker)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, l)
}
// Update - updater lambgo
func (h Handler) Update(c echo.Context) error {
var lambgo any
if err := c.Bind(&lambgo); err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
l, err := h.Updater.Update(lambgo)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, l)
}
// Delete - deleter lambgo
func (h Handler) Delete(c echo.Context) error {
id := c.Param("id")
if id == "" {
return c.JSON(http.StatusBadRequest, errors.New("id param not present"))
}
err := h.Deleter.Delete(id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
}
// Executor - executor lambgo
func (h Handler) Execute(c echo.Context) error {
var lambgo any
if err := c.Bind(&lambgo); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
l, err := h.Executor.Execute(lambgo)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, l)
}