-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo.go
111 lines (85 loc) · 2.66 KB
/
todo.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
package handler
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/shandysiswandi/echo-service/internal/domain"
)
func (h *Handler) FetchTodos(c echo.Context) error {
ctx := c.Request().Context()
data, err := h.tdu.FetchTodos(ctx)
if err != nil {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, data)
}
func (h *Handler) GetTodoById(c echo.Context) error {
ctx := c.Request().Context()
id := c.Param("id")
data, err := h.tdu.GetTodoByID(ctx, id)
if err != nil {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, data)
}
func (h *Handler) CreateTodo(c echo.Context) error {
ctx := c.Request().Context()
payload := domain.TodoCreatePayload{}
// biding payload to struct
if err := c.Bind(&payload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// validate payload struct
if err := c.Validate(&payload); err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
// call usecase or business logic
payload.ID = h.generator.Nanoid()
payload.Timestamp = h.clock.Now()
if err := h.tdu.CreateTodo(ctx, payload); err != nil {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "OK")
}
func (h *Handler) UpdateTodoById(c echo.Context) error {
ctx := c.Request().Context()
payload := domain.TodoUpdatePayload{ID: c.Param("id")}
// biding payload to struct
if err := c.Bind(&payload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// validate payload struct
if err := c.Validate(&payload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// call usecase or business logic
if err := h.tdu.UpdateTodoByID(ctx, payload); err != nil {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "OK")
}
func (h *Handler) ReplaceTodoById(c echo.Context) error {
ctx := c.Request().Context()
payload := domain.TodoReplacePayload{ID: c.Param("id")}
// biding payload to struct
if err := c.Bind(&payload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// validate payload struct
if err := c.Validate(&payload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
// call usecase or business logic
if err := h.tdu.ReplaceTodoByID(ctx, payload); err != nil {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "OK")
}
func (h *Handler) DeleteTodoById(c echo.Context) error {
ctx := c.Request().Context()
id := c.Param("id")
// call usecase or business logic
if err := h.tdu.DeleteTodoByID(ctx, id); err != nil {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "OK")
}