Skip to content

Commit

Permalink
[Refactor] 게시물 상세 정보 조회 시 퀘스트 관련 정보를 포함한다 (#73)
Browse files Browse the repository at this point in the history
refator: 게시물 상세 조회에 퀘스트 정보 포함
  • Loading branch information
vectorch9 authored Nov 2, 2023
1 parent de35e71 commit d562bde
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/main/java/daybyquest/post/application/GetPostService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package daybyquest.post.application;

import daybyquest.post.dto.response.PostResponse;
import daybyquest.post.dto.response.PostWithQuestResponse;
import daybyquest.post.query.PostDao;
import daybyquest.post.query.PostData;
import daybyquest.quest.domain.Quests;
import daybyquest.user.query.Profile;
import daybyquest.user.query.ProfileDao;
import org.springframework.stereotype.Service;
Expand All @@ -15,15 +16,18 @@ public class GetPostService {

private final ProfileDao profileDao;

public GetPostService(final PostDao postDao, final ProfileDao profileDao) {
private final Quests quests;

public GetPostService(final PostDao postDao, final ProfileDao profileDao, final Quests quests) {
this.postDao = postDao;
this.profileDao = profileDao;
this.quests = quests;
}

@Transactional(readOnly = true)
public PostResponse invoke(final Long loginId, final Long postId) {
public PostWithQuestResponse invoke(final Long loginId, final Long postId) {
final PostData postData = postDao.getByPostId(loginId, postId);
final Profile profile = profileDao.getById(loginId, postData.getUserId());
return PostResponse.of(postData, profile);
return PostWithQuestResponse.of(postData, profile);
}
}
3 changes: 3 additions & 0 deletions src/main/java/daybyquest/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public Post(Long userId, Long questId, String content, List<Image> images) {
validateUserId();
validateImages();
validateContent();
if (questId == null) {
success();
}
}

private void validateUserId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package daybyquest.post.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import daybyquest.image.vo.Image;
import daybyquest.post.domain.PostState;
import daybyquest.post.query.PostData;
import daybyquest.user.dto.response.ProfileResponse;
import daybyquest.user.query.Profile;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class PostWithQuestResponse {

private ProfileResponse author;

private Long id;

private String content;

@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime updatedAt;

private boolean liked;

private List<String> imageIdentifiers;

private SimpleQuestResponse quest;

public PostWithQuestResponse(final ProfileResponse author, final Long id, final String content,
final LocalDateTime updatedAt, final boolean liked,
final List<String> imageIdentifiers, final SimpleQuestResponse quest) {
this.author = author;
this.id = id;
this.content = content;
this.updatedAt = updatedAt;
this.liked = liked;
this.imageIdentifiers = imageIdentifiers;
this.quest = quest;
if (quest.getQuestId() == null) {
this.quest = null;
}
}

public static PostWithQuestResponse of(final PostData postData, final Profile profile) {
return new PostWithQuestResponse(
ProfileResponse.of(profile),
postData.getId(), postData.getContent(), postData.getUpdatedAt(), postData.isLiked(),
postData.getImages().stream().map(Image::getImageIdentifier).toList(),
new SimpleQuestResponse(postData.getQuestId(), postData.getQuestTitle(), postData.getState())
);
}

@Getter
@NoArgsConstructor
private static class SimpleQuestResponse {

private Long questId;

private String title;

private PostState state;

public SimpleQuestResponse(final Long questId, final String title, final PostState state) {
this.questId = questId;
this.title = title;
this.state = state;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import daybyquest.post.application.SwipePostService;
import daybyquest.post.dto.request.JudgePostRequest;
import daybyquest.post.dto.request.SavePostRequest;
import daybyquest.post.dto.response.PostResponse;
import daybyquest.post.dto.response.PostWithQuestResponse;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -40,11 +40,11 @@ public PostCommandApi(final SavePostService savePostService, final SwipePostServ

@PostMapping("/post")
@Authorization
public ResponseEntity<PostResponse> savePost(final AccessUser accessUser,
public ResponseEntity<PostWithQuestResponse> savePost(final AccessUser accessUser,
@RequestPart SavePostRequest request,
@RequestPart List<MultipartFile> files) {
final Long postId = savePostService.invoke(accessUser.getId(), request, files);
final PostResponse response = getPostService.invoke(accessUser.getId(), postId);
final PostWithQuestResponse response = getPostService.invoke(accessUser.getId(), postId);
return ResponseEntity.ok(response);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/daybyquest/post/presentation/PostQueryApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import daybyquest.post.application.GetPostFromFollowingService;
import daybyquest.post.application.GetPostService;
import daybyquest.post.dto.response.PagePostsResponse;
import daybyquest.post.dto.response.PostResponse;
import daybyquest.post.dto.response.PostWithQuestResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -32,9 +32,9 @@ public PostQueryApi(final GetPostService getPostService,

@GetMapping("/post/{postId}")
@Authorization
public ResponseEntity<PostResponse> getPost(final AccessUser accessUser,
public ResponseEntity<PostWithQuestResponse> getPost(final AccessUser accessUser,
@PathVariable final Long postId) {
final PostResponse response = getPostService.invoke(accessUser.getId(), postId);
final PostWithQuestResponse response = getPostService.invoke(accessUser.getId(), postId);
return ResponseEntity.ok(response);
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static daybyquest.image.vo.QImage.image;
import static daybyquest.like.domain.QPostLike.postLike;
import static daybyquest.post.domain.QPost.post;
import static daybyquest.quest.domain.QQuest.quest;
import static daybyquest.relation.domain.QFollow.follow;

import com.querydsl.core.types.ConstructorExpression;
Expand Down Expand Up @@ -56,7 +57,12 @@ private ConstructorExpression<PostData> postDataProjection(final Long userId) {
post.updatedAt,
JPAExpressions.selectFrom(postLike)
.where(postLike.userId.eq(userId).and(postLike.postId.eq(post.id)))
.exists());
.exists(),
post.questId,
JPAExpressions.select(quest.title)
.from(quest)
.where(quest.id.eq(post.questId)),
post.state);
}

@Override
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/daybyquest/post/query/PostData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package daybyquest.post.query;

import daybyquest.image.vo.Image;
import daybyquest.post.domain.PostState;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Getter;
Expand All @@ -20,13 +21,22 @@ public class PostData {

private List<Image> images;

private final Long questId;

private final String questTitle;

private final PostState state;

public PostData(final Long userId, final Long id, final String content, final LocalDateTime updatedAt,
final boolean liked) {
final boolean liked, final Long questId, final String questTitle, final PostState state) {
this.userId = userId;
this.id = id;
this.content = content;
this.updatedAt = updatedAt;
this.liked = liked;
this.questId = questId;
this.questTitle = questTitle;
this.state = state;
}

public void setImages(final List<Image> images) {
Expand Down

0 comments on commit d562bde

Please sign in to comment.