Skip to content

Commit 7636244

Browse files
committed
feat: add edit post endpoint
1 parent 84dfe26 commit 7636244

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

controller/post_controller.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"fmt"
45
"net/http"
56

67
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
@@ -15,6 +16,7 @@ type (
1516
CreatePost(ctx *gin.Context)
1617
GetPostById(ctx *gin.Context)
1718
DeletePostById(ctx *gin.Context)
19+
UpdatePostById(ctx *gin.Context)
1820
}
1921

2022
postController struct {
@@ -38,6 +40,8 @@ func (c *postController) CreatePost(ctx *gin.Context) {
3840
return
3941
}
4042

43+
fmt.Println("userId", userId)
44+
4145
result, err := c.postService.CreatePost(ctx.Request.Context(), userId, post)
4246
if err != nil {
4347
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_POST, err.Error(), nil)
@@ -81,3 +85,31 @@ func (c *postController) DeletePostById(ctx *gin.Context) {
8185
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_POST, nil)
8286
ctx.JSON(http.StatusOK, res)
8387
}
88+
89+
func (c *postController) UpdatePostById(ctx *gin.Context) {
90+
var post dto.PostUpdateRequest
91+
userId := ctx.GetString("user_id")
92+
93+
if err := ctx.ShouldBind(&post); err != nil {
94+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
95+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
96+
return
97+
}
98+
99+
postId, err := uuid.Parse(ctx.Param("post_id"))
100+
if err != nil {
101+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
102+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
103+
return
104+
}
105+
106+
result, err := c.postService.UpdatePostById(ctx.Request.Context(), userId, postId, post)
107+
if err != nil {
108+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_POST, err.Error(), nil)
109+
ctx.JSON(http.StatusBadRequest, res)
110+
return
111+
}
112+
113+
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_POST, result)
114+
ctx.JSON(http.StatusOK, res)
115+
}

dto/post_dto.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ const (
1111
MESSAGE_FAILED_GET_POST_DATA_FROM_BODY = "failed get data from body"
1212
MESSAGE_FAILED_CREATE_POST = "failed create post"
1313
MESSAGE_FAILED_GET_POST_ID = "failed get post id"
14+
MESSAGE_FAILED_UPDATE_POST = "failed update post"
1415

1516
// Succcess
1617
MESSAGE_SUCCESS_CREATE_POST = "success create post"
1718
MESSAGE_SUCCESS_GET_POST_BY_ID = "success get post by id"
1819
MESSAGE_SUCCESS_DELETE_POST = "success delete post"
20+
MESSAGE_SUCCESS_UPDATE_POST = "success update post"
1921
)
2022

2123
var (
2224
ErrCreatePost = errors.New("failed to create post")
2325
ErrGetPostById = errors.New("post not found")
2426
ErrParseParentID = errors.New("failed to parse parent id")
2527
ErrDeletePostById = errors.New("failed to delete post")
28+
ErrUpdatePostById = errors.New("failed to update post")
2629
)
2730

2831
type (
@@ -37,4 +40,8 @@ type (
3740
ParentID *uuid.UUID `json:"parent_id"`
3841
User UserResponse `json:"user"`
3942
}
43+
44+
PostUpdateRequest struct {
45+
Text string `json:"text" form:"text" binding:"required"`
46+
}
4047
)

dto/user_dto.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
ErrUsernameAlreadyExists = errors.New("username already exist")
2929
ErrUsernameNotFound = errors.New("username not found")
3030
ErrPasswordNotMatch = errors.New("password not match")
31+
ErrUnauthorized = errors.New("unauthorized")
3132
)
3233

3334
type (

repository/post_repository.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type (
1313
CreatePost(ctx context.Context, tx *gorm.DB, post entity.Post) (entity.Post, error)
1414
GetPostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) (entity.Post, error)
1515
DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error
16+
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID, post entity.Post) (entity.Post, error)
1617
}
1718

1819
postRepository struct {
@@ -73,3 +74,15 @@ func (r *postRepository) DeletePostById(ctx context.Context, tx *gorm.DB, postId
7374

7475
return nil
7576
}
77+
78+
func (r *postRepository) UpdatePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID, post entity.Post) (entity.Post, error) {
79+
if tx == nil {
80+
tx = r.db
81+
}
82+
83+
if err := tx.WithContext(ctx).Model(&entity.Post{}).Where("id = ?", postId).Updates(post).Error; err != nil {
84+
return entity.Post{}, err
85+
}
86+
87+
return post, nil
88+
}

routes/post_route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ func Post(route *gin.Engine, injector *do.Injector) {
1919
routes.POST("/", middleware.Authenticate(jwtService), postController.CreatePost)
2020
routes.GET("/:post_id", postController.GetPostById)
2121
routes.DELETE("/:post_id", middleware.Authenticate(jwtService), postController.DeletePostById)
22+
routes.PATCH("/:post_id", middleware.Authenticate(jwtService), postController.UpdatePostById)
2223
}
2324
}

service/post_service.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type (
1414
CreatePost(ctx context.Context, userId string, req dto.PostCreateRequest) (dto.PostResponse, error)
1515
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, error)
1616
DeletePostById(ctx context.Context, postId uuid.UUID) error
17+
UpdatePostById(ctx context.Context, userId string, postId uuid.UUID, req dto.PostUpdateRequest) (dto.PostResponse, error)
1718
}
1819

1920
postService struct {
@@ -100,3 +101,34 @@ func (s *postService) DeletePostById(ctx context.Context, postId uuid.UUID) erro
100101

101102
return nil
102103
}
104+
105+
func (s *postService) UpdatePostById(ctx context.Context, userId string, postId uuid.UUID, req dto.PostUpdateRequest) (dto.PostResponse, error) {
106+
post, err := s.postRepo.GetPostById(ctx, nil, postId)
107+
if err != nil {
108+
return dto.PostResponse{}, dto.ErrGetPostById
109+
}
110+
111+
if post.UserID.String() != userId {
112+
return dto.PostResponse{}, dto.ErrUnauthorized
113+
}
114+
115+
post.Text = req.Text
116+
117+
result, err := s.postRepo.UpdatePostById(ctx, nil, postId, post)
118+
if err != nil {
119+
return dto.PostResponse{}, dto.ErrUpdatePostById
120+
}
121+
122+
return dto.PostResponse{
123+
ID: result.ID.String(),
124+
Text: result.Text,
125+
ParentID: result.ParentID,
126+
User: dto.UserResponse{
127+
ID: result.UserID.String(),
128+
Name: result.User.Name,
129+
Bio: result.User.Bio,
130+
UserName: result.User.Username,
131+
ImageUrl: result.User.ImageUrl,
132+
},
133+
}, nil
134+
}

0 commit comments

Comments
 (0)