|
1 | | -package com.example.FixLog.service; |
2 | | - |
3 | | -import com.example.FixLog.domain.member.Member; |
4 | | -import com.example.FixLog.domain.post.Post; |
5 | | -import com.example.FixLog.dto.PageResponseDto; |
6 | | -import com.example.FixLog.dto.post.MyPostPageResponseDto; |
7 | | -import com.example.FixLog.exception.CustomException; |
8 | | -import com.example.FixLog.exception.ErrorCode; |
9 | | -import com.example.FixLog.repository.MemberRepository; |
10 | | -import com.example.FixLog.repository.fork.ForkRepository; |
11 | | -import com.example.FixLog.repository.post.PostRepository; |
12 | | -import org.springframework.data.domain.Page; |
13 | | -import org.springframework.data.domain.PageRequest; |
14 | | -import org.springframework.data.domain.Pageable; |
15 | | -import org.springframework.data.domain.Sort; |
16 | | -import org.springframework.stereotype.Service; |
17 | | - |
18 | | -import java.util.List; |
19 | | -import java.util.Map; |
20 | | -import java.util.stream.Collectors; |
21 | | - |
22 | | -@Service |
23 | | -public class MypagePostService { |
24 | | - |
25 | | - private final PostRepository postRepository; |
26 | | - private final MemberRepository memberRepository; |
27 | | - private final ForkRepository forkRepository; |
28 | | - |
29 | | - public MypagePostService(PostRepository postRepository, MemberRepository memberRepository, ForkRepository forkRepository) { |
30 | | - this.postRepository = postRepository; |
31 | | - this.memberRepository = memberRepository; |
32 | | - this.forkRepository = forkRepository; |
33 | | - } |
34 | | - |
35 | | - // 내가 쓴 글 보기 |
36 | | - public PageResponseDto<MyPostPageResponseDto> getMyPosts(String email, int page, int sort, int size) { |
37 | | - Member member = memberRepository.findByEmail(email) |
38 | | - .orElseThrow(() -> new CustomException(ErrorCode.USER_EMAIL_NOT_FOUND)); |
39 | | - |
40 | | - // 1: 오래된순, 0: 최신순 |
41 | | - Sort.Direction direction = (sort == 1) ? Sort.Direction.ASC : Sort.Direction.DESC; |
42 | | - Pageable pageable = PageRequest.of(page, size, Sort.by(direction, "createdAt")); |
43 | | - |
44 | | - Page<Post> postPage = postRepository.findByUserId(member, pageable); |
45 | | - List<Post> posts = postPage.getContent(); |
46 | | - |
47 | | - // fork count 한번에 조회 |
48 | | - List<Object[]> forkCounts = forkRepository.countForksByOriginalPosts(posts); |
49 | | - Map<Long, Integer> forkCountMap = forkCounts.stream() |
50 | | - .collect(Collectors.toMap( |
51 | | - row -> (Long) row[0], // postId |
52 | | - row -> ((Long) row[1]).intValue() // 포크 카운트 (int) |
53 | | - )); |
54 | | - |
55 | | - return PageResponseDto.from(postPage, post -> |
56 | | - MyPostPageResponseDto.from(post, forkCountMap.getOrDefault(post.getPostId(), 0) // 없으면 0 반환 |
57 | | - ) |
58 | | - ); |
59 | | - } |
60 | | -} |
| 1 | +//package com.example.FixLog.service; |
| 2 | +// |
| 3 | +//import com.example.FixLog.domain.like.PostLike; |
| 4 | +//import com.example.FixLog.domain.member.Member; |
| 5 | +//import com.example.FixLog.domain.post.Post; |
| 6 | +//import com.example.FixLog.dto.PageResponseDto; |
| 7 | +//import com.example.FixLog.dto.post.MyPostPageResponseDto; |
| 8 | +//import com.example.FixLog.exception.CustomException; |
| 9 | +//import com.example.FixLog.exception.ErrorCode; |
| 10 | +//import com.example.FixLog.repository.MemberRepository; |
| 11 | +//import com.example.FixLog.repository.fork.ForkRepository; |
| 12 | +//import com.example.FixLog.repository.like.PostLikeRepository; |
| 13 | +//import com.example.FixLog.repository.post.PostRepository; |
| 14 | +//import org.springframework.data.domain.Page; |
| 15 | +//import org.springframework.data.domain.PageRequest; |
| 16 | +//import org.springframework.data.domain.Pageable; |
| 17 | +//import org.springframework.data.domain.Sort; |
| 18 | +// |
| 19 | +//import java.util.List; |
| 20 | +//import java.util.Map; |
| 21 | +//import java.util.stream.Collectors; |
| 22 | +// |
| 23 | +//public class MypagePostService { |
| 24 | +// private final PostRepository postRepository; |
| 25 | +// private final MemberRepository memberRepository; |
| 26 | +// private final ForkRepository forkRepository; |
| 27 | +// private final postLikeRepository postLikeRepository; |
| 28 | +// |
| 29 | +// public MypagePostService(PostRepository postRepository, MemberRepository memberRepository, ForkRepository forkRepository, PostLikeRepository postLikeRepository) { |
| 30 | +// this.postRepository = postRepository; |
| 31 | +// this.memberRepository = memberRepository; |
| 32 | +// this.forkRepository = forkRepository; |
| 33 | +// } |
| 34 | +// |
| 35 | +// // 내가 쓴 글 보기 |
| 36 | +// public PageResponseDto<MyPostPageResponseDto> getMyPosts(String email, int page, int sort, int size) { |
| 37 | +// Member member = memberRepository.findByEmail(email) |
| 38 | +// .orElseThrow(() -> new CustomException(ErrorCode.USER_EMAIL_NOT_FOUND)); |
| 39 | +// |
| 40 | +// // 1: 오래된순, 0: 최신순 |
| 41 | +// Sort.Direction direction = (sort == 1) ? Sort.Direction.ASC : Sort.Direction.DESC; |
| 42 | +// Pageable pageable = PageRequest.of(page, size, Sort.by(direction, "createdAt")); |
| 43 | +// |
| 44 | +// Page<Post> postPage = postRepository.findByUserId(member, pageable); |
| 45 | +// List<Post> posts = postPage.getContent(); |
| 46 | +// |
| 47 | +// // fork count 한번에 조회 |
| 48 | +// List<Object[]> forkCounts = forkRepository.countForksByOriginalPosts(posts); |
| 49 | +// Map<Long, Integer> forkCountMap = forkCounts.stream() |
| 50 | +// .collect(Collectors.toMap( |
| 51 | +// row -> (Long) row[0], // postId |
| 52 | +// row -> ((Long) row[1]).intValue() // 포크 카운트 (int) |
| 53 | +// )); |
| 54 | +// |
| 55 | +// return PageResponseDto.from(postPage, post -> |
| 56 | +// MyPostPageResponseDto.from(post, forkCountMap.getOrDefault(post.getPostId(), 0) // 없으면 0 반환 |
| 57 | +// ) |
| 58 | +// ); |
| 59 | +// } |
| 60 | +// // 내가 좋아요한 글 보기 |
| 61 | +// public PageResponseDto<MyPostPageResponseDto> getLikedPosts(String email, int page, int sort, int size) { |
| 62 | +// Member member = memberRepository.findByEmail(email) |
| 63 | +// .orElseThrow(() -> new CustomException(ErrorCode.USER_EMAIL_NOT_FOUND)); |
| 64 | +// |
| 65 | +// // 1: 오래된순, 0: 최신순 |
| 66 | +// Sort.Direction direction = (sort == 1) ? Sort.Direction.ASC : Sort.Direction.DESC; |
| 67 | +// Pageable pageable = PageRequest.of(page, size, Sort.by(direction, "postId.createdAt")); |
| 68 | +// |
| 69 | +// Page<PostLike> postLikePage = postLikeRepository.findByUserId(member, pageable); |
| 70 | +// List<Post> likedPosts = postLikePage.map(PostLike::getPostId).getContent(); |
| 71 | +// |
| 72 | +// // fork count 한번에 조회 |
| 73 | +// List<Object[]> forkCounts = forkRepository.countForksByOriginalPosts(likedPosts); |
| 74 | +// Map<Long, Integer> forkCountMap = forkCounts.stream() |
| 75 | +// .collect(Collectors.toMap( |
| 76 | +// row -> (Long) row[0], |
| 77 | +// row -> ((Long) row[1]).intValue() |
| 78 | +// )); |
| 79 | +// |
| 80 | +// return PageResponseDto.from(postLikePage.map(PostLike::getPostId), post -> |
| 81 | +// MyPostPageResponseDto.from(post, forkCountMap.getOrDefault(post.getPostId(), 0)) |
| 82 | +// ); |
| 83 | +// } |
| 84 | +// |
| 85 | +//} |
0 commit comments