From b61c7714b74b78990cbbbdcfd55bb289a7d24107 Mon Sep 17 00:00:00 2001 From: bngsh Date: Thu, 2 Nov 2023 22:43:17 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20#25=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0?= =?UTF-8?q?=EC=97=90=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=A0=95=EB=B3=B4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/whyranoid/walkie/domain/Post.java | 15 ++++--- .../com/whyranoid/walkie/dto/PostDto.java | 26 +++++++++--- .../querydsl/PostRepositoryCustom.java | 2 + .../querydsl/PostRepositoryImpl.java | 40 ++++++++++++++----- .../walkie/service/CommunityService.java | 9 +++++ 5 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/whyranoid/walkie/domain/Post.java b/src/main/java/com/whyranoid/walkie/domain/Post.java index f78565c..9b9fc15 100644 --- a/src/main/java/com/whyranoid/walkie/domain/Post.java +++ b/src/main/java/com/whyranoid/walkie/domain/Post.java @@ -5,10 +5,14 @@ import lombok.NoArgsConstructor; import lombok.Setter; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.validation.constraints.NotNull; -import java.util.ArrayList; -import java.util.List; @Entity @Getter @@ -45,11 +49,6 @@ public class Post { @JoinColumn(name = "walkie_id", nullable = false) // referencedColumnName는 디폴트가 pk private Walkie user; - @ManyToMany - @JoinTable(name = "walkie_post_like", - joinColumns = @JoinColumn(name = "post_id", nullable = false), - inverseJoinColumns = @JoinColumn(name = "walkie_id", nullable = false)) - private List liker = new ArrayList<>(); // @OneToOne(optional = false) // optional = false 안해주면 history가 겹치는 경우가 발생 시 에러 // @JoinColumn(name = "history", nullable = false) diff --git a/src/main/java/com/whyranoid/walkie/dto/PostDto.java b/src/main/java/com/whyranoid/walkie/dto/PostDto.java index c1ac64a..04f704f 100644 --- a/src/main/java/com/whyranoid/walkie/dto/PostDto.java +++ b/src/main/java/com/whyranoid/walkie/dto/PostDto.java @@ -7,8 +7,8 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -17,8 +17,8 @@ public class PostDto { @Schema(description = "[응답] 워키 아이디", example = "3") private Long viewerId; - @Schema(description = "[응답] 작성자 아이디", example = "2") - private Long posterId; + @Schema(description = "[응답] 작성자") + private WalkieDto poster; @Schema(description = "[응답] 게시글 pk", example = "26343") private Long postId; @@ -27,7 +27,7 @@ public class PostDto { private Boolean liked = false; @Schema(description = "[응답] 좋아요 누른 유저 리스트") - private List liker; + private List likers = new ArrayList<>(); @Schema(description = "[응답] 사진파일 URI") private String photo; @@ -44,12 +44,26 @@ public class PostDto { @Schema(description = "[응답] 기록 데이터") private String historyContent; + @QueryProjection + public PostDto(Post post, Long viewerId, List liker) { + this.viewerId = viewerId; + this.poster = new WalkieDto(post.getUser()); + this.postId = post.getPostId(); + this.liked = liker.stream().map(WalkieDto::getWalkieId).anyMatch(id -> id.longValue() == viewerId.longValue()); + this.likers = liker; + this.photo = post.getPhoto(); + this.content = post.getContent(); + this.date = post.getDate(); + this.colorMode = post.getColorMode(); + this.historyContent = post.getHistoryContent(); + } + @QueryProjection public PostDto(Post post, Long viewerId) { this.viewerId = viewerId; - this.posterId = post.getUser().getUserId(); + this.poster = new WalkieDto(post.getUser()); this.postId = post.getPostId(); - this.liker = post.getLiker().stream().map(WalkieDto::new).collect(Collectors.toList()); + this.liked = likers.stream().map(WalkieDto::getWalkieId).anyMatch(id -> id.longValue() == viewerId.longValue()); this.photo = post.getPhoto(); this.content = post.getContent(); this.date = post.getDate(); diff --git a/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java index e241394..e2e8aa8 100644 --- a/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java +++ b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java @@ -10,4 +10,6 @@ public interface PostRepositoryCustom { List findCurrentPosts(JPQLQuery following, Long viewerId, Integer pagingSize, Integer pagingStart); List findMyPosts(Long viewerId, Integer pagingSize, Integer pagingStart); + + Long findPostId(String photo, String date); } diff --git a/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java index 6b53d3a..16a4c84 100644 --- a/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java +++ b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java @@ -5,42 +5,62 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import com.whyranoid.walkie.dto.PostDto; import com.whyranoid.walkie.dto.QPostDto; +import com.whyranoid.walkie.dto.QWalkieDto; import lombok.RequiredArgsConstructor; +import java.util.ArrayList; import java.util.List; +import static com.querydsl.core.group.GroupBy.groupBy; +import static com.querydsl.core.group.GroupBy.list; import static com.whyranoid.walkie.domain.QPost.post; +import static com.whyranoid.walkie.domain.QPostLike.postLike; +import static com.whyranoid.walkie.domain.QWalkie.walkie; @RequiredArgsConstructor public class PostRepositoryImpl implements PostRepositoryCustom { private final JPAQueryFactory queryFactory; - // TODO: 나의 좋아요 여부 반영 - // TODO: 좋아요 누른 사람 목록 반영 @Override public List findCurrentPosts(JPQLQuery following, Long viewerId, Integer pagingSize, Integer pagingStart) { - return queryFactory - .select(new QPostDto(post, Expressions.asNumber(viewerId))) + return new ArrayList<>(queryFactory .from(post) .where(post.user.userId.in(following)) .orderBy(post.date.desc()) .offset(pagingStart) .limit(pagingSize) - .fetch(); + .leftJoin(postLike).on(postLike.post.postId.eq(post.postId)) + .join(walkie).on(walkie.userId.eq(postLike.liker.userId)) + .transform(groupBy(post.postId).as(new QPostDto( + post, + Expressions.asNumber(viewerId), + list(new QWalkieDto(walkie)) + ))).values()); } - // TODO: 나의 좋아요 여부 반영 - // TODO: 좋아요 누른 사람 목록 반영 @Override public List findMyPosts(Long viewerId, Integer pagingSize, Integer pagingStart) { - return queryFactory - .select(new QPostDto(post, Expressions.asNumber(viewerId))) + return new ArrayList<>(queryFactory .from(post) .where(post.user.userId.eq(viewerId)) .orderBy(post.date.desc()) .offset(pagingStart) .limit(pagingSize) - .fetch(); + .leftJoin(postLike).on(postLike.post.postId.eq(post.postId)) + .join(walkie).on(walkie.userId.eq(postLike.liker.userId)) + .transform(groupBy(post.postId).as(new QPostDto( + post, + Expressions.asNumber(viewerId), + list(new QWalkieDto(walkie)) + ))).values()); + } + + @Override + public Long findPostId(String photo, String date) { + return queryFactory + .select(post.postId) + .where(post.photo.eq(photo).and(post.date.eq(date))) + .fetchOne(); } } diff --git a/src/main/java/com/whyranoid/walkie/service/CommunityService.java b/src/main/java/com/whyranoid/walkie/service/CommunityService.java index a86712d..852903d 100644 --- a/src/main/java/com/whyranoid/walkie/service/CommunityService.java +++ b/src/main/java/com/whyranoid/walkie/service/CommunityService.java @@ -56,6 +56,15 @@ public ApiResponse uploadPost(MultipartFile image, Long walkieId, String content uploadImage(image, imageUrl); post.setPhoto(ApiBaseUrlSingleton.getInstance().getBaseUrl()+ '/' + imageUrl); communityRepository.uploadPost(post); + + // TODO: 셀프좋아요 하지 않아도 되도록 게시글 조회 쿼리 수정 + PostLikeDto selfLike = PostLikeDto.builder() + .likerId(walkieId) + .postId(postRepository.findPostId(post.getPhoto(), post.getDate())) + .build(); + + sendPostLike(selfLike); + return ApiResponse.builder() .status(200) .message("게시글 업로드 완료!")