Skip to content

Commit d1b9627

Browse files
committed
feat: add delete post by id endpoint
1 parent f47c659 commit d1b9627

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

controller/post_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type (
1414
PostController interface {
1515
CreatePost(ctx *gin.Context)
1616
GetPostById(ctx *gin.Context)
17+
DeletePostById(ctx *gin.Context)
1718
}
1819

1920
postController struct {
@@ -64,3 +65,17 @@ func (c *postController) GetPostById(ctx *gin.Context) {
6465
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_POST_BY_ID, result)
6566
ctx.JSON(http.StatusOK, res)
6667
}
68+
69+
func (c *postController) DeletePostById(ctx *gin.Context) {
70+
postId, err := uuid.Parse(ctx.Param("post_id"))
71+
if err != nil {
72+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
73+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
74+
return
75+
}
76+
77+
err = c.postService.DeletePostById(ctx.Request.Context(), postId)
78+
79+
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_POST, nil)
80+
ctx.JSON(http.StatusOK, res)
81+
}

dto/post_dto.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ const (
1515
// Succcess
1616
MESSAGE_SUCCESS_CREATE_POST = "success create post"
1717
MESSAGE_SUCCESS_GET_POST_BY_ID = "success get post by id"
18+
MESSAGE_SUCCESS_DELETE_POST = "success delete post"
1819
)
1920

2021
var (
21-
ErrCreatePost = errors.New("failed to create post")
22-
ErrGetPostById = errors.New("post not found")
23-
ErrParseParentID = errors.New("failed to parse parent id")
22+
ErrCreatePost = errors.New("failed to create post")
23+
ErrGetPostById = errors.New("post not found")
24+
ErrParseParentID = errors.New("failed to parse parent id")
25+
ErrDeletePostById = errors.New("failed to delete post")
2426
)
2527

2628
type (

repository/post_repository.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type (
1212
PostRepository interface {
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)
15+
DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error
1516
}
1617

1718
postRepository struct {
@@ -60,3 +61,15 @@ func (r *postRepository) GetPostById(ctx context.Context, tx *gorm.DB, postId uu
6061

6162
return post, nil
6263
}
64+
65+
func (r *postRepository) DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error {
66+
if tx == nil {
67+
tx = r.db
68+
}
69+
70+
if err := tx.WithContext(ctx).Delete(&entity.Post{}, postId).Error; err != nil {
71+
return err
72+
}
73+
74+
return nil
75+
}

routes/post_route.go

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

service/post_service.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type (
1313
PostService interface {
1414
CreatePost(ctx context.Context, req dto.PostCreateRequest) (dto.PostResponse, error)
1515
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, error)
16+
DeletePostById(ctx context.Context, postId uuid.UUID) error
1617
}
1718

1819
postService struct {
@@ -92,3 +93,16 @@ func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.Po
9293
},
9394
}, nil
9495
}
96+
97+
func (s *postService) DeletePostById(ctx context.Context, postId uuid.UUID) error {
98+
_, err := s.postRepo.GetPostById(ctx, nil, postId)
99+
if err != nil {
100+
return dto.ErrGetPostById
101+
}
102+
103+
if err := s.postRepo.DeletePostById(ctx, nil, postId); err != nil {
104+
return dto.ErrDeletePostById
105+
}
106+
107+
return nil
108+
}

0 commit comments

Comments
 (0)