-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from TEAM-DHS/feat/program
[FEAT] 행사 리스트 조회 API
- Loading branch information
Showing
12 changed files
with
209 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/efub/dhs/domain/program/dto/request/ProgramListRequestDto.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.efub.dhs.domain.program.dto.request; | ||
|
||
import java.util.List; | ||
|
||
import com.efub.dhs.domain.program.entity.Category; | ||
import com.efub.dhs.domain.program.entity.Sort; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ProgramListRequestDto { | ||
private String keyword; | ||
private Sort sort; | ||
private List<Category> category; | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/efub/dhs/domain/program/entity/Sort.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,13 @@ | ||
package com.efub.dhs.domain.program.entity; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public enum Sort { | ||
|
||
NEW("최신순"), | ||
POPULAR("인기순"), | ||
DEADLINE("마감임박순"); | ||
|
||
private final String description; | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/efub/dhs/domain/program/repository/ProgramRepositoryCustom.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,12 @@ | ||
package com.efub.dhs.domain.program.repository; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import com.efub.dhs.domain.program.dto.request.ProgramListRequestDto; | ||
import com.efub.dhs.domain.program.entity.Program; | ||
|
||
public interface ProgramRepositoryCustom { | ||
|
||
Page<Program> findProgramListByFilter(ProgramListRequestDto requestDto, Pageable pageable); | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/com/efub/dhs/domain/program/repository/ProgramRepositoryImpl.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,83 @@ | ||
package com.efub.dhs.domain.program.repository; | ||
|
||
import static com.efub.dhs.domain.program.entity.QProgram.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.efub.dhs.domain.program.dto.request.ProgramListRequestDto; | ||
import com.efub.dhs.domain.program.entity.Category; | ||
import com.efub.dhs.domain.program.entity.Program; | ||
import com.efub.dhs.domain.program.entity.Sort; | ||
import com.querydsl.core.types.OrderSpecifier; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ProgramRepositoryImpl implements ProgramRepositoryCustom { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Page<Program> findProgramListByFilter(ProgramListRequestDto requestDto, Pageable pageable) { | ||
List<Program> programList; | ||
|
||
OrderSpecifier<Long> defaultSpecifier = program.programId.asc(); | ||
OrderSpecifier<LocalDateTime> newSpecifier = program.createdDate.desc(); | ||
OrderSpecifier<Long> popularSpecifier = program.likeNumber.desc(); | ||
OrderSpecifier<LocalDateTime> deadlineSpecifier = program.deadline.asc(); | ||
|
||
JPAQuery<Program> foundQuery = findQuery(requestDto, pageable); | ||
|
||
Sort sort = requestDto.getSort(); | ||
if (sort == null) { | ||
programList = foundQuery.orderBy(defaultSpecifier).fetch(); | ||
} else if (sort.equals(Sort.NEW)) { | ||
programList = foundQuery.orderBy(newSpecifier).fetch(); | ||
} else if (sort.equals(Sort.POPULAR)) { | ||
programList = foundQuery.orderBy(popularSpecifier).fetch(); | ||
} else if (sort.equals(Sort.DEADLINE)) { | ||
programList = foundQuery.orderBy(deadlineSpecifier).fetch(); | ||
} else { | ||
throw new RuntimeException("행사를 찾을 수 없습니다."); | ||
} | ||
|
||
return new PageImpl<>(programList); | ||
} | ||
|
||
private JPAQuery<Program> findQuery(ProgramListRequestDto requestDto, Pageable pageable) { | ||
return queryFactory | ||
.selectFrom(program) | ||
.where( | ||
program.isOpen.eq(true), | ||
keywordCondition(requestDto.getKeyword()), | ||
categoryCondition(requestDto.getCategory()) | ||
) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()); | ||
} | ||
|
||
private BooleanExpression keywordCondition(String keyword) { | ||
if (keyword != null) { | ||
return program.title.containsIgnoreCase(keyword).or(program.content.containsIgnoreCase(keyword)); | ||
} | ||
return null; | ||
} | ||
|
||
private BooleanExpression categoryCondition(List<Category> categoryList) { | ||
if (categoryList != null && !categoryList.isEmpty()) { | ||
return program.category.in(categoryList); | ||
} | ||
return null; | ||
} | ||
|
||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/efub/dhs/global/config/QueryDslConfig.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.efub.dhs.global.config; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
@Configuration | ||
public class QueryDslConfig { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |