Skip to content

Commit a645721

Browse files
committed
refactor to remove duplicate code
1 parent 51049c1 commit a645721

File tree

1 file changed

+24
-66
lines changed

1 file changed

+24
-66
lines changed

api/restHandler/UserAttributesRestHandler.go

Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,13 @@ func NewUserAttributesRestHandlerImpl(logger *zap.SugaredLogger, enforcer casbin
5555
}
5656

5757
func (handler *UserAttributesRestHandlerImpl) AddUserAttributes(w http.ResponseWriter, r *http.Request) {
58-
userId, err := handler.userService.GetLoggedInUser(r)
59-
if userId == 0 || err != nil {
60-
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
61-
return
62-
}
63-
decoder := json.NewDecoder(r.Body)
64-
var dto attributes.UserAttributesDto
65-
err = decoder.Decode(&dto)
66-
if err != nil {
67-
handler.logger.Errorw("request err, AddUserAttributes", "err", err, "payload", dto)
68-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
69-
return
70-
}
71-
72-
dto.UserId = userId
73-
//if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*"); !ok {
74-
// common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
75-
// return
76-
//}
77-
emailId, err := handler.userService.GetActiveEmailById(userId)
78-
if err != nil {
79-
handler.logger.Errorw("request err, UpdateUserAttributes", "err", err, "payload", dto)
80-
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
58+
dto, success := handler.validateUserAttributesRequest(w, r, "PatchUserAttributes")
59+
if !success {
8160
return
8261
}
83-
dto.EmailId = emailId
8462

8563
handler.logger.Infow("request payload, AddUserAttributes", "payload", dto)
86-
resp, err := handler.userAttributesService.AddUserAttributes(&dto)
64+
resp, err := handler.userAttributesService.AddUserAttributes(dto)
8765
if err != nil {
8866
handler.logger.Errorw("service err, AddUserAttributes", "err", err, "payload", dto)
8967
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -99,79 +77,64 @@ func (handler *UserAttributesRestHandlerImpl) AddUserAttributes(w http.ResponseW
9977
// @Success 200 {object} attributes.UserAttributesDto
10078
// @Router /orchestrator/attributes/user/update [POST]
10179
func (handler *UserAttributesRestHandlerImpl) UpdateUserAttributes(w http.ResponseWriter, r *http.Request) {
102-
userId, err := handler.userService.GetLoggedInUser(r)
103-
if userId == 0 || err != nil {
104-
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
80+
dto, success := handler.validateUserAttributesRequest(w, r, "PatchUserAttributes")
81+
if !success {
10582
return
10683
}
10784

108-
decoder := json.NewDecoder(r.Body)
109-
var dto attributes.UserAttributesDto
110-
err = decoder.Decode(&dto)
85+
handler.logger.Infow("request payload, UpdateUserAttributes", "payload", dto)
86+
resp, err := handler.userAttributesService.UpdateUserAttributes(dto)
11187
if err != nil {
112-
handler.logger.Errorw("request err, UpdateUserAttributes", "err", err, "payload", dto)
113-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
88+
handler.logger.Errorw("service err, UpdateUserAttributes", "err", err, "payload", dto)
89+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
11490
return
11591
}
92+
common.WriteJsonResp(w, nil, resp, http.StatusOK)
93+
}
11694

117-
dto.UserId = userId
118-
//if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionUpdate, "*"); !ok {
119-
// common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
120-
// return
121-
//}
122-
123-
emailId, err := handler.userService.GetActiveEmailById(userId)
124-
if err != nil {
125-
handler.logger.Errorw("request err, UpdateUserAttributes", "err", err, "payload", dto)
126-
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
95+
func (handler *UserAttributesRestHandlerImpl) PatchUserAttributes(w http.ResponseWriter, r *http.Request) {
96+
dto, success := handler.validateUserAttributesRequest(w, r, "PatchUserAttributes")
97+
if !success {
12798
return
12899
}
129-
dto.EmailId = emailId
130100

131-
handler.logger.Infow("request payload, UpdateUserAttributes", "payload", dto)
132-
resp, err := handler.userAttributesService.UpdateUserAttributes(&dto)
101+
handler.logger.Infow("request payload, PatchUserAttributes", "payload", dto)
102+
resp, err := handler.userAttributesService.PatchUserAttributes(dto)
133103
if err != nil {
134-
handler.logger.Errorw("service err, UpdateUserAttributes", "err", err, "payload", dto)
104+
handler.logger.Errorw("service err, PatchUserAttributes", "err", err, "payload", dto)
135105
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
136106
return
137107
}
138108
common.WriteJsonResp(w, nil, resp, http.StatusOK)
139109
}
140110

141-
func (handler *UserAttributesRestHandlerImpl) PatchUserAttributes(w http.ResponseWriter, r *http.Request) {
111+
func (handler *UserAttributesRestHandlerImpl) validateUserAttributesRequest(w http.ResponseWriter, r *http.Request, operation string) (*attributes.UserAttributesDto, bool) {
142112
userId, err := handler.userService.GetLoggedInUser(r)
143113
if userId == 0 || err != nil {
144114
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
145-
return
115+
return nil, false
146116
}
147117

148118
decoder := json.NewDecoder(r.Body)
149119
var dto attributes.UserAttributesDto
150120
err = decoder.Decode(&dto)
151121
if err != nil {
152-
handler.logger.Errorw("request err, PatchUserAttributes", "err", err, "payload", dto)
122+
handler.logger.Errorw("request err, "+operation, "err", err, "payload", dto)
153123
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
154-
return
124+
return nil, false
155125
}
156126

157127
dto.UserId = userId
158128

159129
emailId, err := handler.userService.GetActiveEmailById(userId)
160130
if err != nil {
161-
handler.logger.Errorw("request err, PatchUserAttributes", "err", err, "payload", dto)
131+
handler.logger.Errorw("request err, "+operation, "err", err, "payload", dto)
162132
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
163-
return
133+
return nil, false
164134
}
165135
dto.EmailId = emailId
166136

167-
handler.logger.Infow("request payload, PatchUserAttributes", "payload", dto)
168-
resp, err := handler.userAttributesService.PatchUserAttributes(&dto)
169-
if err != nil {
170-
handler.logger.Errorw("service err, PatchUserAttributes", "err", err, "payload", dto)
171-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
172-
return
173-
}
174-
common.WriteJsonResp(w, nil, resp, http.StatusOK)
137+
return &dto, true
175138
}
176139

177140
// @Summary get user attributes
@@ -195,11 +158,6 @@ func (handler *UserAttributesRestHandlerImpl) GetUserAttribute(w http.ResponseWr
195158
return
196159
}
197160

198-
//if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !ok {
199-
// common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
200-
// return
201-
//}
202-
203161
dto := attributes.UserAttributesDto{}
204162

205163
emailId, err := handler.userService.GetActiveEmailById(userId)

0 commit comments

Comments
 (0)