Skip to content

Commit 84dfe26

Browse files
committed
fix: change userId in create post using context
1 parent d1b9627 commit 84dfe26

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

controller/post_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ func NewPostController(ps service.PostService) PostController {
3030

3131
func (c *postController) CreatePost(ctx *gin.Context) {
3232
var post dto.PostCreateRequest
33+
userId := ctx.GetString("user_id")
34+
3335
if err := ctx.ShouldBind(&post); err != nil {
3436
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
3537
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
3638
return
3739
}
3840

39-
result, err := c.postService.CreatePost(ctx.Request.Context(), post)
41+
result, err := c.postService.CreatePost(ctx.Request.Context(), userId, post)
4042
if err != nil {
4143
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_POST, err.Error(), nil)
4244
ctx.JSON(http.StatusBadRequest, res)

dto/post_dto.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var (
2828
type (
2929
PostCreateRequest struct {
3030
Text string `json:"text" form:"text" binding:"required"`
31-
UserID string `json:"user_id" form:"user_id" binding:"required"`
3231
ParentID *uuid.UUID `json:"parent_id," form:"parent_id"`
3332
}
3433

service/post_service.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type (
1313
PostService interface {
14-
CreatePost(ctx context.Context, req dto.PostCreateRequest) (dto.PostResponse, error)
14+
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
1717
}
@@ -31,27 +31,16 @@ func NewPostService(userRepo repository.UserRepository, postRepo repository.Post
3131
}
3232
}
3333

34-
func (s *postService) CreatePost(ctx context.Context, req dto.PostCreateRequest) (dto.PostResponse, error) {
35-
user, err := s.userRepo.GetUserById(ctx, nil, req.UserID)
36-
37-
if err != nil {
38-
return dto.PostResponse{}, dto.ErrGetUserById
39-
}
40-
34+
func (s *postService) CreatePost(ctx context.Context, userId string, req dto.PostCreateRequest) (dto.PostResponse, error) {
4135
if req.ParentID != nil {
4236
_, err := s.postRepo.GetPostById(ctx, nil, *req.ParentID)
4337
if err != nil {
4438
return dto.PostResponse{}, dto.ErrGetPostById
4539
}
4640
}
4741

48-
if err != nil {
49-
return dto.PostResponse{}, dto.ErrParseParentID
50-
}
51-
5242
post := entity.Post{
5343
Text: req.Text,
54-
UserID: user.ID,
5544
ParentID: req.ParentID,
5645
}
5746

@@ -60,6 +49,11 @@ func (s *postService) CreatePost(ctx context.Context, req dto.PostCreateRequest)
6049
return dto.PostResponse{}, dto.ErrCreatePost
6150
}
6251

52+
user, err := s.userRepo.GetUserById(ctx, nil, userId)
53+
if err != nil {
54+
return dto.PostResponse{}, dto.ErrGetUserById
55+
}
56+
6357
return dto.PostResponse{
6458
ID: result.ID.String(),
6559
Text: result.Text,

0 commit comments

Comments
 (0)