Skip to content

Commit

Permalink
Merge pull request #30 from Team-Walkie/mvc/byeonghee
Browse files Browse the repository at this point in the history
[api] κ²Œμ‹œκΈ€ κ°€μ Έμ˜€κΈ°μ— μ’‹μ•„μš” 정보 μΆ”κ°€
  • Loading branch information
bngsh authored Nov 2, 2023
2 parents 5c57624 + b61c771 commit d049d6f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
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

0 comments on commit d049d6f

Please sign in to comment.