Skip to content

Commit 0830c64

Browse files
committed
feat: add get user posts by username endpoint
1 parent f08fab2 commit 0830c64

File tree

6 files changed

+115
-2
lines changed

6 files changed

+115
-2
lines changed

controller/user_controller.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type (
1717
GetUserByUsername(ctx *gin.Context)
1818
UpdateUser(ctx *gin.Context)
1919
CheckUsername(ctx *gin.Context)
20+
GetUserPosts(ctx *gin.Context)
2021
}
2122

2223
userController struct {
@@ -139,3 +140,35 @@ func (c *userController) CheckUsername(ctx *gin.Context) {
139140
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_USERNAME_EXISTS, dto.ErrUsernameAlreadyExists.Error(), nil)
140141
ctx.JSON(http.StatusBadRequest, res)
141142
}
143+
144+
func (c *userController) GetUserPosts(ctx *gin.Context) {
145+
username := ctx.Param("username")
146+
if username == "" {
147+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, dto.ErrUsernameNotFound.Error(), nil)
148+
ctx.JSON(http.StatusBadRequest, res)
149+
return
150+
}
151+
152+
var req dto.PaginationRequest
153+
if err := ctx.ShouldBind(&req); err != nil {
154+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
155+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
156+
return
157+
}
158+
159+
result, err := c.userService.GetUserPosts(ctx.Request.Context(), username, req)
160+
if err != nil {
161+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER_POSTS, err.Error(), nil)
162+
ctx.JSON(http.StatusBadRequest, res)
163+
return
164+
}
165+
166+
res := utils.Response{
167+
Status: true,
168+
Message: dto.MESSAGE_SUCCESS_GET_ALL_POSTS,
169+
Data: result.Data,
170+
Meta: result.PaginationResponse,
171+
}
172+
173+
ctx.JSON(http.StatusOK, res)
174+
}

dto/user_dto.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const (
1717
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
1818
MESSAGE_FAILED_UPDATE_USER = "failed update user"
1919
MESSAGE_FAILED_USERNAME_EXISTS = "failed get username"
20+
MESSAGE_FAILED_GET_USER_POSTS = "failed get user posts"
2021

2122
// Success
2223
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
2324
MESSAGE_SUCCESS_GET_USER = "success get user"
2425
MESSAGE_SUCCESS_LOGIN = "success login"
2526
MESSAGE_SUCCESS_UPDATE_USER = "success update user"
2627
MESSAGE_SUCCESS_USERNAME_AVAILABLE = "username available"
28+
MESSAGE_SUCCESS_GET_USER_POSTS = "success get user posts"
2729
)
2830

2931
var (

provider/user_provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ func ProvideUserDependencies(injector *do.Injector) {
1515

1616
// Repository
1717
userRepository := repository.NewUserRepository(db)
18+
postRepository := repository.NewPostRepository(db)
1819

1920
// Service
20-
userService := service.NewUserService(userRepository, jwtService)
21+
userService := service.NewUserService(userRepository, postRepository, jwtService)
2122

2223
// Controller
2324
do.Provide(injector, func(i *do.Injector) (controller.UserController, error) {

repository/post_repository.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
DeletePostById(ctx context.Context, tx *gorm.DB, postId uint64) error
1616
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uint64, post entity.Post) (entity.Post, error)
1717
GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
18+
GetAllPostsWithPaginationByUsername(ctx context.Context, tx *gorm.DB, username string, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
1819
GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uint64, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error)
1920
UpdateLikesCount(ctx context.Context, tx *gorm.DB, postId uint64, count int) error
2021
}
@@ -173,3 +174,39 @@ func (r *postRepository) UpdateLikesCount(ctx context.Context, tx *gorm.DB, post
173174

174175
return nil
175176
}
177+
178+
func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context, tx *gorm.DB, username string, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error) {
179+
if tx == nil {
180+
tx = r.db
181+
}
182+
183+
var posts []entity.Post
184+
var err error
185+
var count int64
186+
187+
req.Default()
188+
189+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id IS NULL").Where("\"User\".username = ?", username).Order("created_at DESC")
190+
if req.Search != "" {
191+
query = query.Where("text LIKE ?", "%"+req.Search+"%")
192+
}
193+
194+
if err := query.Count(&count).Error; err != nil {
195+
return dto.GetAllPostsRepositoryResponse{}, err
196+
}
197+
198+
if err := query.Scopes(Paginate(req)).Find(&posts).Error; err != nil {
199+
return dto.GetAllPostsRepositoryResponse{}, err
200+
}
201+
202+
totalPage := TotalPage(count, int64(req.PerPage))
203+
return dto.GetAllPostsRepositoryResponse{
204+
Posts: posts,
205+
PaginationResponse: dto.PaginationResponse{
206+
Page: req.Page,
207+
PerPage: req.PerPage,
208+
Count: count,
209+
MaxPage: totalPage,
210+
},
211+
}, err
212+
}

