Skip to content

Commit 96e9e33

Browse files
committed
feat: add get post by id endpoint
1 parent 50d21ff commit 96e9e33

5 files changed

Lines changed: 62 additions & 5 deletions

File tree

controller/post_controller.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"github.com/Lab-RPL-ITS/twitter-clone-api/service"
88
"github.com/Lab-RPL-ITS/twitter-clone-api/utils"
99
"github.com/gin-gonic/gin"
10+
"github.com/google/uuid"
1011
)
1112

1213
type (
1314
PostController interface {
14-
Create(ctx *gin.Context)
15+
CreatePost(ctx *gin.Context)
16+
GetPostById(ctx *gin.Context)
1517
}
1618

1719
postController struct {
@@ -25,7 +27,7 @@ func NewPostController(ps service.PostService) PostController {
2527
}
2628
}
2729

28-
func (c *postController) Create(ctx *gin.Context) {
30+
func (c *postController) CreatePost(ctx *gin.Context) {
2931
var post dto.PostCreateRequest
3032
if err := ctx.ShouldBind(&post); err != nil {
3133
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
@@ -43,3 +45,22 @@ func (c *postController) Create(ctx *gin.Context) {
4345
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_POST, result)
4446
ctx.JSON(http.StatusOK, res)
4547
}
48+
49+
func (c *postController) GetPostById(ctx *gin.Context) {
50+
postId, err := uuid.Parse(ctx.Param("post_id"))
51+
if err != nil {
52+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
53+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
54+
return
55+
}
56+
57+
result, err := c.postService.GetPostById(ctx.Request.Context(), postId)
58+
if err != nil {
59+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
60+
ctx.JSON(http.StatusBadRequest, res)
61+
return
62+
}
63+
64+
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_POST, result)
65+
ctx.JSON(http.StatusOK, res)
66+
}

dto/post_dto.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
// Failed
1111
MESSAGE_FAILED_GET_POST_DATA_FROM_BODY = "failed get data from body"
1212
MESSAGE_FAILED_CREATE_POST = "failed create post"
13+
MESSAGE_FAILED_GET_POST_ID = "failed get post id"
1314

1415
// Succcess
1516
MESSAGE_SUCCESS_CREATE_POST = "success create post"

repository/post_repository.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,19 @@ func (r *postRepository) GetPostById(ctx context.Context, tx *gorm.DB, postId uu
4343
}
4444

4545
var post entity.Post
46-
if err := tx.WithContext(ctx).Joins("JOIN users ON users.id = posts.user_id ").Where("posts.id = ?", postId).Take(&post).Error; err != nil {
47-
return entity.Post{}, err
46+
if err := tx.WithContext(ctx).Joins("User").Where("posts.id = ?", postId).Take(&post).Error; err != nil {
47+
return entity.Post{
48+
ID: post.ID,
49+
Text: post.Text,
50+
ParentID: post.ParentID,
51+
User: entity.User{
52+
ID: post.User.ID,
53+
Name: post.User.Name,
54+
Username: post.User.Username,
55+
Bio: post.User.Bio,
56+
ImageUrl: post.User.ImageUrl,
57+
},
58+
}, err
4859
}
4960

5061
return post, nil

routes/post_route.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func Post(route *gin.Engine, injector *do.Injector) {
1515

1616
routes := route.Group("/api/post")
1717
{
18-
routes.POST("/", middleware.Authenticate(jwtService), postController.Create)
18+
// Post
19+
routes.POST("/", middleware.Authenticate(jwtService), postController.CreatePost)
20+
routes.GET("/:post_id", postController.GetPostById)
1921
}
2022
}

service/post_service.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
77
"github.com/Lab-RPL-ITS/twitter-clone-api/entity"
88
"github.com/Lab-RPL-ITS/twitter-clone-api/repository"
9+
"github.com/google/uuid"
910
)
1011

1112
type (
1213
PostService interface {
1314
CreatePost(ctx context.Context, req dto.PostCreateRequest) (dto.PostResponse, error)
15+
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, error)
1416
}
1517

1618
postService struct {
@@ -70,3 +72,23 @@ func (s *postService) CreatePost(ctx context.Context, req dto.PostCreateRequest)
7072
},
7173
}, nil
7274
}
75+
76+
func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, error) {
77+
post, err := s.postRepo.GetPostById(ctx, nil, postId)
78+
if err != nil {
79+
return dto.PostResponse{}, dto.ErrGetPostById
80+
}
81+
82+
return dto.PostResponse{
83+
ID: post.ID.String(),
84+
Text: post.Text,
85+
ParentID: post.ParentID,
86+
User: dto.UserResponse{
87+
ID: post.UserID.String(),
88+
Name: post.User.Name,
89+
Bio: post.User.Bio,
90+
UserName: post.User.Username,
91+
ImageUrl: post.User.ImageUrl,
92+
},
93+
}, nil
94+
}

0 commit comments

Comments
 (0)