Skip to content

Commit f49bf40

Browse files
committed
feat: add IsDeleted field to PostResponse and related structures
1 parent 60f0ac9 commit f49bf40

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

dto/post_dto.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ type (
3838
}
3939

4040
PostResponse struct {
41-
ID uint64 `json:"id"`
42-
Text string `json:"text"`
43-
TotalLikes uint64 `json:"total_likes"`
44-
ParentID *uint64 `json:"parent_id"`
45-
User UserResponse `json:"user"`
46-
Replies []PostResponse `json:"replies,omitempty"`
41+
ID uint64 `json:"id"`
42+
Text string `json:"text"`
43+
TotalLikes uint64 `json:"total_likes"`
44+
ParentID *uint64 `json:"parent_id"`
45+
IsDeleted bool `json:"is_deleted"`
46+
User UserResponse `json:"user"`
47+
}
48+
49+
PostWithRepliesResponse struct {
50+
PostResponse
51+
Replies []PostResponse `json:"replies"`
4752
}
4853

4954
PostUpdateRequest struct {
@@ -56,7 +61,7 @@ type (
5661
}
5762

5863
PostRepliesPaginationResponse struct {
59-
Data PostResponse `json:"data"`
64+
Data PostWithRepliesResponse `json:"data"`
6065
PaginationResponse
6166
}
6267

repository/post_repository.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (r *postRepository) GetAllPostsWithPagination(ctx context.Context, tx *gorm
102102

103103
req.Default()
104104

105-
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id IS NULL").Order("created_at DESC")
105+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Unscoped().Where("posts.parent_id IS NULL").Order("created_at DESC")
106106
if req.Search != "" {
107107
query = query.Where("text LIKE ?", "%"+req.Search+"%")
108108
}
@@ -138,7 +138,7 @@ func (r *postRepository) GetAllPostRepliesWithPagination(ctx context.Context, tx
138138

139139
req.Default()
140140

141-
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id = ?", postId).Order("created_at DESC")
141+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Unscoped().Where("posts.parent_id = ?", postId).Order("created_at DESC")
142142
if req.Search != "" {
143143
query = query.Where("text LIKE ?", "%"+req.Search+"%")
144144
}
@@ -147,7 +147,7 @@ func (r *postRepository) GetAllPostRepliesWithPagination(ctx context.Context, tx
147147
return dto.GetAllRepliesRepositoryResponse{}, err
148148
}
149149

150-
if err := query.Scopes(Paginate(req)).Find(&replies).Error; err != nil {
150+
if err := query.Scopes(Paginate(req)).Unscoped().Find(&replies).Error; err != nil {
151151
return dto.GetAllRepliesRepositoryResponse{}, err
152152
}
153153

@@ -186,7 +186,7 @@ func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context
186186

187187
req.Default()
188188

189-
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id IS NULL").Where("\"User\".username = ?", username)
189+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Unscoped().Where("posts.parent_id IS NULL").Where("\"User\".username = ?", username)
190190
if req.IsLiked {
191191
query = query.Joins("INNER JOIN likes ON likes.post_id = posts.id AND likes.user_id = posts.user_id")
192192
}
@@ -203,7 +203,7 @@ func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context
203203
if err := query.Scopes(Paginate(dto.PaginationRequest{
204204
Page: req.Page,
205205
PerPage: req.PerPage,
206-
})).Find(&posts).Error; err != nil {
206+
})).Unscoped().Find(&posts).Error; err != nil {
207207
return dto.GetAllPostsRepositoryResponse{}, err
208208
}
209209

service/post_service.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,21 @@ func (s *postService) CreatePost(ctx context.Context, userId string, req dto.Pos
5757
return dto.PostResponse{}, dto.ErrGetUserById
5858
}
5959

60+
if result.DeletedAt.Valid {
61+
return dto.PostResponse{
62+
ID: result.ID,
63+
Text: "",
64+
TotalLikes: 0,
65+
IsDeleted: result.DeletedAt.Valid,
66+
ParentID: req.ParentID,
67+
}, nil
68+
}
69+
6070
return dto.PostResponse{
6171
ID: result.ID,
6272
Text: result.Text,
6373
TotalLikes: result.TotalLikes,
74+
IsDeleted: result.DeletedAt.Valid,
6475
ParentID: req.ParentID,
6576
User: dto.UserResponse{
6677
ID: user.ID.String(),
@@ -89,6 +100,7 @@ func (s *postService) GetPostById(ctx context.Context, postId uint64, req dto.Pa
89100
ID: reply.ID,
90101
Text: reply.Text,
91102
TotalLikes: reply.TotalLikes,
103+
IsDeleted: reply.DeletedAt.Valid,
92104
ParentID: reply.ParentID,
93105
User: dto.UserResponse{
94106
ID: reply.UserID.String(),
@@ -102,18 +114,25 @@ func (s *postService) GetPostById(ctx context.Context, postId uint64, req dto.Pa
102114
data = append(data, datum)
103115
}
104116

117+
if data == nil {
118+
data = make([]dto.PostResponse, 0)
119+
}
120+
105121
return dto.PostRepliesPaginationResponse{
106-
Data: dto.PostResponse{
107-
ID: post.ID,
108-
Text: post.Text,
109-
TotalLikes: post.TotalLikes,
110-
ParentID: post.ParentID,
111-
User: dto.UserResponse{
112-
ID: post.UserID.String(),
113-
Name: post.User.Name,
114-
Bio: post.User.Bio,
115-
UserName: post.User.Username,
116-
ImageUrl: post.User.ImageUrl,
122+
Data: dto.PostWithRepliesResponse{
123+
PostResponse: dto.PostResponse{
124+
ID: post.ID,
125+
Text: post.Text,
126+
TotalLikes: post.TotalLikes,
127+
IsDeleted: post.DeletedAt.Valid,
128+
ParentID: post.ParentID,
129+
User: dto.UserResponse{
130+
ID: post.UserID.String(),
131+
Name: post.User.Name,
132+
Bio: post.User.Bio,
133+
UserName: post.User.Username,
134+
ImageUrl: post.User.ImageUrl,
135+
},
117136
},
118137
Replies: data,
119138
},
@@ -160,6 +179,7 @@ func (s *postService) UpdatePostById(ctx context.Context, userId string, postId
160179
ID: result.ID,
161180
Text: result.Text,
162181
TotalLikes: result.TotalLikes,
182+
IsDeleted: result.DeletedAt.Valid,
163183
ParentID: result.ParentID,
164184
User: dto.UserResponse{
165185
ID: result.UserID.String(),
@@ -183,6 +203,7 @@ func (s *postService) GetAllPosts(ctx context.Context, req dto.PaginationRequest
183203
ID: post.ID,
184204
Text: post.Text,
185205
TotalLikes: post.TotalLikes,
206+
IsDeleted: post.DeletedAt.Valid,
186207
ParentID: post.ParentID,
187208
User: dto.UserResponse{
188209
ID: post.UserID.String(),

service/user_service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ func (s *userService) GetUserPosts(ctx context.Context, username string, req dto
177177
ID: post.ID,
178178
Text: post.Text,
179179
TotalLikes: post.TotalLikes,
180+
IsDeleted: post.DeletedAt.Valid,
180181
ParentID: post.ParentID,
181182
User: dto.UserResponse{
182183
ID: post.UserID.String(),

0 commit comments

Comments
 (0)