Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] 게시글 가져오기에 좋아요 정보 추가 #30

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/main/java/com/whyranoid/walkie/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Walkie> liker = new ArrayList<>();

// @OneToOne(optional = false) // optional = false 안해주면 history가 겹치는 경우가 발생 시 에러
// @JoinColumn(name = "history", nullable = false)
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/com/whyranoid/walkie/dto/PostDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -27,7 +27,7 @@ public class PostDto {
private Boolean liked = false;

@Schema(description = "[응답] 좋아요 누른 유저 리스트")
private List<WalkieDto> liker;
private List<WalkieDto> likers = new ArrayList<>();

@Schema(description = "[응답] 사진파일 URI")
private String photo;
Expand All @@ -44,12 +44,26 @@ public class PostDto {
@Schema(description = "[응답] 기록 데이터")
private String historyContent;

@QueryProjection
public PostDto(Post post, Long viewerId, List<WalkieDto> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface PostRepositoryCustom {
List<PostDto> findCurrentPosts(JPQLQuery<Long> following, Long viewerId, Integer pagingSize, Integer pagingStart);

List<PostDto> findMyPosts(Long viewerId, Integer pagingSize, Integer pagingStart);

Long findPostId(String photo, String date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostDto> findCurrentPosts(JPQLQuery<Long> 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<PostDto> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("게시글 업로드 완료!")
Expand Down