Skip to content

Commit

Permalink
Merge pull request #36 from TEAM-DHS/feat/program
Browse files Browse the repository at this point in the history
[FEAT] 행사 리스트 조회 API
  • Loading branch information
xyzwv authored Nov 22, 2023
2 parents 5b77597 + 33f97b7 commit f6a3c0a
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 6 deletions.
30 changes: 30 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
buildscript {
ext {
queryDslVersion = "5.0.0"
}
}

plugins {
id 'java'
id 'org.springframework.boot' version '2.7.16'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
// querydsl
id 'com.ewerk.gradle.plugins.querydsl' version '1.0.10'
}

group = 'com.efub'
Expand Down Expand Up @@ -55,6 +63,28 @@ dependencies {
// test
testRuntimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// querydsl
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}"
}

def querydslDir = "$buildDir/generated/querydsl"

querydsl {
jpa = true
querydslSourcesDir = querydslDir
}

sourceSets {
main.java.srcDir querydslDir
}

configurations {
querydsl.extendsFrom compileClasspath
}

compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.efub.dhs.domain.program.dto.request.ProgramCreationRequestDto;
import com.efub.dhs.domain.program.dto.request.ProgramListRequestDto;
import com.efub.dhs.domain.program.dto.request.ProgramRegistrationRequestDto;
import com.efub.dhs.domain.program.dto.response.ProgramCreatedResponseDto;
import com.efub.dhs.domain.program.dto.response.ProgramCreationResponseDto;
Expand All @@ -35,7 +36,7 @@ public class ProgramController {

@GetMapping("/{programId}")
@ResponseStatus(value = HttpStatus.OK)
public ProgramDetailResponseDto programFind(@PathVariable Long programId) {
public ProgramDetailResponseDto findProgramById(@PathVariable Long programId) {
return programService.findProgramById(programId);
}

Expand All @@ -62,4 +63,9 @@ public ProgramCreatedResponseDto findProgramCreated(@RequestParam int page) {
public ProgramListResponseDto findProgramLiked(@RequestParam int page) {
return programMemberService.findProgramLiked(page);
}

@GetMapping
public ProgramListResponseDto findProgramList(@RequestParam int page, ProgramListRequestDto requestDto) {
return programService.findProgramList(page, requestDto);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/efub/dhs/domain/program/dto/ProgramDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class ProgramDto {
private Long programId;
private String title;
private Category category;
private String category;
private LocalDateTime schedule;
private String postalCode;
private String location;
Expand All @@ -35,7 +35,7 @@ public ProgramDto(Program program, Integer remainingDays, GoalDto goal, List<Ima
String depositInfo, List<NoticeDto> notices, HostDto host) {
this.programId = program.getProgramId();
this.title = program.getTitle();
this.category = program.getCategory();
this.category = Category.to(program.getCategory());
this.schedule = program.getSchedule();
this.postalCode = program.getPostalCode();
this.location = program.getLocation();
Expand Down
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class ProgramOutlineResponseDto {
private Long programId;
private String title;
private Category category;
private String category;
private String thumbnailImage;
private Integer remainingDays;
private Boolean isOpen;
Expand All @@ -24,7 +24,7 @@ public class ProgramOutlineResponseDto {
public ProgramOutlineResponseDto(Program program, Integer remainingDays, GoalDto goal, Boolean hasLike) {
this.programId = program.getProgramId();
this.title = program.getTitle();
this.category = program.getCategory();
this.category = Category.to(program.getCategory());
this.thumbnailImage = program.getImages().get(0).getUrl();
this.remainingDays = remainingDays;
this.isOpen = program.getIsOpen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public static Category from(String name) {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid Category name: " + name));
}

public static String to(Category category) {
return category.name;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/efub/dhs/domain/program/entity/Sort.java
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.efub.dhs.domain.program.entity.Category;
import com.efub.dhs.domain.program.entity.Program;

public interface ProgramRepository extends JpaRepository<Program, Long> {
public interface ProgramRepository extends JpaRepository<Program, Long>, ProgramRepositoryCustom {

//List<Program> findTop3ByCategoryAndScheduleMonth(Category category, Month month);
List<Program> findTop3ByCategory(Category category);
Expand Down
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);
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -15,11 +17,14 @@
import com.efub.dhs.domain.program.dto.HostDto;
import com.efub.dhs.domain.program.dto.ImageDto;
import com.efub.dhs.domain.program.dto.NoticeDto;
import com.efub.dhs.domain.program.dto.PageInfoDto;
import com.efub.dhs.domain.program.dto.ProgramDto;
import com.efub.dhs.domain.program.dto.ProgramMemberDto;
import com.efub.dhs.domain.program.dto.request.ProgramCreationRequestDto;
import com.efub.dhs.domain.program.dto.request.ProgramListRequestDto;
import com.efub.dhs.domain.program.dto.request.ProgramRegistrationRequestDto;
import com.efub.dhs.domain.program.dto.response.ProgramDetailResponseDto;
import com.efub.dhs.domain.program.dto.response.ProgramListResponseDto;
import com.efub.dhs.domain.program.dto.response.ProgramOutlineResponseDto;
import com.efub.dhs.domain.program.entity.Notice;
import com.efub.dhs.domain.program.entity.Program;
Expand All @@ -38,6 +43,8 @@
@RequiredArgsConstructor
public class ProgramService {

private static final int PAGE_SIZE = 12;

private final ProgramRepository programRepository;
private final ProgramImageRepository programImageRepository;
private final NoticeRepository noticeRepository;
Expand Down Expand Up @@ -143,4 +150,14 @@ public Registration registerProgram(Long programId, ProgramRegistrationRequestDt
Registration registration = requestDto.toEntity(currentUser, program);
return registrationService.saveRegistration(registration);
}

public ProgramListResponseDto findProgramList(int page, ProgramListRequestDto requestDto) {
Member currentUser = getCurrentUser();
Page<Program> programPage = programRepository.findProgramListByFilter(requestDto,
PageRequest.of(page, PAGE_SIZE));
PageInfoDto pageInfoDto = PageInfoDto.from(programPage);
List<ProgramOutlineResponseDto> programOutlineResponseDtoList =
convertToProgramOutlineResponseDtoList(programPage.getContent(), currentUser);
return new ProgramListResponseDto(programOutlineResponseDtoList, pageInfoDto);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/efub/dhs/global/config/QueryDslConfig.java
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);
}
}

0 comments on commit f6a3c0a

Please sign in to comment.