/
handlers.go
117 lines (93 loc) · 2.96 KB
/
handlers.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
package generic
import (
"github.com/HiWay-Media/hwm-go-utils/api/models"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"gopkg.in/validator.v2"
"gorm.io/gorm"
"net/http"
"strconv"
)
type IHandler[T any] interface {
Get(c *fiber.Ctx) error
Create(c *fiber.Ctx) error
Delete(c *fiber.Ctx) error
List(c *fiber.Ctx) error
}
type Handler[T any] struct {
Service IService[T]
Logger *zap.SugaredLogger
}
func NewHandler[T any](service IService[T], logger *zap.SugaredLogger) IHandler[T] {
return &Handler[T]{Service: service, Logger: logger}
}
func (s *Handler[T]) List(c *fiber.Ctx) error {
start, err := strconv.Atoi(c.Query("start", "0"))
if err != nil {
return c.Status(http.StatusBadRequest).JSON(models.ApiDefaultError("start invalid"))
}
limit, err := strconv.Atoi(c.Query("limit", "0"))
if err != nil {
return c.Status(http.StatusBadRequest).JSON(models.ApiDefaultError("limit invalid"))
}
r, err := s.Service.List(start, limit)
if err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(http.StatusNotFound).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusInternalServerError).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusOK).JSON(models.ApiDefaultResponse(r))
}
func (s *Handler[T]) Create(c *fiber.Ctx) error {
var requestBody T
if err := c.BodyParser(&requestBody); err != nil {
return c.Status(http.StatusBadRequest).JSON(models.ApiDefaultError(err.Error()))
}
if err := validator.Validate(&requestBody); err != nil {
return c.Status(http.StatusBadRequest).JSON(models.ApiDefaultError(err.Error()))
}
err := s.Service.Create(&requestBody)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusOK).JSON(models.ApiDefaultResponse(requestBody))
}
func (s *Handler[T]) Get(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return c.Status(http.StatusBadRequest).JSON(models.ApiDefaultError("id invalid"))
}
var anyId any
anyId, err := strconv.Atoi(id)
if err != nil {
anyId = id
}
r, err := s.Service.Get(anyId)
if err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(http.StatusNotFound).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusInternalServerError).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusOK).JSON(models.ApiDefaultResponse(r))
}
func (s *Handler[T]) Delete(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return c.Status(http.StatusBadRequest).JSON(models.ApiDefaultError("id invalid"))
}
var anyId any
anyId, err := strconv.Atoi(id)
if err != nil {
anyId = id
}
err = s.Service.Delete(anyId)
if err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(http.StatusNotFound).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusInternalServerError).JSON(models.ApiDefaultError(err.Error()))
}
return c.Status(http.StatusOK).JSON(models.ApiDefault())
}