diff --git a/src/main/java/com/example/stepperbackend/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/example/stepperbackend/apiPayload/code/status/ErrorStatus.java index 9124363..9ba26f3 100644 --- a/src/main/java/com/example/stepperbackend/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/example/stepperbackend/apiPayload/code/status/ErrorStatus.java @@ -50,6 +50,7 @@ public enum ErrorStatus implements BaseErrorCode { // scrap SCRAP_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "SCRAP4001", "이미 스크랩된 게시물입니다."), + SCRAP_NOT_FOUND(HttpStatus.BAD_REQUEST, "SCRAP4001", "스크랩한 글이 없습니다."), //comment MY_COMMENTS_NOT_FOUND(HttpStatus.BAD_REQUEST, "MY_COMMENTS4001", "내가 작성한 댓글이 없습니다."), diff --git a/src/main/java/com/example/stepperbackend/apiPayload/exception/handler/ScrapHandler.java b/src/main/java/com/example/stepperbackend/apiPayload/exception/handler/ScrapHandler.java new file mode 100644 index 0000000..9ec5767 --- /dev/null +++ b/src/main/java/com/example/stepperbackend/apiPayload/exception/handler/ScrapHandler.java @@ -0,0 +1,10 @@ +package com.example.stepperbackend.apiPayload.exception.handler; + +import com.example.stepperbackend.apiPayload.code.BaseErrorCode; +import com.example.stepperbackend.apiPayload.exception.GeneralException; + +public class ScrapHandler extends GeneralException { + public ScrapHandler(BaseErrorCode code) { + super(code); + } +} diff --git a/src/main/java/com/example/stepperbackend/repository/ScrapRepository.java b/src/main/java/com/example/stepperbackend/repository/ScrapRepository.java index 69c74cb..9d66749 100644 --- a/src/main/java/com/example/stepperbackend/repository/ScrapRepository.java +++ b/src/main/java/com/example/stepperbackend/repository/ScrapRepository.java @@ -1,5 +1,6 @@ package com.example.stepperbackend.repository; +import com.example.stepperbackend.domain.Comment; import com.example.stepperbackend.domain.Member; import com.example.stepperbackend.domain.Post; import com.example.stepperbackend.domain.mapping.Scrap; @@ -7,6 +8,8 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.List; + public interface ScrapRepository extends JpaRepository { boolean existsByMemberAndPost(Member member, Post post); @@ -15,4 +18,6 @@ public interface ScrapRepository extends JpaRepository { @Query("SELECT count(e) FROM Scrap e WHERE e.post = :post") int getCountByPost(Post post); + + List findByMember(Member member); } diff --git a/src/main/java/com/example/stepperbackend/service/PostService/PostService.java b/src/main/java/com/example/stepperbackend/service/PostService/PostService.java index 108d869..0c025fe 100644 --- a/src/main/java/com/example/stepperbackend/service/PostService/PostService.java +++ b/src/main/java/com/example/stepperbackend/service/PostService/PostService.java @@ -16,4 +16,7 @@ public interface PostService { List getCommentsList(String email); + List getScrapList(String email); + + } diff --git a/src/main/java/com/example/stepperbackend/service/PostService/PostServiceImpl.java b/src/main/java/com/example/stepperbackend/service/PostService/PostServiceImpl.java index e07f27a..9189c5a 100644 --- a/src/main/java/com/example/stepperbackend/service/PostService/PostServiceImpl.java +++ b/src/main/java/com/example/stepperbackend/service/PostService/PostServiceImpl.java @@ -1,15 +1,13 @@ package com.example.stepperbackend.service.PostService; import com.example.stepperbackend.apiPayload.code.status.ErrorStatus; -import com.example.stepperbackend.apiPayload.exception.handler.CommentHandler; -import com.example.stepperbackend.apiPayload.exception.handler.MemberHandler; -import com.example.stepperbackend.apiPayload.exception.handler.PostHandler; -import com.example.stepperbackend.apiPayload.exception.handler.RateDiaryHandler; +import com.example.stepperbackend.apiPayload.exception.handler.*; import com.example.stepperbackend.domain.Comment; import com.example.stepperbackend.domain.Member; import com.example.stepperbackend.domain.Post; import com.example.stepperbackend.domain.WeeklyMission; import com.example.stepperbackend.domain.enums.BodyPart; +import com.example.stepperbackend.domain.mapping.Scrap; import com.example.stepperbackend.repository.*; import com.example.stepperbackend.service.badgeService.BadgeService; import com.example.stepperbackend.web.dto.PostDto; @@ -47,7 +45,7 @@ public PostDto.PostResponseDto createPost(PostDto.PostRequestDto postRequestDto, post = postRepository.save(post); // 첫 커뮤니티 게시글 작성 완료 - if(postRepository.getCountByMember(member) == 1){ + if (postRepository.getCountByMember(member) == 1) { badgeService.putFirstBadge("첫 게시글 작성 완료", member); } @@ -81,15 +79,15 @@ public PostDto.PostViewDto getPost(Long postId, String email) { .orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); Post post = postRepository.findById(postId) - .orElseThrow(()-> new PostHandler(ErrorStatus.POST_NOT_FOUND)); + .orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); int likes = likeRepository.getCountByPost(post); int scraps = scrapRepository.getCountByPost(post); - int commentsCount= commentRepository.getCountByPost(post); + int commentsCount = commentRepository.getCountByPost(post); - return PostConverter.toViewDto(post,likes,scraps,commentsCount); + return PostConverter.toViewDto(post, likes, scraps, commentsCount); } @Override @@ -120,15 +118,40 @@ public List getCommentsList(String email) { List comments = commentRepository.findByMember(member); - if(comments.isEmpty()) {throw new CommentHandler(ErrorStatus.MY_COMMENTS_NOT_FOUND);} + if (comments.isEmpty()) { + throw new CommentHandler(ErrorStatus.MY_COMMENTS_NOT_FOUND); + } return comments.stream() .map(comment -> { Post post = comment.getPost(); int likes = likeRepository.getCountByPost(post); int scraps = scrapRepository.getCountByPost(post); - int commentsCount= commentRepository.getCountByPost(post); - return PostConverter.toViewDto(post,likes,scraps,commentsCount); + int commentsCount = commentRepository.getCountByPost(post); + return PostConverter.toViewDto(post, likes, scraps, commentsCount); + }) + .distinct() + .toList(); + } + + @Override + public List getScrapList(String email) { + Member member = memberRepository.findByEmail(email) + .orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + + List scraps = scrapRepository.findByMember(member); + + if (scraps.isEmpty()) { + throw new ScrapHandler(ErrorStatus.SCRAP_NOT_FOUND); + } + + return scraps.stream() + .map(scrap -> { + Post post = scrap.getPost(); + int likes = likeRepository.getCountByPost(post); + int scrapsCount = scrapRepository.getCountByPost(post); + int commentsCount = commentRepository.getCountByPost(post); + return PostConverter.toViewDto(post, likes, scrapsCount, commentsCount); }) .distinct() .toList(); diff --git a/src/main/java/com/example/stepperbackend/web/controller/PostController.java b/src/main/java/com/example/stepperbackend/web/controller/PostController.java index e345802..a5d3a08 100644 --- a/src/main/java/com/example/stepperbackend/web/controller/PostController.java +++ b/src/main/java/com/example/stepperbackend/web/controller/PostController.java @@ -111,4 +111,13 @@ public ApiResponse> getCommentsList(HttpServletRequest List response = postService.getCommentsList(email); return ApiResponse.onSuccess(response); } + + @Operation(summary = "내가 스크랩한 글 조회 API", description = "내가 스크랩한 글 조회") + @GetMapping("/my_scrap") + public ApiResponse> getScrapList(HttpServletRequest request) { + String token = request.getHeader("Authorization").substring(7); + String email = jwtUtil.getUsername(token); + List response = postService.getScrapList(email); + return ApiResponse.onSuccess(response); + } } \ No newline at end of file