Skip to content

Commit

Permalink
feat : post domain search query dev, application & presentation layer…
Browse files Browse the repository at this point in the history
… deving (#53)
  • Loading branch information
CodingLeeSeungHoon committed May 7, 2024
1 parent 5c84440 commit a8e58f5
Show file tree
Hide file tree
Showing 45 changed files with 1,071 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.StarPagePost;
import com.neo.needeachother.post.domain.domainservice.PostFeatureUseAbleQualificationService;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.StarPagePost;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.StarPagePost;
import com.neo.needeachother.post.domain.domainservice.PostFeatureUseAbleQualificationService;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.StarPagePost;
import com.neo.needeachother.post.domain.domainservice.PostFeatureUseAbleQualificationService;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.StarPagePost;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.post.domain.dto.PostSearchCondition;
import com.neo.needeachother.post.domain.dto.StarPagePostHeadlineDto;
import com.neo.needeachother.post.domain.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PostByCategoryViewService {

private final PostRepository postRepository;

public Slice<StarPagePostHeadlineDto> getPostPageByCategoryWithInfiniteScroll(String categoryId, Long lastPostId, Pageable pageable,
boolean orderByCreatedAt, boolean orderByLikeCount, boolean onlySearchHostWritten, boolean onlySearchHostHearted) {

return postRepository.searchPostHeadlineByCategoryIdWithNoOffset(
CategoryId.of(categoryId),
PostSearchCondition.of(orderByCreatedAt, orderByLikeCount, onlySearchHostWritten, onlySearchHostHearted),
lastPostId,
pageable);
}

public Page<StarPagePostHeadlineDto> getPostPageByCategoryWithPaging(String categoryId, Pageable pageable,
boolean orderByCreatedAt, boolean orderByLikeCount, boolean onlySearchHostWritten, boolean onlySearchHostHearted) {
return postRepository.searchPostHeadlineByCategoryIdWithPaging(
CategoryId.of(categoryId),
PostSearchCondition.of(orderByCreatedAt, orderByLikeCount, onlySearchHostWritten, onlySearchHostHearted),
pageable
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.common.exception.NEOUnexpectedException;
import com.neo.needeachother.post.application.dto.PostDetailDto;
import com.neo.needeachother.post.domain.*;
import com.neo.needeachother.post.domain.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PostDetailViewService {

private final PostRepository postRepository;

public PostDetailDto getPostDetailView(String contentType, Long postId) {
PostType postType = PostType.valueOf(contentType);
switch (postType) {
case COMMON -> {
return getCommonPostDetailView(postId);
}
case ALBUM -> {
return getAlbumPostDetailView(postId);
}
case GOLD_BALANCE -> {
return getGoldBalancePostDetailView(postId);
}
case VOTE -> {
return getVotePostDetailView(postId);
}
default -> throw new NEOUnexpectedException("적절하지 않은 컨텐츠 타입의 입력");
}
}

private PostDetailDto getCommonPostDetailView(Long postId) {
return postRepository.findCommonPostById(postId)
.map(CommonPost::toPostDetailDto)
// TODO : orElseThrow로 변경
.orElse(null);
}

private PostDetailDto getAlbumPostDetailView(Long postId) {
return postRepository.findAlbumPostById(postId)
.map(AlbumPost::toPostDetailDto)
// TODO : orElseThrow로 변경
.orElse(null);
}

private PostDetailDto getGoldBalancePostDetailView(Long postId) {
return postRepository.findGoldBalancePostById(postId)
.map(GoldBalancePost::toPostDetailDto)
// TODO : orElseThrow로 변경
.orElse(null);
}

private PostDetailDto getVotePostDetailView(Long postId) {
return postRepository.findVotePostById(postId)
.map(VotePost::toPostDetailDto)
// TODO : orElseThrow로 변경
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.neo.needeachother.common.exception.NEOUnexpectedException;
import com.neo.needeachother.post.domain.*;
import com.neo.needeachother.post.domain.repository.PostRepository;

import java.util.Optional;

public final class PostServiceHelper {
public static CommonPost findExistingCommonPost(PostRepository repository, Long postId){
Expand Down Expand Up @@ -37,7 +40,8 @@ public static VotePost findExistingVotePost(PostRepository repository, Long post
}

public static StarPagePost findExistingStarPagePost(PostRepository repository, Long postId){
return repository.findById(postId)
repository.findById(postId);
return repository.findById(postId)
.orElseThrow(() -> new NEOUnexpectedException("포스트 아이디와 매칭되는 포스트가 없습니다."));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.StarPagePost;
import com.neo.needeachother.post.domain.domainservice.PostFeatureUseAbleQualificationService;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.neo.needeachother.post.application;

import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.post.domain.Author;
import com.neo.needeachother.post.domain.CommonPost;
import com.neo.needeachother.post.domain.CommonPostParagraph;
import com.neo.needeachother.post.domain.PostRepository;
import com.neo.needeachother.post.domain.*;
import com.neo.needeachother.post.domain.repository.PostRepository;
import com.neo.needeachother.post.domain.domainservice.CreatePostByCategoryService;
import com.neo.needeachother.post.presentation.dto.CommonPostParagraphRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,10 +19,43 @@ public class WritePostService {
private final CreatePostByCategoryService createPostByCategoryService;

@Transactional
public void writeCommonPost(String title, Author author, CategoryId categoryId, List<CommonPostParagraph> paragraphs){
CommonPost createdCommonPost = createPostByCategoryService.createCommonPost(title, author, categoryId, paragraphs);
public void writeCommonPost(String title, String authorName, String authorEmail, String categoryId, List<CommonPostParagraphRequest> paragraphs) {
CommonPost createdCommonPost = createPostByCategoryService.createCommonPost(
title,
Author.of(authorName, authorEmail),
CategoryId.of(categoryId),
paragraphs.stream()
.map(paragraph -> CommonPostParagraph.of(paragraph.getBody(), paragraph.getParagraphType()))
.toList());
postRepository.save(createdCommonPost);
}

@Transactional
public void writeAlbumPost(String title, String authorName, String authorEmail, String categoryId, String imagePath) {
AlbumPost createdAlbumPost = createPostByCategoryService.createAlbumPost(
title, Author.of(authorName, authorEmail), AlbumImage.of(imagePath), CategoryId.of(categoryId));
postRepository.save(createdAlbumPost);
}

@Transactional
public void writeGoldBalancePost(String title, String authorName, String authorEmail, String categoryId, String question,
String leftExample, String rightExample) {
GoldBalancePost createdGoldBalancePost = createPostByCategoryService.createGoldBalancePost(
title, Author.of(authorName, authorEmail), question, leftExample, rightExample, CategoryId.of(categoryId));
postRepository.save(createdGoldBalancePost);
}

@Transactional
public void writeVotePost(String title, String authorName, String authorEmail, String categoryId, String question,
int timeToLive, List<String> voteOptionNameList) {

List<VoteItem> voteItemList = voteOptionNameList.stream()
.map(VoteItem::of)
.toList();

VotePost createdVotePost = createPostByCategoryService.createVotePost(
title, Author.of(authorName, authorEmail), question, timeToLive, voteItemList, CategoryId.of(categoryId));

postRepository.save(createdVotePost);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.neo.needeachother.post.application.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class CommonPostParagraphDto {
private String paragraphType;
private String body;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.neo.needeachother.post.application.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PostDetailDto {

private Long postId;
private String categoryId;
private String title;
private String authorName;
private String status;
private int likeCount;
private boolean hostHeart;
private LocalDateTime exposureAt;
private String postType;

/* common & image post data */
private String representativeImage;

/* only common post data */
private List<CommonPostParagraphDto> paragraph;

/* GoldBalance, Vote post data */
private String question;
private Map<String, VoteAblePostOptionDetailDto> options;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.neo.needeachother.post.application.dto;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class VoteAblePostOptionDetailDto {
private String optionName;
private Integer optionCount;
private Integer optionRate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AlbumImage {

@Getter(value = AccessLevel.PROTECTED)
@Column(name = "image_path")
private String path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.category.domain.ContentType;
import com.neo.needeachother.post.application.dto.PostDetailDto;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -29,4 +30,20 @@ public AlbumPost of(CategoryId categoryId, String title, Author author, PostStat
AlbumImage albumImage){
return new AlbumPost(categoryId, title, author, status, albumImage);
}

@Override
public PostDetailDto toPostDetailDto() {
return PostDetailDto.builder()
.postId(this.getId())
.categoryId(this.getCategoryId().getValue())
.title(this.getTitle())
.authorName(this.getAuthor().getAuthorName())
.status(this.getStatus().name())
.likeCount(this.getLikeCount())
.hostHeart(this.isHostHeart())
.exposureAt(this.getExposureAt())
.postType(this.getPostType().name())
.representativeImage(this.image.getPath())
.build();
}
}
Loading

0 comments on commit a8e58f5

Please sign in to comment.