Skip to content

Commit

Permalink
feat : 조회용 DTO 추가, QueryDSL JOIN 최적화, 스타페이지 조회 API 표현, 응용 계층 개발 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingLeeSeungHoon committed Apr 22, 2024
1 parent 2942f36 commit 881672b
Show file tree
Hide file tree
Showing 41 changed files with 923 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import com.neo.needeachother.category.domain.Category;
import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.category.domain.repository.CategoryRepository;
import com.neo.needeachother.starpage.domain.StarPageId;

public final class CategoryServiceHelper {
public static Category findExistingCategory(CategoryRepository repo, CategoryId categoryId){
return repo.findById(categoryId)
.orElseThrow();
}

public static Category findExistingCategoryByStarPageId(CategoryRepository repo, CategoryId categoryId, StarPageId starPageId){
return repo.findByCategoryIdAndStarPageId(categoryId, starPageId)
.orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Category {

@Getter
@EmbeddedId
@Column(name = "category_id")
private CategoryId categoryId;

@Getter
Expand All @@ -33,10 +35,12 @@ public class Category {
@Convert(converter = CategoryStatusConverter.class)
private CategoryStatus categoryStatus;

@Getter
@Column(name = "content_type")
@Convert(converter = ContentTypeConverter.class)
private ContentType contentType;

@Getter
@Embedded
@AttributeOverride(name = "categoryTitle", column = @Column(name = "title"))
private CategoryInformation categoryInformation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.neo.needeachother.category.domain.Category;
import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.category.domain.ContentType;
import com.neo.needeachother.starpage.domain.StarPageId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface CategoryRepository extends CategoryCustomRepository, JpaRepository<Category, CategoryId> {
Optional<Category> findByCategoryIdAndStarPageId(CategoryId categoryId, StarPageId starPageId);
}

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

import com.querydsl.jpa.JPQLTemplates;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
Expand All @@ -19,7 +20,7 @@ public class NEOQueryDslConfig {

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
return new JPAQueryFactory(JPQLTemplates.DEFAULT, entityManager);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class StarPagePost extends NEOTimeDefaultEntity {
private Long id;

@Embedded
@Column(name = "category_id")
private CategoryId categoryId;

@Column(name = "post_title")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.starpage.domain.*;
import com.neo.needeachother.starpage.domain.domainservice.CategoryVerifyForLayoutService;
import com.neo.needeachother.starpage.domain.repository.StarPageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.neo.needeachother.starpage.application.StarPageServiceHelper.*;

@Service
@RequiredArgsConstructor
public class LayoutService {
public class LayoutManagementService {

private final StarPageRepository starPageRepository;
private final CategoryVerifyForLayoutService categoryVerifyForLayoutService;

@Transactional
public void appendLayoutInStarPage(StarPageId id, String email, CategoryId categoryId){
StarPage foundStarPage = findExistingStarPage(starPageRepository, id);
// foundStarPage.appendCategoricalLayoutLine(NEOMember.of(email));
foundStarPage.appendCategoricalLayoutLine(NEOMember.of(email), categoryVerifyForLayoutService, categoryId, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@
import com.neo.needeachother.starpage.domain.repository.StarPageRepository;

public final class StarPageServiceHelper {
public static StarPage findExistingStarPage(StarPageRepository repo, StarPageId starPageId){
public static StarPage findExistingStarPage(StarPageRepository repo, StarPageId starPageId) {
return repo.findById(starPageId)
.orElseThrow(() -> new NEOExpectedException(NEODomainType.STARPAGE,
NEOErrorCode.NOT_EXIST_STARPAGE,
NEOErrorCode.NOT_EXIST_STARPAGE.getErrorDescription()));
}

public static StarPage findExistingStarPageWithLayout(StarPageRepository repo, StarPageId starPageId) {
return repo.findStarPageWithLayout(starPageId)
.orElseThrow(() -> new NEOExpectedException(NEODomainType.STARPAGE,
NEOErrorCode.NOT_EXIST_STARPAGE,
NEOErrorCode.NOT_EXIST_STARPAGE.getErrorDescription()));
}

public static StarPage findExistingStarPageWithTopViewInformation(StarPageRepository repo, StarPageId starPageId){
return repo.findStarPageWithInformation(starPageId)
.orElseThrow(() -> new NEOExpectedException(NEODomainType.STARPAGE,
NEOErrorCode.NOT_EXIST_STARPAGE,
NEOErrorCode.NOT_EXIST_STARPAGE.getErrorDescription()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.neo.needeachother.starpage.application;

import com.neo.needeachother.starpage.application.dto.StarPageTopViewData;
import com.neo.needeachother.starpage.application.dto.StarPageViewData;
import com.neo.needeachother.starpage.application.mapper.StarPageViewDataMapper;
import com.neo.needeachother.starpage.domain.StarPage;
import com.neo.needeachother.starpage.domain.StarPageId;
import com.neo.needeachother.starpage.domain.dto.LayoutHeadLine;
import com.neo.needeachother.starpage.domain.repository.StarPageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.neo.needeachother.starpage.application.StarPageServiceHelper.*;

@Service
@RequiredArgsConstructor
public class StarPageViewDataService {

private final StarPageRepository starPageRepository;
private final StarPageViewDataMapper starPageViewDataMapper;

public StarPageTopViewData getStarPageTopViewData(String starPageId){
// 스타페이지 정보 포함된 스타페이지
StarPage foundStarPage = findExistingStarPageWithTopViewInformation(starPageRepository, new StarPageId(starPageId));

return starPageViewDataMapper.mapToTopViewData(foundStarPage);
}

public StarPageViewData getStarPageLayoutViewData(String starPageId){
// 레이아웃이 조인된 스타페이지
StarPage foundStarPage = findExistingStarPageWithLayout(starPageRepository, new StarPageId(starPageId));

// 레이아웃 순서에 따른 순차적인 뷰를 구성하는 데이터
List<LayoutHeadLine> starPageViewLayoutList = foundStarPage.getLayoutHeadLines(starPageRepository);

return starPageViewDataMapper.mapToViewData(foundStarPage.getStarPageId(), starPageViewLayoutList);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.neo.needeachother.starpage.application.dto;

import lombok.Builder;
import lombok.Getter;

import java.util.List;
import java.util.Map;

@Getter
@Builder
public class StarPageLayoutViewData {
private String layoutTitle;
private boolean hasTap;
private Map<String, List<StarPageLayoutViewTileData>> layoutTileDataWithTap;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.neo.needeachother.starpage.application.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Map;

@Getter
@Builder
public class StarPageLayoutViewTileData {
private Long postId;
private int likeCount;
private String categoryType;
private String categoryTitle;
private String author;
private String title;
private String representativeImage;
private String question;
private String leftExample;
private String rightExample;
private int leftCount;
private int rightCount;
private int leftRate;
private int rightRate;
private Map<String, Integer> optionCount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.neo.needeachother.starpage.application.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Map;

@Getter
@Builder
public class StarPageTopViewData {
private String starPageId;
private String hostActiveName;
private String starPageIntroduce;
private List<String> starTypes;
private Map<String, String> starUrl;
private String topRepresentativeImageUrl;
private String profileImageUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.neo.needeachother.starpage.application.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@Builder
public class StarPageViewData {
private String starPageId;
private List<StarPageLayoutViewData> layoutViewData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.neo.needeachother.starpage.application.mapper;

public interface Mapper<IN, OUT>{
OUT map(IN input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.neo.needeachother.starpage.application.mapper;

import com.neo.needeachother.starpage.application.dto.StarPageLayoutViewData;
import com.neo.needeachother.starpage.application.dto.StarPageLayoutViewTileData;
import com.neo.needeachother.starpage.domain.dto.LayoutHeadLine;
import com.neo.needeachother.starpage.domain.dto.RepresentativeArticleHeadLine;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@RequiredArgsConstructor
public class StarPageLayoutViewDataMapper implements Mapper<LayoutHeadLine, StarPageLayoutViewData> {

private final StarPageLayoutViewTileDataMapper starPageLayoutViewTileDataMapper;

@Override
public StarPageLayoutViewData map(LayoutHeadLine input) {
boolean hasTap = input.getLayoutContents().get(0) instanceof RepresentativeArticleHeadLine;
return StarPageLayoutViewData.builder()
.layoutTitle(input.getLayoutTitle())
.hasTap(hasTap)
.layoutTileDataWithTap(getStarPageLayoutViewTileDataMap(hasTap, input))
.build();
}

private Map<String, List<StarPageLayoutViewTileData>> getStarPageLayoutViewTileDataMap(boolean hasTap, LayoutHeadLine layoutHeadLine) {
Map<String, List<StarPageLayoutViewTileData>> tileMap = new HashMap<>();
if (hasTap) {
layoutHeadLine.getLayoutContents()
.stream()
.map(starPageHeadLine -> (RepresentativeArticleHeadLine) starPageHeadLine)
.forEach(representativeArticleHeadLine -> {
List<StarPageLayoutViewTileData> dataList = tileMap.getOrDefault(representativeArticleHeadLine.getTapName(), new ArrayList<>());
dataList.add(starPageLayoutViewTileDataMapper.map(representativeArticleHeadLine));
tileMap.put(representativeArticleHeadLine.getTapName(), dataList);
});
} else {
tileMap.put(layoutHeadLine.getLayoutTitle(), layoutHeadLine.getLayoutContents()
.stream()
.map(starPageLayoutViewTileDataMapper::map)
.toList());
}
return tileMap;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.neo.needeachother.starpage.application.mapper;

import com.neo.needeachother.starpage.application.dto.StarPageLayoutViewTileData;
import com.neo.needeachother.starpage.domain.dto.*;
import org.springframework.stereotype.Component;

@Component
public class StarPageLayoutViewTileDataMapper implements Mapper<StarPageHeadLine, StarPageLayoutViewTileData> {

@Override
public StarPageLayoutViewTileData map(StarPageHeadLine input) {
StarPageLayoutViewTileData.StarPageLayoutViewTileDataBuilder builder = StarPageLayoutViewTileData
.builder()
.postId(input.getPostId())
.likeCount(input.getLikeCount())
.categoryType(input.getCategoryType());

if (input instanceof CommonPostHeadLine) {
builder.author(((CommonPostHeadLine) input).getAuthor())
.title(((CommonPostHeadLine) input).getTitle())
.representativeImage(((CommonPostHeadLine) input).getRepresentativeImage());
} else if (input instanceof GoldBalancePostHeadLine) {
builder.question(((GoldBalancePostHeadLine) input).getQuestion())
.leftExample(((GoldBalancePostHeadLine) input).getLeftExample())
.rightExample(((GoldBalancePostHeadLine) input).getRightExample())
.leftCount(((GoldBalancePostHeadLine) input).getLeftCount())
.rightCount(((GoldBalancePostHeadLine) input).getRightCount())
.leftRate(((GoldBalancePostHeadLine) input).getLeftRate())
.rightRate(((GoldBalancePostHeadLine) input).getRightRate());
} else if (input instanceof ImagePostHeadLine) {
builder.representativeImage(((ImagePostHeadLine) input).getRepresentativeImage());
} else if (input instanceof VotePostHeadLine) {
builder.question(((VotePostHeadLine) input).getQuestion())
.optionCount(((VotePostHeadLine) input).getOptionCount());
} else if (input instanceof RepresentativeArticleHeadLine) {
builder.author(((RepresentativeArticleHeadLine) input).getAuthor())
.categoryTitle(((RepresentativeArticleHeadLine) input).getCategoryTitle())
.title(((RepresentativeArticleHeadLine) input).getTitle())
.representativeImage(((RepresentativeArticleHeadLine) input).getRepresentativeImage());
}

return builder.build();
}

}
Loading

0 comments on commit 881672b

Please sign in to comment.