Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class ChallengeConverter {

Expand Down Expand Up @@ -163,38 +164,68 @@ public static ChallengeCommentResponse commentChallenge(

public static ChallengeGetCommentCommand getCommentChallenge(
Long challengeId,
Long userId,
Pageable pageable
) {
return ChallengeGetCommentCommand.builder()
.challengeId(challengeId)
.userId(userId)
.pageable(pageable)
.build();
}

public static ChallengeGetCommentResponse getCommentChallenge(
Comment comment,
List<Comment> children
List<Comment> children,
Long userId,
Set<Long> likedCommentIds
) {
List<ChallengeGetCommentResponse> childResponses = new ArrayList<>();

for (Comment child : children) {
boolean isMine = false;
boolean isLiked = false;

if (child.getUser().getId().equals(userId)) {
isMine = true;
}

if (likedCommentIds.contains(child.getId())) {
isLiked = true;
}

childResponses.add(ChallengeGetCommentResponse.builder()
.commentId(child.getId())
.content(child.getContent())
.nickName(child.getUser().getNickname())
.profileImageUrl(child.getUser().getProfileImageUrl())
.likeCount(child.getLikeCount())
.children(null)
.isMine(isMine)
.isLiked(isLiked)
.build());
}

boolean isMine = false;
boolean isLiked = false;

if (comment.getUser().getId().equals(userId)) {
isMine = true;
}

if (likedCommentIds.contains(comment.getId())) {
isLiked = true;
}

return ChallengeGetCommentResponse.builder()
.commentId(comment.getId())
.content(comment.getContent())
.nickName(comment.getUser().getNickname())
.profileImageUrl(comment.getUser().getProfileImageUrl())
.likeCount(comment.getLikeCount())
.children(childResponses)
.isMine(isMine)
.isLiked(isLiked)
.build();
}

Expand Down Expand Up @@ -242,29 +273,6 @@ public static ChallengeDeleteCommand deleteChallenge(
.build();
}


public static ChallengeMyCommand myChallenge(
Long userId,
Pageable pageable
) {
return ChallengeMyCommand.builder()
.userId(userId)
.pageable(pageable)
.build();
}

public static ChallengeOthersCommand othersChallenge(
Long userId,
String nickname,
Pageable pageable
) {
return ChallengeOthersCommand.builder()
.userId(userId)
.nickname(nickname)
.pageable(pageable)
.build();
}

public static ChallengeViewCommand viewChallenge(
Long userId,
Pageable trendingPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@Builder
public record ChallengeGetCommentCommand(
Long challengeId,
Long userId,
Pageable pageable
) {
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public record ChallengeGetCommentResponse(
String profileImageUrl,
String content,
Integer likeCount,
Boolean isMine,
Boolean isLiked,
List<ChallengeGetCommentResponse> children
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.loopon.challenge.application.converter.ChallengeConverter;
import com.loopon.challenge.application.dto.command.*;
import com.loopon.challenge.application.dto.response.*;
import com.loopon.challenge.domain.Challenge;
import com.loopon.challenge.domain.ChallengeHashtag;
import com.loopon.challenge.domain.ChallengeImage;
import com.loopon.challenge.domain.Comment;
import com.loopon.challenge.domain.*;
import com.loopon.challenge.domain.repository.ChallengeRepository;
import com.loopon.global.domain.ErrorCode;
import com.loopon.global.domain.dto.SliceResponse;
Expand All @@ -23,13 +20,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Stream;

@Service
Expand Down Expand Up @@ -73,56 +64,53 @@ public ChallengeGetResponse getChallenge(

@Transactional(readOnly = true)
public SliceResponse<ChallengeGetCommentResponse> getCommentChallenge(
ChallengeGetCommentCommand commandDto
ChallengeGetCommentCommand dto
) {
Challenge challenge = challengeRepository.findById(commandDto.challengeId())
Challenge challenge = challengeRepository.findById(dto.challengeId())
.orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND));

Slice<Comment> comments = challengeRepository.findCommentsWithUserByChallengeId(challenge.getId(), commandDto.pageable());

List<Long> parentIds = new ArrayList<>();
for (Comment comment : comments.getContent()) {
parentIds.add(comment.getId());
}
Slice<Comment> comments = challengeRepository.findCommentsWithUserByChallengeId(challenge.getId(), dto.pageable());

List<Long> parentIds = comments.getContent().stream()
.map(Comment::getId)
.toList();

List<Comment> children = challengeRepository.findAllCommentWithUserByParentIdIn(parentIds);


List<Long> allCommentIds = new ArrayList<>(parentIds);
children.forEach(c -> allCommentIds.add(c.getId()));

List<CommentLike> commentLikes = challengeRepository.findAllCommentLikeByUserIdAndCommentIdIn(dto.userId(), allCommentIds);

Set<Long> likedCommentIds = new HashSet<>();

for (CommentLike like : commentLikes) {
Long commentId = like.getComment().getId();
likedCommentIds.add(commentId);
}


Map<Long, List<Comment>> childrenMap = new HashMap<>();

for (Comment child : children) {
Long parentId = child.getParent().getId();

childrenMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
}


return SliceResponse.from(comments.map(comment ->
ChallengeConverter.getCommentChallenge(comment, childrenMap.getOrDefault(comment.getId(), new ArrayList<>()))
ChallengeConverter.getCommentChallenge(
comment,
childrenMap.getOrDefault(comment.getId(), Collections.emptyList()),
dto.userId(),
likedCommentIds
)
));
}

@Transactional(readOnly = true)
public SliceResponse<ChallengePreviewResponse> myChallenge(
ChallengeMyCommand commandDto
) {
User user = userRepository.findById(commandDto.userId())
.orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND));

return SliceResponse.from(challengeRepository.findViewByUserId(user.getId(), commandDto.pageable()));
}

@Transactional(readOnly = true)
public SliceResponse<ChallengePreviewResponse> othersChallenge(
ChallengeOthersCommand commandDto
) {
User myself = userRepository.findById(commandDto.userId())
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
User target = userRepository.findByNickname(commandDto.nickname())
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

checkAllowed(target, myself);

return SliceResponse.from(challengeRepository.findViewByUserId(target.getId(), commandDto.pageable()));
}

@Transactional(readOnly = true)
public ChallengeCombinedViewResponse viewChallenge(
ChallengeViewCommand commandDto
Expand All @@ -133,7 +121,7 @@ public ChallengeCombinedViewResponse viewChallenge(

LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(3);
Slice<Challenge> trendingChallenges = challengeRepository.findTrendingChallenges(
threeDaysAgo, commandDto.trendingPage());
threeDaysAgo, user.getId(), commandDto.trendingPage());


List<Long> trendingIds = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.loopon.challenge.domain.repository;

import com.loopon.challenge.domain.*;
import org.springframework.data.domain.Page;
import com.loopon.challenge.application.dto.response.ChallengePreviewResponse;
import com.loopon.challenge.domain.Challenge;
import com.loopon.challenge.domain.ChallengeHashtag;
import com.loopon.challenge.domain.ChallengeHashtagId;
import com.loopon.challenge.domain.ChallengeImage;
import com.loopon.challenge.domain.ChallengeLike;
import com.loopon.challenge.domain.Comment;
import com.loopon.challenge.domain.CommentLike;
import com.loopon.challenge.domain.Hashtag;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
Expand All @@ -28,35 +24,25 @@ public interface ChallengeRepository {

Long save(Challenge challenge);

ChallengeHashtagId saveChallengeHashtag(ChallengeHashtag challengeHashtag);

Long saveChallengeImage(ChallengeImage challengeImage);

Hashtag saveHashtag(Hashtag hashtag);

List<ChallengeHashtag> findAllChallengeHashtagByChallengeId(Long id);

List<ChallengeHashtag> findAllChallengeHashtagWithHashtagByChallengeId(Long id);

Optional<Hashtag> findHashtagByName(String name);

List<Hashtag> findAllHashtagByNameIn(List<String> hashtagList);

List<ChallengeImage> findAllImageByChallengeId(Long challengeId);

Optional<Challenge> findById(Long challengeId);

void deleteChallengeHashtag(ChallengeHashtag challengeHashtag);

void deleteAllByExpeditionId(Long expeditionId);

Slice<Challenge> findAllWithJourneyAndUserByExpeditionId(Long expeditionId, Pageable pageable);

Boolean existsChallengeLikeByIdAndUserId(Long challengeId, Long userId);

Page<ChallengeImage> findThumbnailsByUserId(Long userId, Pageable pageable);

void saveAllHashtags(List<Hashtag> hashtagList);

Optional<ChallengeLike> findChallengeLikeByUserIdAndId(Long userId, Long challengeId);

Expand All @@ -80,11 +66,9 @@ public interface ChallengeRepository {

void delete(Challenge challenge);

Slice<ChallengePreviewResponse> findViewByUserId(Long userId, Pageable pageable);

List<Comment> findAllCommentWithUserByParentIdIn(List<Long> parentIds);

Slice<Challenge> findTrendingChallenges(LocalDateTime threeDaysAgo, Pageable pageable);
Slice<Challenge> findTrendingChallenges(LocalDateTime threeDaysAgo, Long userId, Pageable pageable);

Slice<Challenge> findFriendsChallenges(List<Long> friendsIds, List<Long> trendingIds, Pageable pageable);

Expand All @@ -95,4 +79,6 @@ public interface ChallengeRepository {
Boolean existsCommentLikeByCommentIdAndUserId(Long commentId, Long userId);

Slice<Challenge> findAllWithJourneyAndUserByUserId(Long userId, Pageable pageable);

List<CommentLike> findAllCommentLikeByUserIdAndCommentIdIn(Long userId, List<Long> commentIds);
}
Loading