Skip to content

Commit

Permalink
Merge pull request #139 from dnd-side-project/fix/#135
Browse files Browse the repository at this point in the history
Fix /popular and /{wordId}/comments token error
  • Loading branch information
miraexhoi authored Sep 19, 2024
2 parents 7ae7359 + af6b6b2 commit 5918b3c
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.dnd.spaced.domain.comment.domain.repository.dto.request.CommentConditionDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.CommentInfoWithLikeDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentInfoDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentWithoutIsLikeDto;
import com.dnd.spaced.domain.word.domain.Word;
import com.dnd.spaced.domain.word.domain.repository.WordRepository;
import java.util.List;
Expand Down Expand Up @@ -112,23 +113,19 @@ public void processLike(String email, Long commentId) {
}

public List<MultipleCommentInfoDto> findAllBy(CommentConditionInfoDto dto) {
CommentConditionDto commentConditionDto = CommentRepositoryMapper.to(
dto.wordId(),
findAccountId(dto.email()),
dto.lastCommentId(),
dto.lastLikeCount(),
dto.pageable()
);
List<CommentInfoWithLikeDto> result = commentRepository.findAllBy(commentConditionDto);

return CommentServiceMapper.fromComment(result);
if (dto.email() != null) {
return findMemberComments(dto);
} else {
return findNonMemberComments(dto);
}
}

public List<MultiplePopularCommentInfoDto> findPopularAllBy(Pageable pageable, String email) {
Long accountId = findAccountId(email);
List<PopularCommentInfoDto> result = commentRepository.findPopularAllBy(pageable, accountId);

return CommentServiceMapper.fromPopularComment(result);
if (email != null) {
return findPopularAllForMember(pageable, email);
} else {
return findPopularAllForNonMember(pageable);
}
}

public List<LikedCommentDto> findAllByLiked(ReadCommentAllByLikedDto dto) {
Expand Down Expand Up @@ -157,6 +154,46 @@ public List<WrittenCommentDto> findAllByWritten(ReadCommentAllByWrittenDto dto)
return CommentServiceMapper.ofWritten(result, account);
}

private List<MultiplePopularCommentInfoDto> findPopularAllForMember(Pageable pageable, String email) {
Long accountId = findAccountId(email);
List<PopularCommentInfoDto> result = commentRepository.findPopularAllBy(pageable, accountId);

return CommentServiceMapper.fromPopularComment(result);
}

private List<MultiplePopularCommentInfoDto> findPopularAllForNonMember(Pageable pageable) {
List<PopularCommentWithoutIsLikeDto> result = commentRepository.findPopularAll(pageable);

return CommentServiceMapper.fromPopularCommentWithOutIsLike(result);
}

private List<MultipleCommentInfoDto> findMemberComments(CommentConditionInfoDto dto) {
CommentConditionDto commentConditionDto = CommentRepositoryMapper.toCommentConditionDto(
dto.wordId(),
findAccountId(dto.email()),
dto.lastCommentId(),
dto.lastLikeCount(),
dto.pageable()
);

List<CommentInfoWithLikeDto> result = commentRepository.findAllBy(commentConditionDto);
return CommentServiceMapper.fromComment(result);
}

private List<MultipleCommentInfoDto> findNonMemberComments(CommentConditionInfoDto dto) {
CommentConditionDto commentConditionDto = CommentRepositoryMapper.toCommentConditionDto(
dto.wordId(),
null,
dto.lastCommentId(),
dto.lastLikeCount(),
dto.pageable()
);

List<CommentInfoWithLikeDto> result = commentRepository.findAllBy(commentConditionDto);
List<CommentInfoWithLikeDto> nonMemberComments = CommentServiceMapper.toNonMemberCommentList(result);
return CommentServiceMapper.fromComment(nonMemberComments);
}

