Skip to content

Commit 603ab1c

Browse files
fix unlike bug
1 parent 4c3fd25 commit 603ab1c

File tree

4 files changed

+25
-44
lines changed

4 files changed

+25
-44
lines changed

internal/handlers/comment.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ func (c *CommentHandler) LikeComment(ctx *gin.Context) {
231231
ctx.JSON(http.StatusOK, gin.H{"message": "comment liked successfully"})
232232
}
233233

234-
//TODO: fix unlike comment
235234
func (c *CommentHandler) UnlikeComment(ctx *gin.Context) {
236235
uidRaw, exists := ctx.Get("user_id")
237236
if !exists {

internal/repository/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type PostRepository interface {
5858
RemoveLike(userID, postID uint) error
5959
ListLikesByPostID(postID uint) ([]models.PostLike, error)
6060
HasUserLiked(userID, postID uint) (bool, error)
61+
CountLikes(postID uint) (int64, error)
62+
UpdateLikesCount(postID uint, count int) error
6163
}
6264

6365
type CommentRepository interface {

internal/repository/post.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,10 @@ func (r *postRepository) HasUserLiked(userID, postID uint) (bool, error) {
118118
return false, err
119119
}
120120
return count > 0, nil
121+
}
122+
123+
func (r *postRepository) UpdateLikesCount(postID uint, count int) error {
124+
return r.db.Model(&models.Post{}).
125+
Where("id = ?", postID).
126+
Update("likes_count", count).Error
121127
}

internal/services/post.go

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package services
22

33
import (
44
"errors"
5-
"strings"
65

7-
"github.com/lib/pq"
86
"github.com/nevzattalhaozcan/forgotten/internal/config"
97
"github.com/nevzattalhaozcan/forgotten/internal/models"
108
"github.com/nevzattalhaozcan/forgotten/internal/repository"
@@ -193,11 +191,6 @@ func (s *PostService) LikePost(userID, postID uint) error {
193191
return err
194192
}
195193

196-
like := &models.PostLike{
197-
UserID: userID,
198-
PostID: postID,
199-
}
200-
201194
hasLiked, err := s.postRepo.HasUserLiked(userID, postID)
202195
if err != nil {
203196
return err
@@ -206,26 +199,27 @@ func (s *PostService) LikePost(userID, postID uint) error {
206199
return errors.New("user has already liked this post")
207200
}
208201

209-
err = s.postRepo.AddLike(like)
202+
err = s.postRepo.AddLike(&models.PostLike{
203+
UserID: userID,
204+
PostID: postID,
205+
})
206+
if err != nil {
207+
return err
208+
}
209+
210+
count, err := s.postRepo.CountLikes(postID)
210211
if err != nil {
211-
if isUniqueConstraintError(err, "idx_user_post_like") {
212-
return errors.New("user has already liked this post")
213-
}
214212
return err
215213
}
216214

217-
// Only increment the count if the like was successfully added
218-
post.LikesCount++
215+
post.LikesCount = int(count)
219216
if err := s.postRepo.Update(post); err != nil {
220-
// If updating the count fails, we should remove the like to maintain consistency
221-
s.postRepo.RemoveLike(userID, postID)
222217
return err
223218
}
224219

225-
return nil
220+
return s.postRepo.UpdateLikesCount(postID, int(count))
226221
}
227222

228-
//TODO: fix success issue for the first unlike action
229223
func (s *PostService) UnlikePost(userID, postID uint) error {
230224
_, err := s.userRepo.GetByID(userID)
231225
if err != nil {
@@ -235,7 +229,7 @@ func (s *PostService) UnlikePost(userID, postID uint) error {
235229
return err
236230
}
237231

238-
post, err := s.postRepo.GetByID(postID)
232+
_, err = s.postRepo.GetByID(postID)
239233
if err != nil {
240234
if errors.Is(err, gorm.ErrRecordNotFound) {
241235
return errors.New("post not found")
@@ -251,23 +245,16 @@ func (s *PostService) UnlikePost(userID, postID uint) error {
251245
return errors.New("user has not liked this post")
252246
}
253247

254-
err = s.postRepo.RemoveLike(userID, postID)
255-
if err != nil {
256-
if errors.Is(err, gorm.ErrRecordNotFound) {
257-
return errors.New("user has not liked this post")
258-
}
248+
if err := s.postRepo.RemoveLike(userID, postID); err != nil {
259249
return err
260250
}
261251

262-
// Only decrement if the like was successfully removed and count is positive
263-
if post.LikesCount > 0 {
264-
post.LikesCount--
265-
if err := s.postRepo.Update(post); err != nil {
266-
return err
267-
}
252+
count, err := s.postRepo.CountLikes(postID)
253+
if err != nil {
254+
return err
268255
}
269256

270-
return nil
257+
return s.postRepo.UpdateLikesCount(postID, int(count))
271258
}
272259

273260
func (s *PostService) ListLikesByPostID(postID uint) ([]models.PostLike, error) {
@@ -289,16 +276,3 @@ func (s *PostService) ListLikesByPostID(postID uint) ([]models.PostLike, error)
289276

290277
return likes, nil
291278
}
292-
293-
func isUniqueConstraintError(err error, constraintName string) bool {
294-
if pqErr, ok := err.(*pq.Error); ok {
295-
// PostgreSQL unique constraint violation code is 23505
296-
if pqErr.Code == "23505" {
297-
// Check if the constraint name matches
298-
return strings.Contains(string(pqErr.Constraint), constraintName) ||
299-
strings.Contains(pqErr.Message, constraintName) ||
300-
strings.Contains(pqErr.Detail, constraintName)
301-
}
302-
}
303-
return false
304-
}

0 commit comments

Comments
 (0)