generated from dzungtran/echo-rest-api
/
user.go
216 lines (190 loc) · 5.41 KB
/
user.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package handlers
import (
"errors"
"net/http"
"strconv"
"github.com/royzaa/basic-rest-echo/modules/core/domains"
"github.com/royzaa/basic-rest-echo/modules/core/dto"
"github.com/royzaa/basic-rest-echo/modules/core/usecases"
"github.com/royzaa/basic-rest-echo/pkg/authz"
"github.com/royzaa/basic-rest-echo/pkg/constants"
"github.com/royzaa/basic-rest-echo/pkg/middlewares"
"github.com/royzaa/basic-rest-echo/pkg/utils"
"github.com/royzaa/basic-rest-echo/pkg/wrapper"
"github.com/labstack/echo/v4"
)
type UserHandler struct {
UserUC usecases.UserUsecase
}
// NewUserHandler will initialize the user resources endpoint
func NewUserHandler(g *echo.Group, middManager *middlewares.MiddlewareManager, userUsecase usecases.UserUsecase) {
handler := &UserHandler{
UserUC: userUsecase,
}
apiV1 := g.Group("admin/users", middManager.Auth(), middManager.CheckPolicies())
apiV1.GET("", wrapper.Wrap(handler.Fetch)).Name = "list:user"
apiV1.POST("", wrapper.Wrap(handler.Create)).Name = "create:user"
apiV1Resource := g.Group("admin/users/:userId", middManager.Auth())
apiV1Resource.GET("", wrapper.Wrap(handler.GetByID)).Name = "read:user"
apiV1Resource.PUT("", wrapper.Wrap(handler.Update), middManager.CheckPolicies()).Name = "update:user"
apiV1Resource.DELETE("", wrapper.Wrap(handler.Delete), middManager.CheckPolicies()).Name = "delete:user"
}
// Create will store the user by given request body
func (h *UserHandler) Create(c echo.Context) wrapper.Response {
ctx := c.Request().Context()
var req dto.CreateUserReq
var user *domains.User
var err error
if err = c.Bind(&req); err != nil {
return wrapper.Response{
Status: http.StatusBadRequest,
Error: utils.NewError(err, ""),
}
}
if user, err = h.UserUC.Create(ctx, req); err != nil {
return wrapper.Response{
Status: http.StatusInternalServerError,
Error: utils.NewError(err, ""),
}
}
return wrapper.Response{Status: http.StatusCreated, Data: user}
}
// GetUserInfo godoc
// @Summary Get user info
// @Description Get user info ID
// @Tags users
// @Accept json
// @Produce json
// @Param userId path int true "User ID"
// @Success 200 {object} wrapper.SuccessResponse{data=domains.User}
// @Router /users/{userId} [get]
func (h *UserHandler) GetByID(c echo.Context) wrapper.Response {
ctx := c.Request().Context()
id, err := strconv.Atoi(c.Param("userId"))
if err != nil {
return wrapper.Response{
Status: http.StatusBadRequest,
Error: errors.New("invalid id"),
}
}
denyMsg, err := authz.CheckPoliciesContext(c, authz.WithInputExtraData("user_info", &domains.User{
Id: int64(id),
}))
if err != nil {
msg := ""
if len(denyMsg) > 0 {
msg = denyMsg[0]
}
return wrapper.Response{
Status: http.StatusForbidden,
Error: utils.NewError(err, msg),
}
}
user, err := h.UserUC.GetByID(ctx, int64(id))
if err != nil {
if err == constants.ErrNotFound {
return wrapper.Response{
Status: http.StatusNotFound,
Error: utils.NewNotFoundError(),
}
}
return wrapper.Response{
Status: http.StatusInternalServerError,
Error: utils.NewError(err, ""),
}
}
return wrapper.Response{
Data: user,
}
}
// Fetch will fetch the user
func (h *UserHandler) Fetch(c echo.Context) wrapper.Response {
ctx := c.Request().Context()
var req dto.SearchUsersReq
if err := c.Bind(&req); err != nil {
return wrapper.Response{
Status: http.StatusBadRequest,
Error: utils.NewError(err, ""),
}
}
users, count, err := h.UserUC.Fetch(ctx, req)
if err != nil {
return wrapper.Response{
Error: err,
Status: http.StatusInternalServerError,
}
}
return wrapper.Response{
Data: users,
Total: count,
IncludeTotal: true,
}
}
// Update will get user by given request body
func (h *UserHandler) Update(c echo.Context) wrapper.Response {
ctx := c.Request().Context()
id, err := strconv.Atoi(c.Param("userId"))
if err != nil {
return wrapper.Response{
Status: http.StatusBadRequest,
Error: utils.NewNotFoundError(),
}
}
var req dto.UpdateUserReq
if err = c.Bind(&req); err != nil {
return wrapper.Response{
Status: http.StatusUnprocessableEntity,
Error: utils.NewError(err, ""),
}
}
denyMsg, err := authz.CheckPoliciesContext(c, authz.WithInputExtraData("user_info", &domains.User{
Id: int64(id),
}))
if err != nil {
msg := ""
if len(denyMsg) > 0 {
msg = denyMsg[0]
}
return wrapper.Response{
Status: http.StatusForbidden,
Error: utils.NewError(err, msg),
}
}
if err := h.UserUC.Update(ctx, int64(id), req); err != nil {
if err == constants.ErrNotFound {
return wrapper.Response{
Status: http.StatusNotFound,
Error: utils.NewNotFoundError(),
}
}
return wrapper.Response{
Error: err,
Status: http.StatusInternalServerError,
}
}
return wrapper.Response{}
}
// Delete will delete user by given param
func (h *UserHandler) Delete(c echo.Context) wrapper.Response {
ctx := c.Request().Context()
id, err := strconv.Atoi(c.Param("userId"))
if err != nil {
return wrapper.Response{
Status: http.StatusBadRequest,
Error: utils.NewNotFoundError(),
}
}
if err = h.UserUC.Delete(ctx, int64(id)); err != nil {
if err == constants.ErrNotFound {
return wrapper.Response{
Status: http.StatusNotFound,
Error: utils.NewNotFoundError(),
}
}
return wrapper.Response{
Status: http.StatusInternalServerError,
Error: utils.NewError(err, ""),
}
}
return wrapper.Response{}
}