-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_read.go
120 lines (106 loc) · 2.99 KB
/
handler_read.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
119
120
package meta_gin
import (
"context"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type ReadHandler[M Model, ReqDTO any, ResDTO any] struct {
DB *gorm.DB
DTOHandler DTOHandler[M, ReqDTO, ResDTO]
Service *Service[M]
ServiceExecuters []ServiceExecutor[M]
}
func NewReadHandler[M Model, ReqDTO any, ResDTO any](
db *gorm.DB,
service *Service[M],
dtoHandler DTOHandler[M, ReqDTO, ResDTO],
) *ReadHandler[M, ReqDTO, ResDTO] {
return &ReadHandler[M, ReqDTO, ResDTO]{
DB: db,
Service: service,
DTOHandler: dtoHandler,
}
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) AddServiceExecuter(
serviceExecuter ServiceExecutor[M],
) {
h.ServiceExecuters = append(h.ServiceExecuters, serviceExecuter)
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) GetName() string {
return "read_handler"
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) Method() string {
return http.MethodGet
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) Handlers() map[string]gin.HandlerFunc {
return map[string]gin.HandlerFunc{
"/all": h.List(),
"/": h.ListPagination(),
"/:id": h.Get(),
}
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) List() gin.HandlerFunc {
return func(ctx *gin.Context) {
models, err := h.Service.Find()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
dtos := []ResDTO{}
for _, model := range models {
dtos = append(dtos, h.DTOHandler.FromModel(model))
}
ctx.JSON(http.StatusOK, gin.H{"datas": dtos})
}
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) ListPagination() gin.HandlerFunc {
return func(ctx *gin.Context) {
page := 1
limit := 10
if l, err := strconv.ParseInt(ctx.Query("limit"), 10, 0); err == nil {
limit = int(l)
}
if p, err := strconv.ParseInt(ctx.Query("page"), 10, 0); err == nil {
page = int(p)
}
for _, queryExecutor := range h.ServiceExecuters {
if queryExecutor != nil {
c := context.WithValue(ctx.Request.Context(), pageLimit, PaginationRequest{
Page: page,
Limit: limit,
})
queryExecutor.Execute(c, nil)
}
}
paginatedResponse, err := h.Service.FindWithPagination(page, limit)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
dtos := []ResDTO{}
for _, model := range paginatedResponse.Items {
dtos = append(dtos, h.DTOHandler.FromModel(model))
}
ctx.JSON(http.StatusOK, gin.H{"datas": dtos, "total": paginatedResponse.Total, "has_next": paginatedResponse.HasNext})
}
}
func (h *ReadHandler[M, ReqDTO, ResDTO]) Get() gin.HandlerFunc {
return func(ctx *gin.Context) {
id := ctx.Param("id")
c := context.WithValue(ctx.Request.Context(), resID, id)
for _, service := range h.ServiceExecuters {
if service != nil {
service.Execute(c, nil)
}
}
model, err := h.Service.FindByID(id)
if err != nil {
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
resDTO := h.DTOHandler.FromModel(model)
ctx.JSON(http.StatusOK, gin.H{"data": resDTO})
}
}