Skip to content

Commit e4d3e78

Browse files
committed
refactor
1 parent 2becf74 commit e4d3e78

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

api/restHandler/UserAttributesRestHandler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package restHandler
1919
import (
2020
"encoding/json"
2121
"errors"
22+
"github.com/devtron-labs/devtron/pkg/attributes/bean"
2223
"net/http"
2324

2425
"github.com/devtron-labs/devtron/api/restHandler/common"
@@ -108,15 +109,15 @@ func (handler *UserAttributesRestHandlerImpl) PatchUserAttributes(w http.Respons
108109
common.WriteJsonResp(w, nil, resp, http.StatusOK)
109110
}
110111

111-
func (handler *UserAttributesRestHandlerImpl) validateUserAttributesRequest(w http.ResponseWriter, r *http.Request, operation string) (*attributes.UserAttributesDto, bool) {
112+
func (handler *UserAttributesRestHandlerImpl) validateUserAttributesRequest(w http.ResponseWriter, r *http.Request, operation string) (*bean.UserAttributesDto, bool) {
112113
userId, err := handler.userService.GetLoggedInUser(r)
113114
if userId == 0 || err != nil {
114115
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
115116
return nil, false
116117
}
117118

118119
decoder := json.NewDecoder(r.Body)
119-
var dto attributes.UserAttributesDto
120+
var dto bean.UserAttributesDto
120121
err = decoder.Decode(&dto)
121122
if err != nil {
122123
handler.logger.Errorw("request err, "+operation, "err", err, "payload", dto)
@@ -158,7 +159,7 @@ func (handler *UserAttributesRestHandlerImpl) GetUserAttribute(w http.ResponseWr
158159
return
159160
}
160161

161-
dto := attributes.UserAttributesDto{}
162+
dto := bean.UserAttributesDto{}
162163

163164
emailId, err := handler.userService.GetActiveEmailById(userId)
164165
if err != nil {

pkg/attributes/UserAttributesService.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,17 @@ import (
2828
)
2929

3030
type UserAttributesService interface {
31-
AddUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error)
32-
UpdateUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error)
33-
PatchUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error)
34-
GetUserAttribute(request *UserAttributesDto) (*UserAttributesDto, error)
31+
AddUserAttributes(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error)
32+
UpdateUserAttributes(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error)
33+
PatchUserAttributes(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error)
34+
GetUserAttribute(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error)
3535
}
3636

3737
type UserAttributesServiceImpl struct {
3838
logger *zap.SugaredLogger
3939
attributesRepository repository.UserAttributesRepository
4040
}
4141

42-
type UserAttributesDto struct {
43-
EmailId string `json:"emailId"`
44-
Key string `json:"key"`
45-
Value string `json:"value"`
46-
UserId int32 `json:"-"`
47-
}
48-
4942
func NewUserAttributesServiceImpl(logger *zap.SugaredLogger,
5043
attributesRepository repository.UserAttributesRepository) *UserAttributesServiceImpl {
5144
serviceImpl := &UserAttributesServiceImpl{
@@ -55,7 +48,7 @@ func NewUserAttributesServiceImpl(logger *zap.SugaredLogger,
5548
return serviceImpl
5649
}
5750

58-
func (impl UserAttributesServiceImpl) AddUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) {
51+
func (impl UserAttributesServiceImpl) AddUserAttributes(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error) {
5952
dao := &repository.UserAttributesDao{
6053
EmailId: request.EmailId,
6154
Key: request.Key,
@@ -70,7 +63,7 @@ func (impl UserAttributesServiceImpl) AddUserAttributes(request *UserAttributesD
7063
return request, nil
7164
}
7265

73-
func (impl UserAttributesServiceImpl) UpdateUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) {
66+
func (impl UserAttributesServiceImpl) UpdateUserAttributes(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error) {
7467

7568
userAttribute, err := impl.GetUserAttribute(request)
7669
if err != nil {
@@ -100,7 +93,7 @@ func (impl UserAttributesServiceImpl) UpdateUserAttributes(request *UserAttribut
10093
return request, nil
10194
}
10295

103-
func (impl UserAttributesServiceImpl) PatchUserAttributes(request *UserAttributesDto) (*UserAttributesDto, error) {
96+
func (impl UserAttributesServiceImpl) PatchUserAttributes(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error) {
10497
existingAttribute, err := impl.GetUserAttribute(request)
10598
if err != nil {
10699
impl.logger.Errorw("error while getting user attributes during patch request", "req", request, "error", err)
@@ -156,7 +149,7 @@ func (impl UserAttributesServiceImpl) parseJSONValue(jsonValue, context string)
156149
}
157150

158151
// updateAttributeInDatabase updates the merged data in the database
159-
func (impl UserAttributesServiceImpl) updateAttributeInDatabase(request *UserAttributesDto, mergedData map[string]interface{}) (*UserAttributesDto, error) {
152+
func (impl UserAttributesServiceImpl) updateAttributeInDatabase(request *bean.UserAttributesDto, mergedData map[string]interface{}) (*bean.UserAttributesDto, error) {
160153
mergedJSON, err := json.Marshal(mergedData)
161154
if err != nil {
162155
impl.logger.Errorw("error converting merged data to JSON", "data", mergedData, "error", err)
@@ -253,7 +246,7 @@ func (impl UserAttributesServiceImpl) mergeResourceTypes(existingResources, newR
253246
return hasChanges
254247
}
255248

256-
func (impl UserAttributesServiceImpl) GetUserAttribute(request *UserAttributesDto) (*UserAttributesDto, error) {
249+
func (impl UserAttributesServiceImpl) GetUserAttribute(request *bean.UserAttributesDto) (*bean.UserAttributesDto, error) {
257250

258251
dao := &repository.UserAttributesDao{
259252
EmailId: request.EmailId,
@@ -269,7 +262,7 @@ func (impl UserAttributesServiceImpl) GetUserAttribute(request *UserAttributesDt
269262
impl.logger.Errorw("error in fetching user attributes", "req", request, "error", err)
270263
return nil, errors.New("error occurred while getting user attributes")
271264
}
272-
resAttrDto := &UserAttributesDto{
265+
resAttrDto := &bean.UserAttributesDto{
273266
EmailId: request.EmailId,
274267
Key: request.Key,
275268
Value: modelValue,

pkg/attributes/adapter/adapter.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package adapter
22

3-
import "github.com/devtron-labs/devtron/pkg/attributes"
3+
import (
4+
"github.com/devtron-labs/devtron/pkg/attributes/bean"
5+
)
46

5-
func BuildResponseDTO(request *attributes.UserAttributesDto, mergedValue string) *attributes.UserAttributesDto {
6-
return &attributes.UserAttributesDto{
7+
func BuildResponseDTO(request *bean.UserAttributesDto, mergedValue string) *bean.UserAttributesDto {
8+
return &bean.UserAttributesDto{
79
EmailId: request.EmailId,
810
Key: request.Key,
911
Value: mergedValue,

pkg/attributes/bean/bean.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ type AttributesDto struct {
3131
Active bool `json:"active"`
3232
UserId int32 `json:"-"`
3333
}
34+
35+
type UserAttributesDto struct {
36+
EmailId string `json:"emailId"`
37+
Key string `json:"key"`
38+
Value string `json:"value"`
39+
UserId int32 `json:"-"`
40+
}

0 commit comments

Comments
 (0)