diff --git a/src/main/java/daybyquest/post/application/GetPostService.java b/src/main/java/daybyquest/post/application/GetPostService.java index a6654f3..8e427d5 100644 --- a/src/main/java/daybyquest/post/application/GetPostService.java +++ b/src/main/java/daybyquest/post/application/GetPostService.java @@ -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; @@ -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); } } diff --git a/src/main/java/daybyquest/post/domain/Post.java b/src/main/java/daybyquest/post/domain/Post.java index 853c586..1277bf9 100644 --- a/src/main/java/daybyquest/post/domain/Post.java +++ b/src/main/java/daybyquest/post/domain/Post.java @@ -70,6 +70,9 @@ public Post(Long userId, Long questId, String content, List images) { validateUserId(); validateImages(); validateContent(); + if (questId == null) { + success(); + } } private void validateUserId() { diff --git a/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java b/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java new file mode 100644 index 0000000..334bc16 --- /dev/null +++ b/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java @@ -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 imageIdentifiers; + + private SimpleQuestResponse quest; + + public PostWithQuestResponse(final ProfileResponse author, final Long id, final String content, + final LocalDateTime updatedAt, final boolean liked, + final List 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; + } + } +} diff --git a/src/main/java/daybyquest/post/presentation/PostCommandApi.java b/src/main/java/daybyquest/post/presentation/PostCommandApi.java index a5c6874..3b5a674 100644 --- a/src/main/java/daybyquest/post/presentation/PostCommandApi.java +++ b/src/main/java/daybyquest/post/presentation/PostCommandApi.java @@ -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; @@ -40,11 +40,11 @@ public PostCommandApi(final SavePostService savePostService, final SwipePostServ @PostMapping("/post") @Authorization - public ResponseEntity savePost(final AccessUser accessUser, + public ResponseEntity savePost(final AccessUser accessUser, @RequestPart SavePostRequest request, @RequestPart List 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); } diff --git a/src/main/java/daybyquest/post/presentation/PostQueryApi.java b/src/main/java/daybyquest/post/presentation/PostQueryApi.java index ea1389d..40ee3cb 100644 --- a/src/main/java/daybyquest/post/presentation/PostQueryApi.java +++ b/src/main/java/daybyquest/post/presentation/PostQueryApi.java @@ -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; @@ -32,9 +32,9 @@ public PostQueryApi(final GetPostService getPostService, @GetMapping("/post/{postId}") @Authorization - public ResponseEntity getPost(final AccessUser accessUser, + public ResponseEntity 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); } diff --git a/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java b/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java index 40a7228..f428c3a 100644 --- a/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java +++ b/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java @@ -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; @@ -56,7 +57,12 @@ private ConstructorExpression 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 diff --git a/src/main/java/daybyquest/post/query/PostData.java b/src/main/java/daybyquest/post/query/PostData.java index 8418b59..079e4cc 100644 --- a/src/main/java/daybyquest/post/query/PostData.java +++ b/src/main/java/daybyquest/post/query/PostData.java @@ -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; @@ -20,13 +21,22 @@ public class PostData { private List 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 images) {