11package controller
22
33import (
4+ "fmt"
45 "net/http"
56
67 "github.com/Lab-RPL-ITS/twitter-clone-api/dto"
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+ }
0 commit comments