Skip to content

Commit

Permalink
feat : category infra layer 조회 쿼리 QueryDSL 개발, 기존 Repository 이름 변경 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingLeeSeungHoon committed May 3, 2024
1 parent dccc25c commit 93594ed
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.neo.needeachother.category.domain.dto;

import com.neo.needeachother.category.domain.ContentType;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString(callSuper = true)
public class CategoryDetailViewData {
private final String categoryId;
private final String categoryTitle;
private final String categoryType;
private final boolean isHostWriteOnly;
private final boolean isCommentWriteAble;
private final boolean isUsingRateFilter;
private final int voteFilterRate;
private final long postInCategoryCount;

public CategoryDetailViewData(String categoryId, String categoryTitle, String categoryType,
boolean isHostWriteOnly, boolean isCommentWriteAble,
boolean isUsingRateFilter,
int voteFilterRate, long postInCategoryCount) {
this.categoryId = categoryId;
this.categoryTitle = categoryTitle;
this.categoryType = ContentType.convertToContentType(categoryType).name();
this.isHostWriteOnly = isHostWriteOnly;
this.isCommentWriteAble = isCommentWriteAble;
this.isUsingRateFilter = isUsingRateFilter;
this.voteFilterRate = voteFilterRate;
this.postInCategoryCount = postInCategoryCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.neo.needeachother.category.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@Getter
@ToString
@AllArgsConstructor
public class CategoryDetailViewDto {
private String starPageId;
private List<CategoryDetailViewData> viewData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.neo.needeachother.category.domain.dto;

import com.neo.needeachother.category.domain.ContentType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString(callSuper = true)
public class CategoryViewData {
private String categoryId;
private String categoryTitle;
private String categoryType;
private long postCountInCategory;

public CategoryViewData(String categoryId, String categoryTitle, String categoryType, long postCountInCategory){
this.categoryId = categoryId;
this.categoryTitle = categoryTitle;
this.categoryType = ContentType.convertToContentType(categoryType).name();
this.postCountInCategory = postCountInCategory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.neo.needeachother.category.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

@Getter
@Setter
@ToString
@AllArgsConstructor
public class CategoryViewDto {
private String starPageId;
private List<CategoryViewData> viewData;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

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> {
public interface CategoryRepository extends CategoryRepositoryCustom, JpaRepository<Category, CategoryId> {
Optional<Category> findByCategoryIdAndStarPageId(CategoryId categoryId, StarPageId starPageId);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.neo.needeachother.category.domain.repository;

import com.neo.needeachother.category.domain.CategoryId;
import com.neo.needeachother.category.domain.ContentType;
import com.neo.needeachother.category.domain.dto.CategoryDetailViewDto;
import com.neo.needeachother.category.domain.dto.CategoryViewDto;
import com.neo.needeachother.starpage.domain.StarPageId;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

public interface CategoryRepositoryCustom {
Optional<CategoryViewDto> searchCategoryViewByStarPageId(StarPageId starPageId);
Optional<CategoryDetailViewDto> searchCategoryDetailViewByStarPageId(StarPageId starPageId);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.neo.needeachother.category.infra;

import com.neo.needeachother.category.domain.CategoryStatus;
import com.neo.needeachother.category.domain.dto.CategoryDetailViewData;
import com.neo.needeachother.category.domain.dto.CategoryDetailViewDto;
import com.neo.needeachother.category.domain.dto.CategoryViewData;
import com.neo.needeachother.category.domain.dto.CategoryViewDto;
import com.neo.needeachother.category.domain.repository.CategoryRepositoryCustom;
import com.neo.needeachother.common.exception.NEOUnexpectedException;
import com.neo.needeachother.starpage.domain.StarPageId;
import com.querydsl.core.group.GroupBy;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

import static com.neo.needeachother.category.domain.QCategory.category;
import static com.neo.needeachother.post.domain.QStarPagePost.starPagePost;

@Repository
@RequiredArgsConstructor
public class CategoryRepositoryImpl implements CategoryRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Override
public Optional<CategoryViewDto> searchCategoryViewByStarPageId(StarPageId starPageId) {
List<CategoryViewDto> queryResult = jpaQueryFactory
.select(
Expressions.asString(starPageId.getValue()).as("starPageId"),
category.categoryId.value,
category.categoryInformation.categoryTitle,
category.contentType.stringValue(),
starPagePost.count()
)
.from(category)
.innerJoin(starPagePost).on(starPagePost.categoryId.eq(category.categoryId))
.groupBy(category.categoryId)
.where(
categoryEqStarPageId(starPageId),
neCategoryStatusDeleted()
)
.transform(GroupBy.groupBy(category.starPageId)
.list(Projections.constructor(CategoryViewDto.class,
Expressions.asString(starPageId.getValue()).as("starPageId"),
GroupBy.list(
Projections.constructor(CategoryViewData.class,
category.categoryId.value,
category.categoryInformation.categoryTitle,
category.contentType.stringValue(),
starPagePost.count())
)
)));

if (queryResult.size() > 1) {
throw new NEOUnexpectedException("정상적이지 않은 결과, searchCategoryViewByStarPageId");
}

return Optional.ofNullable(queryResult.isEmpty() ? null : queryResult.get(0));
}

@Override
public Optional<CategoryDetailViewDto> searchCategoryDetailViewByStarPageId(StarPageId starPageId) {
List<CategoryDetailViewDto> queryResult = jpaQueryFactory
.select(category, starPagePost.count())
.from(category)
.innerJoin(starPagePost).on(starPagePost.categoryId.eq(category.categoryId))
.groupBy(category.categoryId)
.where(
categoryEqStarPageId(starPageId),
neCategoryStatusDeleted()
).transform(GroupBy.groupBy(category.starPageId)
.list(
Projections.constructor(CategoryDetailViewDto.class,
Expressions.asString(starPageId.getValue()).as("starPageId"),
GroupBy.list(Projections.constructor(CategoryDetailViewData.class,
category.categoryId.value,
category.categoryInformation.categoryTitle,
category.contentType.stringValue(),
category.restriction.onlyHostWriteContent,
category.restriction.isWriteAbleComment,
category.restriction.useCommentRatingFilter,
category.restriction.filteringRate,
starPagePost.count()
))
)
));

if (queryResult.size() > 1) {
throw new NEOUnexpectedException("정상적이지 않은 결과, searchCategoryDetailViewByStarPageId");
}

return Optional.ofNullable(queryResult.isEmpty() ? null : queryResult.get(0));
}

private BooleanExpression categoryEqStarPageId(StarPageId starPageId) {
return category.starPageId.eq(starPageId);
}

private BooleanExpression neCategoryStatusDeleted() {
return category.categoryStatus.ne(CategoryStatus.DELETED);
}
}
Loading

0 comments on commit 93594ed

Please sign in to comment.