private Long findAccountId(String email) {
return accountRepository.findBy(email)
.map(Account::getId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.dnd.spaced.domain.comment.domain.repository.dto.request.FindCommentAllByWrittenConditionDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.CommentInfoWithLikeDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentInfoDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentWithoutIsLikeDto;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -58,6 +59,12 @@ public static List<MultiplePopularCommentInfoDto> fromPopularComment(List<Popula
.toList();
}

public static List<MultiplePopularCommentInfoDto> fromPopularCommentWithOutIsLike(List<PopularCommentWithoutIsLikeDto> dtos) {
return dtos.stream()
.map(MultiplePopularCommentInfoDto::from)
.toList();
}

public static FindCommentAllByLikedConditionDto ofLiked(Long accountId, Long lastCommentId, Pageable pageable) {
return new FindCommentAllByLikedConditionDto(accountId, lastCommentId, pageable);
}
Expand Down Expand Up @@ -85,4 +92,23 @@ public static ReadCommentAllByLikedDto ofLiked(String email, Long lastCommentId,
public static ReadCommentAllByWrittenDto ofWritten(String email, Long lastCommentId, Pageable pageable) {
return new ReadCommentAllByWrittenDto(email, lastCommentId, pageable);
}

public static List<CommentInfoWithLikeDto> toNonMemberCommentList(List<CommentInfoWithLikeDto> comments) {
return comments.stream()
.map(comment -> new CommentInfoWithLikeDto(
comment.commentId(),
comment.writerId(),
comment.wordId(),
comment.content(),
comment.likeCount(),
comment.createdAt(),
comment.updatedAt(),
comment.writerNickname(),
comment.writerProfileImage(),
comment.likeAccountId(),
false
))
.toList();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dnd.spaced.domain.comment.application.dto.response;

import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentInfoDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentWithoutIsLikeDto;
import java.time.LocalDateTime;

public record MultiplePopularCommentInfoDto(
Expand All @@ -27,6 +28,21 @@ public static MultiplePopularCommentInfoDto from(PopularCommentInfoDto dto) {
);
}

public static MultiplePopularCommentInfoDto from(PopularCommentWithoutIsLikeDto dto) {
PronunciationInfoDto pronunciationInfo = new PronunciationInfoDto(dto.pronunciation().getEnglish());

return new MultiplePopularCommentInfoDto(
dto.commentId(),
new WordInfoDto(dto.wordId(), dto.wordName(), dto.wordCategory().getName(), pronunciationInfo),
dto.content(),
dto.likeCount(),
dto.createdAt(),
dto.updatedAt(),
false
);
}


public record WordInfoDto(Long id, String name, String categoryName, PronunciationInfoDto pronunciationInfo) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dnd.spaced.domain.comment.domain.repository.dto.request.FindCommentAllByWrittenConditionDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.CommentInfoWithLikeDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentInfoDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentWithoutIsLikeDto;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
Expand All @@ -22,6 +23,8 @@ public interface CommentRepository {

List<PopularCommentInfoDto> findPopularAllBy(Pageable pageable, Long accountId);

List<PopularCommentWithoutIsLikeDto> findPopularAll(Pageable pageable);

boolean existsBy(Long commentId);

List<Comment> findAllByLiked(FindCommentAllByLikedConditionDto dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.dnd.spaced.domain.comment.domain.repository.dto.request.FindCommentAllByWrittenConditionDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.CommentInfoWithLikeDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentInfoDto;
import com.dnd.spaced.domain.comment.domain.repository.dto.response.PopularCommentWithoutIsLikeDto;
import com.dnd.spaced.domain.comment.domain.repository.util.CommentSortConditionConverter;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Projections;
Expand Down Expand Up @@ -51,30 +52,32 @@ public void delete(Comment comment) {
@Override
public List<CommentInfoWithLikeDto> findAllBy(CommentConditionDto dto) {
return queryFactory.select(
Projections.constructor(
CommentInfoWithLikeDto.class,
comment.id,
comment.accountId,
comment.wordId,
comment.content,
comment.likeCount,
comment.createdAt,
comment.updatedAt,
account.nickname,
account.profileImage,
like.id
)
)
.from(comment)
.join(account).on(comment.accountId.eq(account.id))
.leftJoin(like).on(comment.id.eq(like.commentId), like.accountId.eq(dto.accountId()))
.where(calculateFindAllBooleanExpression(dto), comment.wordId.eq(dto.wordId()))
.orderBy(
CommentSortConditionConverter.convert(findOrder(dto.pageable()))
.toArray(OrderSpecifier[]::new)
)
.limit(dto.pageable().getPageSize())
.fetch();
Projections.constructor(
CommentInfoWithLikeDto.class,
comment.id,
comment.accountId,
comment.wordId,
comment.content,
comment.likeCount,
comment.createdAt,
comment.updatedAt,
account.nickname,
account.profileImage,
like.id,
like.id.isNotNull()
)
)
.from(comment)
.join(account).on(comment.accountId.eq(account.id))
.leftJoin(like).on(comment.id.eq(like.commentId),
dto.accountId() != null ? like.accountId.eq(dto.accountId()) : like.accountId.isNull())
.where(calculateFindAllBooleanExpression(dto), comment.wordId.eq(dto.wordId()))
.orderBy(
CommentSortConditionConverter.convert(findOrder(dto.pageable()))
.toArray(OrderSpecifier[]::new)
)
.limit(dto.pageable().getPageSize())
.fetch();
}

@Override
Expand Down Expand Up @@ -106,6 +109,34 @@ public List<PopularCommentInfoDto> findPopularAllBy(Pageable pageable, Long acco
.fetch();
}

@Override
public List<PopularCommentWithoutIsLikeDto> findPopularAll(Pageable pageable) {
return queryFactory.select(
Projections.constructor(
PopularCommentWithoutIsLikeDto.class,
comment.id,
word.id,
word.name,
word.category,
word.pronunciation,
comment.content,
comment.likeCount,
comment.createdAt,
comment.updatedAt,
like.id
)
)
.from(comment)
.join(word).on(comment.wordId.eq(word.id))
.leftJoin(like).on(comment.id.eq(like.commentId))
.orderBy(
CommentSortConditionConverter.convert(findOrder(pageable))
.toArray(OrderSpecifier[]::new)
)
.limit(pageable.getPageSize())
.fetch();
}

@Override
public boolean existsBy(Long commentId) {
return commentCrudRepository.existsById(commentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ public static CommentConditionDto to(
) {
return new CommentConditionDto(wordId, accountId, lastCommentId, lastLikeCount, pageable);
}

public static CommentConditionDto toCommentConditionDto(
Long wordId,
Long accountId,
Long lastCommentId,
Integer lastLikeCount,
Pageable pageable) {
return new CommentConditionDto(wordId, accountId, lastCommentId, lastLikeCount, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public record CommentInfoWithLikeDto(
LocalDateTime updatedAt,
String writerNickname,
String writerProfileImage,
Long likeAccountId
Long likeAccountId,
boolean isLike
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.dnd.spaced.domain.comment.domain.repository.dto.response;

import com.dnd.spaced.domain.word.domain.Category;
import com.dnd.spaced.domain.word.domain.Pronunciation;

import java.time.LocalDateTime;

public record PopularCommentWithoutIsLikeDto (
Long commentId,
Long wordId,
String wordName,
Category wordCategory,
Pronunciation pronunciation,
String content,
int likeCount,
LocalDateTime createdAt,
LocalDateTime updatedAt,
Long likeAccountId
) {
}

0 comments on commit 5918b3c

Please sign in to comment.