Skip to content

Commit d73b76c

Browse files
committed
feat: add get user by username endpoint
1 parent 1f60023 commit d73b76c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

controller/user_controller.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type (
1414
Register(ctx *gin.Context)
1515
Login(ctx *gin.Context)
1616
Me(ctx *gin.Context)
17+
GetUserByUsername(ctx *gin.Context)
1718
}
1819

1920
userController struct {
@@ -78,3 +79,22 @@ func (c *userController) Login(ctx *gin.Context) {
7879
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_LOGIN, result)
7980
ctx.JSON(http.StatusOK, res)
8081
}
82+
83+
func (c *userController) GetUserByUsername(ctx *gin.Context) {
84+
username := ctx.Param("username")
85+
if username == "" {
86+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, dto.ErrUsernameNotFound.Error(), nil)
87+
ctx.JSON(http.StatusBadRequest, res)
88+
return
89+
}
90+
91+
result, err := c.userService.GetUserByUsername(ctx.Request.Context(), username)
92+
if err != nil {
93+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, err.Error(), nil)
94+
ctx.JSON(http.StatusBadRequest, res)
95+
return
96+
}
97+
98+
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_USER, result)
99+
ctx.JSON(http.StatusOK, res)
100+
}

routes/user_route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ func User(route *gin.Engine, injector *do.Injector) {
1919
routes.POST("/register", userController.Register)
2020
routes.POST("/login", userController.Login)
2121
routes.GET("/me", middleware.Authenticate(jwtService), userController.Me)
22+
routes.GET("/:username", userController.GetUserByUsername)
2223
}
2324
}

service/user_service.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type (
1717
Register(ctx context.Context, req dto.UserCreateRequest) (dto.UserResponse, error)
1818
GetUserById(ctx context.Context, userId string) (dto.UserResponse, error)
1919
Verify(ctx context.Context, req dto.UserLoginRequest) (dto.UserLoginResponse, error)
20+
GetUserByUsername(ctx context.Context, username string) (dto.UserResponse, error)
2021
}
2122

2223
userService struct {
@@ -105,3 +106,18 @@ func (s *userService) Verify(ctx context.Context, req dto.UserLoginRequest) (dto
105106
Token: token,
106107
}, nil
107108
}
109+
110+
func (s *userService) GetUserByUsername(ctx context.Context, username string) (dto.UserResponse, error) {
111+
user, _, err := s.userRepo.CheckUsername(ctx, nil, username)
112+
if err != nil {
113+
return dto.UserResponse{}, dto.ErrGetUserById
114+
}
115+
116+
return dto.UserResponse{
117+
ID: user.ID.String(),
118+
Name: user.Name,
119+
UserName: user.Username,
120+
Bio: user.Bio,
121+
ImageUrl: user.ImageUrl,
122+
}, nil
123+
}

0 commit comments

Comments
 (0)