-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: 관리자 축제 조회 기능 추가 * feat: 관리자 축제 조회 API 추가
- Loading branch information
1 parent
c41210d
commit 19faf78
Showing
8 changed files
with
540 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/festago/admin/application/AdminFestivalV1QueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.festago.admin.application; | ||
|
||
import com.festago.admin.dto.AdminFestivalV1Response; | ||
import com.festago.admin.repository.AdminFestivalV1QueryDslRepository; | ||
import com.festago.common.querydsl.SearchCondition; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class AdminFestivalV1QueryService { | ||
|
||
private final AdminFestivalV1QueryDslRepository adminFestivalV1QueryDslRepository; | ||
|
||
public Page<AdminFestivalV1Response> findAll(SearchCondition searchCondition) { | ||
return adminFestivalV1QueryDslRepository.findAll(searchCondition); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/festago/admin/dto/AdminFestivalV1Response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.festago.admin.dto; | ||
|
||
import com.querydsl.core.annotations.QueryProjection; | ||
import java.time.LocalDate; | ||
|
||
public record AdminFestivalV1Response( | ||
Long id, | ||
String name, | ||
String schoolName, | ||
LocalDate startDate, | ||
LocalDate endDate, | ||
long stageCount | ||
) { | ||
|
||
@QueryProjection | ||
public AdminFestivalV1Response { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
backend/src/main/java/com/festago/admin/repository/AdminFestivalV1QueryDslRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.festago.admin.repository; | ||
|
||
import static com.festago.festival.domain.QFestival.festival; | ||
import static com.festago.school.domain.QSchool.school; | ||
import static com.festago.stage.domain.QStage.stage; | ||
|
||
import com.festago.admin.dto.AdminFestivalV1Response; | ||
import com.festago.admin.dto.QAdminFestivalV1Response; | ||
import com.festago.common.querydsl.OrderSpecifierUtils; | ||
import com.festago.common.querydsl.QueryDslHelper; | ||
import com.festago.common.querydsl.SearchCondition; | ||
import com.querydsl.core.types.OrderSpecifier; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.util.StringUtils; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class AdminFestivalV1QueryDslRepository { | ||
|
||
private final QueryDslHelper queryDslHelper; | ||
|
||
public Page<AdminFestivalV1Response> findAll(SearchCondition searchCondition) { | ||
Pageable pageable = searchCondition.pageable(); | ||
String searchFilter = searchCondition.searchFilter(); | ||
String searchKeyword = searchCondition.searchKeyword(); | ||
return queryDslHelper.applyPagination(pageable, | ||
queryFactory -> queryFactory.select( | ||
new QAdminFestivalV1Response( | ||
festival.id, | ||
festival.name, | ||
school.name, | ||
festival.startDate, | ||
festival.endDate, | ||
stage.count() | ||
)) | ||
.from(festival) | ||
.innerJoin(school).on(school.id.eq(festival.school.id)) | ||
.leftJoin(stage).on(stage.festival.id.eq(festival.id)) | ||
.where(applySearchFilter(searchFilter, searchKeyword)) | ||
.groupBy(festival.id) | ||
.orderBy(getOrderSpecifier(pageable.getSort())) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()), | ||
queryFactory -> queryFactory.select(festival.count()) | ||
.from(festival) | ||
.where(applySearchFilter(searchFilter, searchKeyword))); | ||
} | ||
|
||
private BooleanExpression applySearchFilter(String searchFilter, String searchKeyword) { | ||
return switch (searchFilter) { | ||
case "id" -> eqId(searchKeyword); | ||
case "name" -> containsName(searchKeyword); | ||
case "schoolName" -> containsSchoolName(searchKeyword); | ||
default -> null; | ||
}; | ||
} | ||
|
||
private BooleanExpression eqId(String searchKeyword) { | ||
if (StringUtils.hasText(searchKeyword)) { | ||
return festival.id.stringValue().eq(searchKeyword); | ||
} | ||
return null; | ||
} | ||
|
||
private BooleanExpression containsName(String searchKeyword) { | ||
if (StringUtils.hasText(searchKeyword)) { | ||
return festival.name.contains(searchKeyword); | ||
} | ||
return null; | ||
} | ||
|
||
private BooleanExpression containsSchoolName(String searchKeyword) { | ||
if (StringUtils.hasText(searchKeyword)) { | ||
return school.name.contains(searchKeyword); | ||
} | ||
return null; | ||
} | ||
|
||
private OrderSpecifier<?> getOrderSpecifier(Sort sort) { | ||
return sort.stream() | ||
.findFirst() | ||
.map(it -> switch (it.getProperty()) { | ||
case "id" -> OrderSpecifierUtils.of(it.getDirection(), festival.id); | ||
case "name" -> OrderSpecifierUtils.of(it.getDirection(), festival.name); | ||
case "schoolName" -> OrderSpecifierUtils.of(it.getDirection(), school.name); | ||
case "startDate" -> OrderSpecifierUtils.of(it.getDirection(), festival.startDate); | ||
case "endDate" -> OrderSpecifierUtils.of(it.getDirection(), festival.endDate); | ||
default -> OrderSpecifierUtils.NULL; | ||
}) | ||
.orElse(OrderSpecifierUtils.NULL); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/festago/common/querydsl/OrderSpecifierUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.festago.common.querydsl; | ||
|
||
import com.querydsl.core.types.Expression; | ||
import com.querydsl.core.types.NullExpression; | ||
import com.querydsl.core.types.Order; | ||
import com.querydsl.core.types.OrderSpecifier; | ||
import org.springframework.data.domain.Sort; | ||
|
||
public class OrderSpecifierUtils { | ||
|
||
public static final OrderSpecifier<?> NULL = new OrderSpecifier(Order.ASC, NullExpression.DEFAULT, | ||
OrderSpecifier.NullHandling.Default); | ||
|
||
private OrderSpecifierUtils() { | ||
} | ||
|
||
public static OrderSpecifier of(Sort.Direction direction, Expression<?> target) { | ||
return new OrderSpecifier(direction.isAscending() ? Order.ASC : Order.DESC, target); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/com/festago/common/querydsl/QueryDslHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.festago.common.querydsl; | ||
|
||
import com.querydsl.core.types.Expression; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class QueryDslHelper { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
public <T> JPAQuery<T> select(Expression<T> expr) { | ||
return queryFactory.select(expr); | ||
} | ||
|
||
public <T> Optional<T> fetchOne(Function<JPAQueryFactory, JPAQuery<T>> queryFunction) { | ||
JPAQuery<T> query = queryFunction.apply(queryFactory); | ||
return Optional.ofNullable(query.fetchOne()); | ||
} | ||
|
||
public <T> Page<T> applyPagination( | ||
Pageable pageable, | ||
Function<JPAQueryFactory, JPAQuery<T>> contentQueryFunction, | ||
Function<JPAQueryFactory, JPAQuery<Long>> countQueryFunction | ||
) { | ||
List<T> content = contentQueryFunction.apply(queryFactory).fetch(); | ||
JPAQuery<Long> countQuery = countQueryFunction.apply(queryFactory); | ||
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); | ||
} | ||
} |
Oops, something went wrong.