Skip to content

Commit

Permalink
Merge pull request #105 from STEPPER-UMC-6th/feat/104
Browse files Browse the repository at this point in the history
[Feat] 내가 스크랩한 글 모음
  • Loading branch information
lehojun authored Aug 12, 2024
2 parents f253beb + 374711b commit 90d7511
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", "내가 작성한 댓글이 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ScrapRepository extends JpaRepository<Scrap, Long> {

boolean existsByMemberAndPost(Member member, Post post);
Expand All @@ -15,4 +18,6 @@ public interface ScrapRepository extends JpaRepository<Scrap, Long> {

@Query("SELECT count(e) FROM Scrap e WHERE e.post = :post")
int getCountByPost(Post post);

List<Scrap> findByMember(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public interface PostService {

List<PostDto.PostViewDto> getCommentsList(String email);

List<PostDto.PostViewDto> getScrapList(String email);


}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -120,15 +118,40 @@ public List<PostDto.PostViewDto> getCommentsList(String email) {

List<Comment> 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<PostDto.PostViewDto> getScrapList(String email) {
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));

List<Scrap> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,13 @@ public ApiResponse<List<PostDto.PostViewDto>> getCommentsList(HttpServletRequest
List<PostDto.PostViewDto> response = postService.getCommentsList(email);
return ApiResponse.onSuccess(response);
}

@Operation(summary = "내가 스크랩한 글 조회 API", description = "내가 스크랩한 글 조회")
@GetMapping("/my_scrap")
public ApiResponse<List<PostDto.PostViewDto>> getScrapList(HttpServletRequest request) {
String token = request.getHeader("Authorization").substring(7);
String email = jwtUtil.getUsername(token);
List<PostDto.PostViewDto> response = postService.getScrapList(email);
return ApiResponse.onSuccess(response);
}
}

0 comments on commit 90d7511

Please sign in to comment.