Skip to content

Commit

Permalink
refactor: normalize user fields when controller responses
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed May 26, 2024
1 parent a9737cd commit 3b9c69d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controller/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 47 in app/controller/auth_controller.go

View workflow job for this annotation

GitHub Actions / linting

Error return value of `presenters.UserResponse` is not checked (errcheck)
}
}

Expand Down
27 changes: 22 additions & 5 deletions app/presenters/user_presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
})

Expand Down
3 changes: 0 additions & 3 deletions internal/repository/auth_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repository
import (
"context"
"errors"
"fmt"
"time"

"github.com/kavkaco/Kavka-Core/database"
Expand Down Expand Up @@ -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}}

Expand Down

0 comments on commit 3b9c69d

Please sign in to comment.