@@ -2,9 +2,7 @@ package services
22
33import (
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
229223func (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
273260func (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