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
5 changes: 3 additions & 2 deletions src/main/java/itstime/reflog/community/domain/Community.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.BatchSize;

@Entity
@Getter
Expand All @@ -42,12 +43,12 @@ public class Community {

private String content; // 게시글 내용

@ElementCollection(fetch = FetchType.EAGER)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "community_post_types", joinColumns = @JoinColumn(name = "community_id"))
@Column(name = "post_type")
private List<String> postTypes; // 글 유형 (최대 2개)

@ElementCollection(fetch = FetchType.EAGER)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "community_learning_types", joinColumns = @JoinColumn(name = "community_id"))
@Column(name = "learning_type")
private List<String> learningTypes; // 학습 유형 (최대 2개)
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/itstime/reflog/community/dto/CommunityDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import itstime.reflog.postlike.domain.enums.PostType;
import itstime.reflog.retrospect.domain.Retrospect;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CommunityDto {

@Getter
Expand Down Expand Up @@ -49,13 +51,15 @@ public static class CombinedCategoryResponse {
private Long postId;
private PostType postType;

public static CombinedCategoryResponse fromCommunity(Community community, String writer, Boolean isLike, Integer totalLike, Long totalComment) {
public static CombinedCategoryResponse fromCommunity(Community community, List<String> postTypes,List<String> learningTypes, String writer, Boolean isLike, Integer totalLike, Long totalComment) {


return CombinedCategoryResponse.builder()
.title(community.getTitle())
.content(community.getContent())
.createdDate(community.getCreatedAt())
.postTypes(community.getPostTypes())
.learningTypes(community.getLearningTypes())
.postTypes(postTypes)
.learningTypes(learningTypes)
.isLike(isLike)
.totalComment(totalComment)
.totalLike(totalLike)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface CommunityRepository extends JpaRepository<Community, Long> {

Expand All @@ -30,4 +31,5 @@ List<Community> findByLearningTypesAndPostTypes(

//내가 작성한 글 모두 찾기
List<Community> findAllByMemberOrderByIdDesc(Member member);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package itstime.reflog.community.service;

import itstime.reflog.comment.repository.CommentRepository;
import itstime.reflog.community.domain.Community;
import itstime.reflog.community.dto.CommunityDto;
import itstime.reflog.community.repository.CommunityRepository;
import itstime.reflog.member.domain.Member;
import itstime.reflog.mypage.domain.MyPage;
import itstime.reflog.mypage.repository.MyPageRepository;
import itstime.reflog.postlike.repository.PostLikeRepository;
import itstime.reflog.postlike.service.PostLikeService;
import itstime.reflog.retrospect.domain.Retrospect;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor //final로 설정한 것만 의존성 주임
public class CommunityConverter {

private final MyPageRepository myPageRepository;
private final PostLikeRepository postLikeRepository;
private final PostLikeService postLikeService;
private final CommentRepository commentRepository;

@Transactional(readOnly = true)
public List<CommunityDto.CombinedCategoryResponse> communityResponseConverter(List<Community> communities, Member member) {

List<CommunityDto.CombinedCategoryResponse> responses = communities.stream()
.map(community -> {
//마이페이지 레포지토리에서 닉네임 반환
String nickname = myPageRepository.findByMember(community.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");

List<String> communityPostTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화
List<String> communityLearningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화

//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumCommunityPostLike(community);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByCommunity(community);
return CommunityDto.CombinedCategoryResponse.fromCommunity(community,communityPostTypes, communityLearningTypes, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());

return responses;
}

@Transactional(readOnly = true)
public List<CommunityDto.CombinedCategoryResponse> retrospectResponseConverter(List<Retrospect> retrospects, Member member) {

List<CommunityDto.CombinedCategoryResponse> responses = retrospects.stream().map(
retrospect -> {
String nickname = myPageRepository.findByMember(retrospect.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");
//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findBookmarkByMemberAndRetrospect(member, retrospect).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumRetrospectPostLike(retrospect);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByRetrospect(retrospect);

return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());

return responses;
}

}
120 changes: 11 additions & 109 deletions src/main/java/itstime/reflog/community/service/CommunityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public class CommunityService {
private final RetrospectRepository retrospectRepository;
private final CommentRepository commentRepository;
private final PostLikeService postLikeService;
private final PostLikeRepository postLikeRepository;
private final MissionService missionService;
private final MemberServiceHelper memberServiceHelper;
private final CommunityConverter communityConverter;


@Transactional
Expand Down Expand Up @@ -222,49 +222,14 @@ public List<CommunityDto.CombinedCategoryResponse> getFilteredCommunity(String m
}

//커뮤니티 response형태로 반환 닉네임 추가
List<CommunityDto.CombinedCategoryResponse> responses = communities.stream()
.map(community -> {
//마이페이지 레포지토리에서 닉네임 반환
String nickname = myPageRepository.findByMember(community.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");

//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumCommunityPostLike(community);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByCommunity(community);
return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());
List<CommunityDto.CombinedCategoryResponse> responses = communityConverter.communityResponseConverter(communities, member);

//글 유형에 회고일지가 있는 경우
if (postTypes != null && postTypes.contains("회고일지")) {
List<Retrospect> retrospects = retrospectRepository.findByVisibilityIsTrue();
List<CommunityDto.CombinedCategoryResponse> retrospectResponses = retrospects.stream()
.map(retrospect -> {
String nickname = myPageRepository.findByMember(retrospect.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");

//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findLikeByMemberAndRetrospect(member, retrospect).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumRetrospectPostLike(retrospect);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByRetrospect(retrospect);

return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());
List<CommunityDto.CombinedCategoryResponse> retrospectResponses = communityConverter.retrospectResponseConverter(retrospects, member);
responses.addAll(retrospectResponses); // 두 리스트 합치기(회고일지, 커뮤니티)
}

return responses;
}

Expand All @@ -277,44 +242,11 @@ public List<CommunityDto.CombinedCategoryResponse> getSearchedCommunity(String m
//커뮤니티 게시물 중 키워드가 일치하는 게시물 찾기
List<Community> communities = communityRepository.searchCommunitiesByTitleContaining(title);

List<CommunityDto.CombinedCategoryResponse> responses = communities.stream()
.map(community -> {
String nickname = myPageRepository.findByMember(community.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");

//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findLikeByMemberAndCommunity(member, community).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumCommunityPostLike(community);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByCommunity(community);
return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());

List<CommunityDto.CombinedCategoryResponse> responses = communityConverter.communityResponseConverter(communities, member);
//회고일지 게시물 중 키워드가 일치하는 게시물 찾기
List<Retrospect> retrospects = retrospectRepository.findByTitleContainingAndVisibilityIsTrue(title);

List<CommunityDto.CombinedCategoryResponse> retrospectResponses = retrospects.stream()
.map(retrospect -> {
String nickname = myPageRepository.findByMember(retrospect.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");
//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findLikeByMemberAndRetrospect(member, retrospect).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumRetrospectPostLike(retrospect);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByRetrospect(retrospect);

return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());
List<CommunityDto.CombinedCategoryResponse> retrospectResponses = communityConverter.retrospectResponseConverter(retrospects, member);
responses.addAll(retrospectResponses); // 두 리스트 합치기(회고일지, 커뮤니티)

return responses;
Expand All @@ -328,42 +260,9 @@ public List<CommunityDto.CombinedCategoryResponse> getAllCommunity(String member
List<Retrospect> retrospects = retrospectRepository.findAllByVisibilityTrueOrderByCreatedDateDesc();

//회고일지, 커뮤니티 리스트를 합쳐 하나의 리스트로
List<CommunityDto.CombinedCategoryResponse> responses = retrospects.stream().map(
retrospect -> {
String nickname = myPageRepository.findByMember(retrospect.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");
//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findBookmarkByMemberAndRetrospect(member, retrospect).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumRetrospectPostLike(retrospect);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByRetrospect(retrospect);

return CommunityDto.CombinedCategoryResponse.fromRetrospect(retrospect, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());
List<CommunityDto.CombinedCategoryResponse> responses = communityConverter.retrospectResponseConverter(retrospects, member);

List<CommunityDto.CombinedCategoryResponse> communityResponses = communities.stream()
.map(community -> {
String nickname = myPageRepository.findByMember(community.getMember())
.map(MyPage::getNickname)
.orElse("닉네임 없음");

//좋아요 있는지 없는지 플래그
Boolean isLike = postLikeRepository.findBookmarkByMemberAndCommunity(member, community).isPresent();

//게시물마다 좋아요 총 갯수 반환
int totalLike = postLikeService.getSumCommunityPostLike(community);

//게시물마다 댓글 수 반환
Long totalComment = commentRepository.countByCommunity(community);

return CommunityDto.CombinedCategoryResponse.fromCommunity(community, nickname, isLike, totalLike, totalComment);
})
.collect(Collectors.toList());
List<CommunityDto.CombinedCategoryResponse> communityResponses = communityConverter.communityResponseConverter(communities, member);

responses.addAll(communityResponses);

Expand All @@ -388,12 +287,15 @@ public List<MyPageDto.MyPagePostResponse> getPostsByProfile(String nickname) {
List<MyPageDto.MyPagePostResponse> responses = communityList.stream()
.map(community -> {

List<String> postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화
List<String> learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화

// 좋아요 총 개수
int totalLike = postLikeService.getSumCommunityPostLike(community);
// 댓글 총 개수
long commentCount = commentRepository.countByCommunity(community);

return MyPageDto.MyPagePostResponse.fromCommunity(community, totalLike, commentCount);
return MyPageDto.MyPagePostResponse.fromCommunity(community, postTypes, learningTypes,totalLike, commentCount);
})
.collect(Collectors.toList());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/itstime/reflog/mypage/dto/MyPageDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static class MyPagePostResponse {
private int totalLike;
private long commentCount;

public static MyPagePostResponse fromCommunity(Community community, int totalLike, long commentCount){
public static MyPagePostResponse fromCommunity(Community community, List<String> postTypes,List<String> learningTypes, int totalLike, long commentCount){
return MyPagePostResponse.builder()
.id(community.getId())
.title(community.getTitle())
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/itstime/reflog/mypage/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -142,12 +143,15 @@ public List<MyPageDto.MyPagePostResponse> getMyPost(String memberId) {
List<MyPageDto.MyPagePostResponse> responses = communityList.stream()
.map(community -> {

List<String> postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화
List<String> learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화

// 좋아요 총 개수
int totalLike = postLikeService.getSumCommunityPostLike(community);
// 댓글 총 개수
long commentCount = commentRepository.countByCommunity(community);

return MyPageDto.MyPagePostResponse.fromCommunity(community, totalLike, commentCount);
return MyPageDto.MyPagePostResponse.fromCommunity(community, postTypes, learningTypes, totalLike, commentCount);
})
.collect(Collectors.toList());

Expand Down Expand Up @@ -181,13 +185,17 @@ public List<MyPageDto.MyPagePostResponse> getMyLikePost(String memberId) {
// 3. 내가 좋아요 한 커뮤니티 글 정리
List<MyPageDto.MyPagePostResponse> responses = postLikeList.stream()
.map(postLike -> {
Community community = postLike.getCommunity();

List<String> postTypes = new ArrayList<>(community.getPostTypes()); // 강제 초기화
List<String> learningTypes = new ArrayList<>(community.getLearningTypes()); // 강제 초기화

// 좋아요 총 개수
int totalLike = postLikeService.getSumCommunityPostLike(postLike.getCommunity());
// 댓글 총 개수
long commentCount = commentRepository.countByCommunity(postLike.getCommunity());

return MyPageDto.MyPagePostResponse.fromCommunity(postLike.getCommunity(), totalLike, commentCount);
return MyPageDto.MyPagePostResponse.fromCommunity(community, postTypes, learningTypes, totalLike, commentCount);
})
.collect(Collectors.toList());

Expand Down
Loading
Loading