routes/user_route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func User(route *gin.Engine, injector *do.Injector) {
2121
routes.POST("/check-username", userController.CheckUsername)
2222
routes.GET("/me", middleware.Authenticate(jwtService), userController.Me)
2323
routes.GET("/:username", userController.GetUserByUsername)
24+
routes.GET("/:username/posts", userController.GetUserPosts)
2425
routes.PATCH("/update", middleware.Authenticate(jwtService), userController.UpdateUser)
2526
}
2627
}

service/user_service.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ type (
1919
Verify(ctx context.Context, req dto.UserLoginRequest) (dto.UserLoginResponse, error)
2020
GetUserByUsername(ctx context.Context, username string) (dto.UserResponse, error)
2121
UpdateUser(ctx context.Context, userId string, req dto.UserProfileUpdateRequest) (dto.UserResponse, error)
22+
GetUserPosts(ctx context.Context, username string, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
2223
}
2324

2425
userService struct {
2526
userRepo repository.UserRepository
27+
postRepo repository.PostRepository
2628
jwtService JWTService
2729
}
2830
)
2931

30-
func NewUserService(userRepo repository.UserRepository, jwtService JWTService) UserService {
32+
func NewUserService(userRepo repository.UserRepository, postRepo repository.PostRepository, jwtService JWTService) UserService {
3133
return &userService{
3234
userRepo: userRepo,
35+
postRepo: postRepo,
3336
jwtService: jwtService,
3437
}
3538
}
@@ -161,3 +164,39 @@ func (s *userService) UpdateUser(ctx context.Context, userId string, req dto.Use
161164
ImageUrl: userUpdate.ImageUrl,
162165
}, nil
163166
}
167+
168+
func (s *userService) GetUserPosts(ctx context.Context, username string, req dto.PaginationRequest) (dto.PostPaginationResponse, error) {
169+
dataWithPaginate, err := s.postRepo.GetAllPostsWithPaginationByUsername(ctx, nil, username, req)
170+
if err != nil {
171+
return dto.PostPaginationResponse{}, err
172+
}
173+
174+
var data []dto.PostResponse
175+
for _, post := range dataWithPaginate.Posts {
176+
datum := dto.PostResponse{
177+
ID: post.ID,
178+
Text: post.Text,
179+
TotalLikes: post.TotalLikes,
180+
ParentID: post.ParentID,
181+
User: dto.UserResponse{
182+
ID: post.UserID.String(),
183+
Name: post.User.Name,
184+
Bio: post.User.Bio,
185+
UserName: post.User.Username,
186+
ImageUrl: post.User.ImageUrl,
187+
},
188+
}
189+
190+
data = append(data, datum)
191+
}
192+
193+
return dto.PostPaginationResponse{
194+
Data: data,
195+
PaginationResponse: dto.PaginationResponse{
196+
Page: dataWithPaginate.Page,
197+
PerPage: dataWithPaginate.PerPage,
198+
MaxPage: dataWithPaginate.MaxPage,
199+
Count: dataWithPaginate.Count,
200+
},
201+
}, nil
202+
}

0 commit comments

Comments
 (0)