Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public async likeComment(commentId: number, user: User) {
  2.     const commentEntity = await this.commentsRepository.findOne({
  3.       id: commentId,
  4.     });
  5.  
  6.     if (
  7.       (await commentEntity.likedByUsers).some(
  8.         existingUser => existingUser.id === user.id,
  9.       )
  10.     ) {
  11.       return { responseMessage: 'Comment added to Liked' };
  12.     }
  13.  
  14.     const usersLlikedComment = [...(await commentEntity.likedByUsers), user];
  15.  
  16.     commentEntity.likedByUsers = Promise.resolve(usersLlikedComment);
  17.     commentEntity.likes = (await commentEntity.likedByUsers).length;
  18.  
  19.     this.commentsRepository.save(commentEntity);
  20.  
  21.     return { responseMessage: 'Comment added to Liked' };
  22.   }
  23.  
  24. ///////////////////////////////////////////////////////////////////////////////////////////////
  25.  
  26.   public async unlikeComment(commentId: number, user: User) {
  27.     const commentEntity = await this.commentsRepository.findOne({
  28.       id: commentId,
  29.     });
  30.  
  31.     if (
  32.       !(await commentEntity.likedByUsers).some(
  33.         userFound => userFound.id === user.id,
  34.       )
  35.     ) {
  36.       return { responseMessage: 'Comment removed from Liked' };
  37.     }
  38.  
  39.     const usersLlikedComment = await commentEntity.likedByUsers;
  40.  
  41.     const usersWithoutCurrentUser = usersLlikedComment.filter(
  42.       userLiked => userLiked.id !== user.id,
  43.     );
  44.  
  45.     commentEntity.likedByUsers = Promise.resolve(usersWithoutCurrentUser);
  46.     commentEntity.likes = (await commentEntity.likedByUsers).length;
  47.  
  48.     this.commentsRepository.save(commentEntity);
  49.  
  50.     return { responseMessage: 'Comment removed from Liked' };
  51.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement