From 3b9c69dcbcac7bd78ab9f3bb8fb2e344652e4553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5B=20Taha=2E=20Dostifam=E2=80=8D=20=5D?= Date: Sun, 26 May 2024 13:58:31 +0330 Subject: [PATCH] refactor: normalize user fields when controller responses --- app/controller/auth_controller.go | 2 +- app/presenters/user_presenter.go | 27 +++++++++++++++++++++----- internal/repository/auth_repository.go | 3 --- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/controller/auth_controller.go b/app/controller/auth_controller.go index 5c208a0..034f085 100644 --- a/app/controller/auth_controller.go +++ b/app/controller/auth_controller.go @@ -44,7 +44,7 @@ func (ctrl *AuthController) HandleLogin(c *gin.Context) { c.Header(presenters.RefreshTokenHeaderName, refreshToken) c.Header(presenters.AccessTokenHeaderName, accessToken) - c.JSON(http.StatusOK, presenters.UserResponse(c, user, userChats)) + presenters.UserResponse(c, user, userChats) } } diff --git a/app/presenters/user_presenter.go b/app/presenters/user_presenter.go index b114601..968dbff 100644 --- a/app/presenters/user_presenter.go +++ b/app/presenters/user_presenter.go @@ -8,10 +8,19 @@ import ( ) type UserDto struct { - Message string `json:"message"` - Code int `json:"code"` - User *model.User `json:"user"` - UserChats []model.Chat `json:"chats"` + Message string `json:"message"` + Code int `json:"code"` + User *UserDetailDto `json:"user"` + UserChats []model.Chat `json:"chats"` +} + +type UserDetailDto struct { + UserID model.UserID `json:"userID"` + Name string `json:"name"` + LastName string `json:"lastName"` + Email string `json:"email"` + Username string `json:"username"` + Biography string `json:"biography"` } func AccessDenied(ctx *gin.Context) { @@ -37,11 +46,19 @@ func UserResponse(ctx *gin.Context, user *model.User, userChats []model.Chat) er } // Response JSON + var normalizedUser UserDetailDto + + normalizedUser.UserID = user.UserID + normalizedUser.Email = user.Email + normalizedUser.Username = user.Username + normalizedUser.Name = user.Name + normalizedUser.LastName = user.LastName + normalizedUser.Biography = user.Biography ctx.JSON(http.StatusOK, UserDto{ Message: "user found", Code: 200, - User: user, + User: &normalizedUser, UserChats: marshaledChatsJson, }) diff --git a/internal/repository/auth_repository.go b/internal/repository/auth_repository.go index cdb7792..0e44059 100644 --- a/internal/repository/auth_repository.go +++ b/internal/repository/auth_repository.go @@ -3,7 +3,6 @@ package repository import ( "context" "errors" - "fmt" "time" "github.com/kavkaco/Kavka-Core/database" @@ -46,8 +45,6 @@ func (a *authRepository) IncrementFailedLoginAttempts(ctx context.Context, userI } func (a *authRepository) ClearFailedLoginAttempts(ctx context.Context, userID model.UserID) error { - fmt.Println("fuck you mother fucker") - filter := bson.M{"user_id": userID} update := bson.M{"$set": bson.M{"failed_login_attempts": 0}}