/
handler.go
157 lines (128 loc) · 4.47 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package articles
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"go.elastic.co/apm"
"github.com/fahminlb33/devoria1-wtc-backend/infrastructure/authentication"
"github.com/fahminlb33/devoria1-wtc-backend/infrastructure/utils"
)
type ArticleHandler struct {
Usecase ArticleUseCase
}
func ConstructArticlesHandler(router *gin.Engine, usecase ArticleUseCase, jwtAuth authentication.IJwtAuth) ArticleHandler {
handler := &ArticleHandler{
Usecase: usecase,
}
v1 := router.Group("/api/v1/articles")
v1.GET("", jwtAuth.JwtAuthMiddleware(), handler.FindAll)
v1.POST("", jwtAuth.JwtAuthMiddleware(), handler.Create)
v1.PUT("", jwtAuth.JwtAuthMiddleware(), handler.Save)
v1.GET("/:id", jwtAuth.JwtAuthMiddleware(), handler.Get)
v1.DELETE("/:id", jwtAuth.JwtAuthMiddleware(), handler.Delete)
return *handler
}
// @Summary Find all articles
// @Description Find all matching articles
// @Tags articles
// @Produce json
// @Param keyword path string false "Keyword to search"
// @Param page path int false "Page number"
// @Param limit path int false "Number of items per page"
// @Router /api/v1/articles/ [get]
// @Security JwtAuth
func (u *ArticleHandler) FindAll(c *gin.Context) {
span, _ := apm.StartSpan(c.Request.Context(), "FindAll", "http")
defer span.End()
model := FindAllModel{Page: 1, Limit: 10}
if err := c.ShouldBindQuery(&model); err != nil {
utils.WriteResponse(c, utils.WrapResponse(http.StatusBadRequest, "Validation failed", err.Error()))
return
}
InjectUserId(c, &model.UserId)
result := u.Usecase.FindAll(c, model)
utils.WriteResponse(c, result)
}
// @Summary Create article
// @Description Create single article
// @Tags articles
// @Accepts json
// @Produce json
// @Param body body string true "Article body"
// @Router /api/v1/articles [post]
func (u *ArticleHandler) Create(c *gin.Context) {
span, _ := apm.StartSpan(c.Request.Context(), "Create", "http")
defer span.End()
var model CreateModel
if err := c.ShouldBindJSON(&model); err != nil {
utils.WriteResponse(c, utils.WrapResponse(http.StatusBadRequest, "Validation failed", err.Error()))
return
}
InjectUserId(c, &model.UserId)
result := u.Usecase.Create(c, model)
utils.WriteResponse(c, result)
}
// @Summary Save article
// @Description Update single article
// @Tags articles
// @Accepts json
// @Produce json
// @Param body body string true "Article body"
// @Router /api/v1/articles [put]
func (u *ArticleHandler) Save(c *gin.Context) {
span, _ := apm.StartSpan(c.Request.Context(), "Save", "http")
defer span.End()
var model SaveModel
if err := c.ShouldBindJSON(&model); err != nil {
utils.WriteResponse(c, utils.WrapResponse(http.StatusBadRequest, "Validation failed", err.Error()))
return
}
InjectUserId(c, &model.UserId)
result := u.Usecase.Save(c, model)
utils.WriteResponse(c, result)
}
// @Summary Show article
// @Description Get single article
// @Tags articles
// @Produce json
// @Param id path int true "Unique article ID"
// @Router /api/v1/articles/:id [get]
func (u *ArticleHandler) Get(c *gin.Context) {
span, _ := apm.StartSpan(c.Request.Context(), "Get", "http")
defer span.End()
var model GetModel
if err := c.ShouldBindUri(&model); err != nil {
utils.WriteResponse(c, utils.WrapResponse(http.StatusBadRequest, "Validation failed", err.Error()))
return
}
InjectUserId(c, &model.UserId)
result := u.Usecase.Get(c, model)
utils.WriteResponse(c, result)
}
// @Summary Delete an article
// @Description Delete single article
// @Tags articles
// @Produce json
// @Param id path int true "Unique article ID"
// @Router /api/v1/articles/:id [delete]
func (u *ArticleHandler) Delete(c *gin.Context) {
span, _ := apm.StartSpan(c.Request.Context(), "Delete", "http")
defer span.End()
var model DeleteModel
if err := c.ShouldBindUri(&model); err != nil {
utils.WriteResponse(c, utils.WrapResponse(http.StatusBadRequest, "Validation failed", err.Error()))
return
}
InjectUserId(c, &model.UserId)
result := u.Usecase.Delete(c, model)
utils.WriteResponse(c, result)
}
func InjectUserId(c *gin.Context, userId *int) {
user, err := authentication.GetJwtUser(c)
if err != nil {
utils.WriteResponse(c, utils.WrapResponse(http.StatusInternalServerError, "Can't get user from token", nil))
return
}
id, _ := strconv.Atoi(user.Id)
*userId = id
}