Skip to content

Commit

Permalink
[feat] getMyPost
Browse files Browse the repository at this point in the history
  • Loading branch information
gol2580 committed Feb 1, 2024
1 parent 9b4abf6 commit 85a0f01
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,4 +15,5 @@ public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select p from Post p where member.memberId= :id and createdAt between :start and :end")
public List<Post> findAllBetweenDate(@Param("id") Long memberId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
public List<Post> findAllByCreatedAtBetween(LocalDateTime start, LocalDateTime end);
List<Post> findByMember(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());}


}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<GetProfilePostDto> response(Long memberId){
List<GetProfilePostDto> list = new ArrayList<>();
Expand Down Expand Up @@ -125,15 +129,37 @@ public List<CommentResponseDto> getMyComment() {
String writer = member.getNickname();
String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name;
List<Comment> comments = commentRepository.findByMember(member);

List<CommentResponseDto> 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<GetSettingPostDto> 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<Post> posts = postRepository.findByMember(member);

List<GetSettingPostDto> 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;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/kimgreen/backend/response/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "내가 쓴 글 보기 성공했습니다.";

}

0 comments on commit 85a0f01

Please sign in to comment.