From 85a0f01444ec12aeb292e8da011cf19ecab18194 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Thu, 1 Feb 2024 13:25:30 +0900 Subject: [PATCH] [feat] getMyPost --- .../community/repository/PostRepository.java | 2 + .../profile/controller/profileController.java | 7 ++- .../dto/Profile/GetSettingPostDto.java | 46 +++++++++++++++++++ .../profile/service/ProfileService.java | 30 +++++++++++- .../kimgreen/backend/response/Message.java | 1 + 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java index 5f435a4..9dcafe3 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java @@ -1,6 +1,7 @@ package com.kimgreen.backend.domain.community.repository; import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.member.entity.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -14,4 +15,5 @@ public interface PostRepository extends JpaRepository { @Query("select p from Post p where member.memberId= :id and createdAt between :start and :end") public List findAllBetweenDate(@Param("id") Long memberId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end); public List findAllByCreatedAtBetween(LocalDateTime start, LocalDateTime end); + List findByMember(Member member); } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java index 15ff25a..82b1f81 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java @@ -37,10 +37,15 @@ public Response getProfileInfo(@RequestParam("memberId") Long memberId) { @Operation(summary = "설정창 내가 쓴 댓글 불러오기") @ResponseStatus(OK) - @GetMapping("/setting/profile") + @GetMapping("/setting/comment") public Response getMyComment() { return success(GET_MY_COMMENT_SUCCESS,profileService.getMyComment()); } + @Operation(summary = "설정창 내가 쓴 글 불러오기") + @ResponseStatus(OK) + @GetMapping("/setting/post") + public Response getMyPost() {return success(GET_MY_POST_SUCCESS, profileService.getMyPost());} + } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java new file mode 100644 index 0000000..8974e87 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java @@ -0,0 +1,46 @@ +package com.kimgreen.backend.domain.profile.dto.Profile; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class GetSettingPostDto { + private Long postId; + private String writerNickname; + private String writerBadge; + private String writerProfileImg; + private String content; + private Long likeCount; + private Long commentCount; + private String imgUrl; + private boolean isLiked; + + public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount, String imgUrl) { + return GetSettingPostDto.builder() + .postId(postId) + .content(content) + .writerBadge(writerBadge) + .writerNickname(writerNickname) + .writerProfileImg(writerProfileImg) + .likeCount(likeCount) + .commentCount(commentCount) + .imgUrl(imgUrl) + .build(); + } + public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount) { + return GetSettingPostDto.builder() + .postId(postId) + .content(content) + .writerBadge(writerBadge) + .writerNickname(writerNickname) + .writerProfileImg(writerProfileImg) + .likeCount(likeCount) + .commentCount(commentCount) + .build(); + } +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index 851db41..3ac65ba 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -4,7 +4,9 @@ import com.kimgreen.backend.domain.community.entity.Comment; import com.kimgreen.backend.domain.community.entity.Likes; import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.community.entity.PostImg; import com.kimgreen.backend.domain.community.repository.CommentRepository; +import com.kimgreen.backend.domain.community.repository.LikeRepository; import com.kimgreen.backend.domain.community.repository.PostImgRepository; import com.kimgreen.backend.domain.community.repository.PostRepository; import com.kimgreen.backend.domain.community.service.S3Service; @@ -15,6 +17,7 @@ import com.kimgreen.backend.domain.member.service.MemberService; import com.kimgreen.backend.domain.profile.dto.Profile.CommentResponseDto; import com.kimgreen.backend.domain.profile.dto.Profile.GetProfileDto; +import com.kimgreen.backend.domain.profile.dto.Profile.GetSettingPostDto; import com.kimgreen.backend.domain.profile.entity.ProfileBadge; import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository; @@ -42,6 +45,7 @@ public class ProfileService { private final PostImgRepository postImgRepository; private final GetProfilePostDto getProfilePostDto; private final CommentRepository commentRepository; + private final LikeRepository likeRepository; public List response(Long memberId){ List list = new ArrayList<>(); @@ -125,15 +129,37 @@ public List getMyComment() { String writer = member.getNickname(); String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; List comments = commentRepository.findByMember(member); + List dto = new ArrayList<>(); for(Comment comment : comments) { Post post = postRepository.findById(comment.getPost().getPostId()).orElseThrow(PostNotFound::new); - CommentResponseDto commentDto - = CommentResponseDto.toDto(comment.getCommentId(),post.getPostId(),writer,writerBadge,comment.getContent()); + CommentResponseDto commentDto = CommentResponseDto.toDto(comment.getCommentId(),post.getPostId(),writerBadge,writer,comment.getContent()); dto.add(commentDto); } return dto; } + public List getMyPost() { + Member member = memberService.getCurrentMember(); + String writer = member.getNickname(); + String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; + String writerProfileImg = s3Service.getFullUrl(memberProfileImgRepository.findByMember(member).getImgUrl()); + List posts = postRepository.findByMember(member); + + List dto = new ArrayList<>(); + for (Post post : posts) { + Long countLike = likeRepository.countLike(post.getPostId()); + Long countComment = commentRepository.countComment(post.getPostId()); + PostImg postImg = postImgRepository.findByPost(post); + //Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, int likeCount, int commentCount, String imgUrl + if (postImg != null) { + dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment, s3Service.getFullUrl(postImg.getImgUrl()))); + } else { + dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment)); + } + + } + return dto; + } } diff --git a/src/main/java/com/kimgreen/backend/response/Message.java b/src/main/java/com/kimgreen/backend/response/Message.java index eb1a580..16eeb0a 100644 --- a/src/main/java/com/kimgreen/backend/response/Message.java +++ b/src/main/java/com/kimgreen/backend/response/Message.java @@ -29,5 +29,6 @@ public class Message { public static String PROFILE_POSTS_SUCCESS="쓴 글 목록 불러오기 성공했습니다"; public static String GET_MY_COMMENT_SUCCESS = "내가 쓴 댓글 보기 성공했습니다."; + public static String GET_MY_POST_SUCCESS = "내가 쓴 글 보기 성공했습니다."; }