Skip to content

Commit

Permalink
Merge pull request #675 from bounswe/bugfix/BE/get-comment-isliked-field
Browse files Browse the repository at this point in the history
get comment response has isliked isdisliked fields for the user
  • Loading branch information
omersafakbebek authored Dec 10, 2023
2 parents ed59dd3 + c94a715 commit 93e15dc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ export class GetCommentResponseDto {
@ApiProperty()
@IsNumber()
dislikeCount: number;

@ApiProperty()
@IsBoolean()
isLiked: boolean;

@ApiProperty()
@IsBoolean()
isDisLiked: boolean;
}
46 changes: 46 additions & 0 deletions ludos/backend/src/services/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EditCommentDto } from '../dtos/comment/request/edit-comment.dto';
import { GetCommentResponseDto } from '../dtos/comment/response/get-comment.response.dto';
import { UserRepository } from '../repositories/user.repository';
import { CommentRepository } from '../repositories/comment.repository';
import { Comment } from 'entities/comment.entity';

@Injectable()
export class CommentService {
Expand Down Expand Up @@ -33,6 +34,27 @@ export class CommentService {
HttpStatus.FORBIDDEN,
);
}

let checkLike = (comment: Comment) => {
let isLiked = false;
comment.likedUsers.forEach(likedUser => {
if (likedUser.id == userId) {
isLiked = true;
}
});
return isLiked;
};

let checkDisLike = (comment: Comment) => {
let isDisliked = false;
comment.dislikedUsers.forEach(dislikedUser => {
if (dislikedUser.id == userId) {
isDisliked = true;
}
});
return isDisliked;
};

return {
author: comment.author,
timestamp: comment.timestamp,
Expand All @@ -41,6 +63,8 @@ export class CommentService {
parentId: comment.parentId,
likeCount: comment.likedUsers.length,
dislikeCount: comment.dislikedUsers.length,
isLiked: checkLike(comment),
isDisLiked: checkDisLike(comment),
};
}

Expand All @@ -57,11 +81,33 @@ export class CommentService {
);
}

let checkLike = (comment: Comment) => {
let isLiked = false;
comment.likedUsers.forEach(likedUser => {
if (likedUser.id == userId) {
isLiked = true;
}
});
return isLiked;
};

let checkDisLike = (comment: Comment) => {
let isDisliked = false;
comment.dislikedUsers.forEach(dislikedUser => {
if (dislikedUser.id == userId) {
isDisliked = true;
}
});
return isDisliked;
};

return (await this.commentRepository.findCommentsByParent(parentId)).map(
(comment) => {
return {
likeCount: comment.likedUsers.length,
dislikeCount: comment.dislikedUsers.length,
isLiked: checkLike(comment),
isDisLiked: checkDisLike(comment),
...comment,
};
},
Expand Down

0 comments on commit 93e15dc

Please sign in to comment.