From 18fd97260ac00f68502e49248890c6e6cf754fbb Mon Sep 17 00:00:00 2001 From: BK Date: Mon, 6 Nov 2023 21:53:51 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=8B=9C=EC=9E=91=EC=95=88=ED=95=9C=20?= =?UTF-8?q?=EC=B1=8C=EB=A6=B0=EC=A7=80=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20-=20NoOffset=20page=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 35 +++++++++ .../BE/common/entity/QBaseEntity.java | 39 ++++++++++ .../igoMoney/BE/domain/QChallenge.java | 70 ++++++++++++++++++ .../igoMoney/BE/domain/QChallengeUser.java | 62 ++++++++++++++++ .../igoMoney/BE/domain/QNotification.java | 65 ++++++++++++++++ .../generated/igoMoney/BE/domain/QRecord.java | 74 +++++++++++++++++++ .../igoMoney/BE/domain/QRefreshToken.java | 49 ++++++++++++ .../generated/igoMoney/BE/domain/QUser.java | 69 +++++++++++++++++ .../igoMoney/BE/domain/QUserReport.java | 53 +++++++++++++ .../BE/common/config/QueryDslConfig.java | 19 +++++ .../BE/controller/ChallengeController.java | 6 +- .../repository/ChallengeCustomRepository.java | 10 +++ .../ChallengeCustomRepositoryImpl.java | 37 ++++++++++ .../BE/repository/ChallengeRepository.java | 2 +- .../igoMoney/BE/service/ChallengeService.java | 4 +- .../BE/service/ChallengeServiceTest.java | 56 ++++++++++++++ 16 files changed, 645 insertions(+), 5 deletions(-) create mode 100644 src/main/generated/igoMoney/BE/common/entity/QBaseEntity.java create mode 100644 src/main/generated/igoMoney/BE/domain/QChallenge.java create mode 100644 src/main/generated/igoMoney/BE/domain/QChallengeUser.java create mode 100644 src/main/generated/igoMoney/BE/domain/QNotification.java create mode 100644 src/main/generated/igoMoney/BE/domain/QRecord.java create mode 100644 src/main/generated/igoMoney/BE/domain/QRefreshToken.java create mode 100644 src/main/generated/igoMoney/BE/domain/QUser.java create mode 100644 src/main/generated/igoMoney/BE/domain/QUserReport.java create mode 100644 src/main/java/igoMoney/BE/common/config/QueryDslConfig.java create mode 100644 src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java create mode 100644 src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java create mode 100644 src/test/java/igoMoney/BE/service/ChallengeServiceTest.java diff --git a/build.gradle b/build.gradle index 269f629..242550e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,10 @@ + +buildscript { + ext { + queryDslVersion = "5.0.0" + } +} + plugins { id 'java' id 'org.springframework.boot' version '3.1.3' @@ -32,6 +39,7 @@ dependencyManagement { } + dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' @@ -47,6 +55,10 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' + //JUnit4 추가 + testImplementation("org.junit.vintage:junit-vintage-engine") { + exclude group: "org.hamcrest", module: "hamcrest-core" + } // jwt implementation group: 'com.auth0', name: 'java-jwt', version: '3.10.2' @@ -68,5 +80,28 @@ dependencies { implementation platform('software.amazon.awssdk:bom:2.20.56') implementation 'software.amazon.awssdk:s3' implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' + + // Querydsl + implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta' + annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta" + annotationProcessor "jakarta.annotation:jakarta.annotation-api" + annotationProcessor "jakarta.persistence:jakarta.persistence-api" +} + +// Querydsl 설정부 +def generated = 'src/main/generated' + +// querydsl QClass 파일 생성 위치를 지정 +tasks.withType(JavaCompile) { + options.getGeneratedSourceOutputDirectory().set(file(generated)) +} + +// java source set 에 querydsl QClass 위치 추가 +sourceSets { + main.java.srcDirs += [ generated ] } +// gradle clean 시에 QClass 디렉토리 삭제 +clean { + delete file(generated) +} \ No newline at end of file diff --git a/src/main/generated/igoMoney/BE/common/entity/QBaseEntity.java b/src/main/generated/igoMoney/BE/common/entity/QBaseEntity.java new file mode 100644 index 0000000..70823db --- /dev/null +++ b/src/main/generated/igoMoney/BE/common/entity/QBaseEntity.java @@ -0,0 +1,39 @@ +package igoMoney.BE.common.entity; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; + + +/** + * QBaseEntity is a Querydsl query type for BaseEntity + */ +@Generated("com.querydsl.codegen.DefaultSupertypeSerializer") +public class QBaseEntity extends EntityPathBase { + + private static final long serialVersionUID = -1350694424L; + + public static final QBaseEntity baseEntity = new QBaseEntity("baseEntity"); + + public final DateTimePath createdDate = createDateTime("createdDate", java.time.LocalDateTime.class); + + public final DateTimePath modifiedDate = createDateTime("modifiedDate", java.time.LocalDateTime.class); + + public QBaseEntity(String variable) { + super(BaseEntity.class, forVariable(variable)); + } + + public QBaseEntity(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QBaseEntity(PathMetadata metadata) { + super(BaseEntity.class, metadata); + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QChallenge.java b/src/main/generated/igoMoney/BE/domain/QChallenge.java new file mode 100644 index 0000000..bf40add --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QChallenge.java @@ -0,0 +1,70 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.PathInits; + + +/** + * QChallenge is a Querydsl query type for Challenge + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QChallenge extends EntityPathBase { + + private static final long serialVersionUID = 563322277L; + + public static final QChallenge challenge = new QChallenge("challenge"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + public final NumberPath categoryId = createNumber("categoryId", Integer.class); + + public final ListPath challengeUsers = this.createList("challengeUsers", ChallengeUser.class, QChallengeUser.class, PathInits.DIRECT2); + + public final StringPath content = createString("content"); + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final DatePath endDate = createDate("endDate", java.time.LocalDate.class); + + public final NumberPath id = createNumber("id", Long.class); + + public final NumberPath leaderId = createNumber("leaderId", Long.class); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final ListPath records = this.createList("records", Record.class, QRecord.class, PathInits.DIRECT2); + + public final DatePath startDate = createDate("startDate", java.time.LocalDate.class); + + public final StringPath status = createString("status"); + + public final NumberPath targetAmount = createNumber("targetAmount", Integer.class); + + public final NumberPath term = createNumber("term", Integer.class); + + public final StringPath title = createString("title"); + + public final NumberPath winnerId = createNumber("winnerId", Long.class); + + public QChallenge(String variable) { + super(Challenge.class, forVariable(variable)); + } + + public QChallenge(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QChallenge(PathMetadata metadata) { + super(Challenge.class, metadata); + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QChallengeUser.java b/src/main/generated/igoMoney/BE/domain/QChallengeUser.java new file mode 100644 index 0000000..5372652 --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QChallengeUser.java @@ -0,0 +1,62 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.PathInits; + + +/** + * QChallengeUser is a Querydsl query type for ChallengeUser + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QChallengeUser extends EntityPathBase { + + private static final long serialVersionUID = -843406576L; + + private static final PathInits INITS = PathInits.DIRECT2; + + public static final QChallengeUser challengeUser = new QChallengeUser("challengeUser"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + public final QChallenge challenge; + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final NumberPath id = createNumber("id", Long.class); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final QUser user; + + public QChallengeUser(String variable) { + this(ChallengeUser.class, forVariable(variable), INITS); + } + + public QChallengeUser(Path path) { + this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS)); + } + + public QChallengeUser(PathMetadata metadata) { + this(metadata, PathInits.getFor(metadata, INITS)); + } + + public QChallengeUser(PathMetadata metadata, PathInits inits) { + this(ChallengeUser.class, metadata, inits); + } + + public QChallengeUser(Class type, PathMetadata metadata, PathInits inits) { + super(type, metadata, inits); + this.challenge = inits.isInitialized("challenge") ? new QChallenge(forProperty("challenge")) : null; + this.user = inits.isInitialized("user") ? new QUser(forProperty("user")) : null; + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QNotification.java b/src/main/generated/igoMoney/BE/domain/QNotification.java new file mode 100644 index 0000000..ba30d7a --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QNotification.java @@ -0,0 +1,65 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.PathInits; + + +/** + * QNotification is a Querydsl query type for Notification + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QNotification extends EntityPathBase { + + private static final long serialVersionUID = 1979697257L; + + private static final PathInits INITS = PathInits.DIRECT2; + + public static final QNotification notification = new QNotification("notification"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + public final BooleanPath checked = createBoolean("checked"); + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final NumberPath id = createNumber("id", Long.class); + + public final StringPath message = createString("message"); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final StringPath title = createString("title"); + + public final QUser user; + + public QNotification(String variable) { + this(Notification.class, forVariable(variable), INITS); + } + + public QNotification(Path path) { + this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS)); + } + + public QNotification(PathMetadata metadata) { + this(metadata, PathInits.getFor(metadata, INITS)); + } + + public QNotification(PathMetadata metadata, PathInits inits) { + this(Notification.class, metadata, inits); + } + + public QNotification(Class type, PathMetadata metadata, PathInits inits) { + super(type, metadata, inits); + this.user = inits.isInitialized("user") ? new QUser(forProperty("user")) : null; + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QRecord.java b/src/main/generated/igoMoney/BE/domain/QRecord.java new file mode 100644 index 0000000..2bb05bc --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QRecord.java @@ -0,0 +1,74 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.PathInits; + + +/** + * QRecord is a Querydsl query type for Record + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QRecord extends EntityPathBase { + + private static final long serialVersionUID = -1181179313L; + + private static final PathInits INITS = PathInits.DIRECT2; + + public static final QRecord record = new QRecord("record"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + public final QChallenge challenge; + + public final StringPath content = createString("content"); + + public final NumberPath cost = createNumber("cost", Integer.class); + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final DatePath date = createDate("date", java.time.LocalDate.class); + + public final BooleanPath hide = createBoolean("hide"); + + public final NumberPath id = createNumber("id", Long.class); + + public final StringPath image = createString("image"); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final StringPath title = createString("title"); + + public final QUser user; + + public QRecord(String variable) { + this(Record.class, forVariable(variable), INITS); + } + + public QRecord(Path path) { + this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS)); + } + + public QRecord(PathMetadata metadata) { + this(metadata, PathInits.getFor(metadata, INITS)); + } + + public QRecord(PathMetadata metadata, PathInits inits) { + this(Record.class, metadata, inits); + } + + public QRecord(Class type, PathMetadata metadata, PathInits inits) { + super(type, metadata, inits); + this.challenge = inits.isInitialized("challenge") ? new QChallenge(forProperty("challenge")) : null; + this.user = inits.isInitialized("user") ? new QUser(forProperty("user")) : null; + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QRefreshToken.java b/src/main/generated/igoMoney/BE/domain/QRefreshToken.java new file mode 100644 index 0000000..240d0ae --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QRefreshToken.java @@ -0,0 +1,49 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; + + +/** + * QRefreshToken is a Querydsl query type for RefreshToken + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QRefreshToken extends EntityPathBase { + + private static final long serialVersionUID = 1327957852L; + + public static final QRefreshToken refreshToken1 = new QRefreshToken("refreshToken1"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final NumberPath id = createNumber("id", Long.class); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final StringPath refreshToken = createString("refreshToken"); + + public final NumberPath userId = createNumber("userId", Long.class); + + public QRefreshToken(String variable) { + super(RefreshToken.class, forVariable(variable)); + } + + public QRefreshToken(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QRefreshToken(PathMetadata metadata) { + super(RefreshToken.class, metadata); + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QUser.java b/src/main/generated/igoMoney/BE/domain/QUser.java new file mode 100644 index 0000000..fd31dc3 --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QUser.java @@ -0,0 +1,69 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; + + +/** + * QUser is a Querydsl query type for User + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QUser extends EntityPathBase { + + private static final long serialVersionUID = -738555575L; + + public static final QUser user = new QUser("user"); + + public final NumberPath badgeCount = createNumber("badgeCount", Integer.class); + + public final BooleanPath banned = createBoolean("banned"); + + public final DatePath banReleaseDate = createDate("banReleaseDate", java.time.LocalDate.class); + + public final NumberPath challengeCount = createNumber("challengeCount", Integer.class); + + public final DateTimePath createdDate = createDateTime("createdDate", java.time.LocalDateTime.class); + + public final StringPath email = createString("email"); + + public final NumberPath id = createNumber("id", Long.class); + + public final StringPath image = createString("image"); + + public final BooleanPath inChallenge = createBoolean("inChallenge"); + + public final StringPath loginId = createString("loginId"); + + public final NumberPath myChallengeId = createNumber("myChallengeId", Long.class); + + public final StringPath nickname = createString("nickname"); + + public final StringPath password = createString("password"); + + public final StringPath provider = createString("provider"); + + public final NumberPath reportedCount = createNumber("reportedCount", Integer.class); + + public final StringPath role = createString("role"); + + public final NumberPath winCount = createNumber("winCount", Integer.class); + + public QUser(String variable) { + super(User.class, forVariable(variable)); + } + + public QUser(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QUser(PathMetadata metadata) { + super(User.class, metadata); + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QUserReport.java b/src/main/generated/igoMoney/BE/domain/QUserReport.java new file mode 100644 index 0000000..e6cb4b0 --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QUserReport.java @@ -0,0 +1,53 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; + + +/** + * QUserReport is a Querydsl query type for UserReport + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QUserReport extends EntityPathBase { + + private static final long serialVersionUID = 1300754397L; + + public static final QUserReport userReport = new QUserReport("userReport"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final NumberPath id = createNumber("id", Long.class); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final NumberPath offenderId = createNumber("offenderId", Long.class); + + public final NumberPath reason = createNumber("reason", Integer.class); + + public final NumberPath recordId = createNumber("recordId", Long.class); + + public final NumberPath reporterId = createNumber("reporterId", Long.class); + + public QUserReport(String variable) { + super(UserReport.class, forVariable(variable)); + } + + public QUserReport(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QUserReport(PathMetadata metadata) { + super(UserReport.class, metadata); + } + +} + diff --git a/src/main/java/igoMoney/BE/common/config/QueryDslConfig.java b/src/main/java/igoMoney/BE/common/config/QueryDslConfig.java new file mode 100644 index 0000000..3d48434 --- /dev/null +++ b/src/main/java/igoMoney/BE/common/config/QueryDslConfig.java @@ -0,0 +1,19 @@ +package igoMoney.BE.common.config; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class QueryDslConfig { + + @PersistenceContext + private EntityManager entityManager; + + @Bean + public JPAQueryFactory jpaQueryFactory() { + return new JPAQueryFactory(entityManager); + } +} diff --git a/src/main/java/igoMoney/BE/controller/ChallengeController.java b/src/main/java/igoMoney/BE/controller/ChallengeController.java index 7010109..23728b8 100644 --- a/src/main/java/igoMoney/BE/controller/ChallengeController.java +++ b/src/main/java/igoMoney/BE/controller/ChallengeController.java @@ -25,9 +25,11 @@ public class ChallengeController { // 시작 안 한 챌린지 목록 조회 @GetMapping("notstarted") - public ResponseEntity> getNotStartedChallengeList() { + public ResponseEntity> getNotStartedChallengeList( + @RequestParam(value="lastId", required=false, defaultValue="10") Long lastId, + @RequestParam(value="pageSize", required=false, defaultValue="10") int pageSize) { - List response = challengeService.getNotStartedChallengeList(); + List response = challengeService.getNotStartedChallengeList(lastId, pageSize); return ResponseEntity.status(HttpStatus.OK).body(response); } diff --git a/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java new file mode 100644 index 0000000..234160a --- /dev/null +++ b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java @@ -0,0 +1,10 @@ +package igoMoney.BE.repository; + +import igoMoney.BE.domain.Challenge; + +import java.time.LocalDate; +import java.util.List; + +public interface ChallengeCustomRepository { + List findAllNotStarted(int pageSize, Long lastId, LocalDate date); +} diff --git a/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java new file mode 100644 index 0000000..318f445 --- /dev/null +++ b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java @@ -0,0 +1,37 @@ +package igoMoney.BE.repository; + +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; +import igoMoney.BE.domain.Challenge; + +import java.time.LocalDate; +import java.util.List; + +import static igoMoney.BE.domain.QChallenge.challenge; + +public class ChallengeCustomRepositoryImpl implements ChallengeCustomRepository { + + private final JPAQueryFactory jpaQueryFactory; + + public ChallengeCustomRepositoryImpl(JPAQueryFactory jpaQueryFactory) { + this.jpaQueryFactory = jpaQueryFactory; + } + + @Override + public List findAllNotStarted(int pageSize, Long lastId, LocalDate date){ + return jpaQueryFactory.selectFrom(challenge) + .where(ltChallengeId(lastId), + challenge.startDate.gt(date)) + .orderBy(challenge.id.desc()) + .limit(pageSize) + .fetch(); + } + + private BooleanExpression ltChallengeId(Long lastId) { + + if (lastId == null) { + return null; // BooleanExpression 자리에 null이 반환되면 조건문에서 자동으로 제거된다 + } + return challenge.id.lt(lastId); + } +} diff --git a/src/main/java/igoMoney/BE/repository/ChallengeRepository.java b/src/main/java/igoMoney/BE/repository/ChallengeRepository.java index 052e82e..6bd7424 100644 --- a/src/main/java/igoMoney/BE/repository/ChallengeRepository.java +++ b/src/main/java/igoMoney/BE/repository/ChallengeRepository.java @@ -8,7 +8,7 @@ import java.time.LocalDate; import java.util.List; -public interface ChallengeRepository extends JpaRepository { +public interface ChallengeRepository extends JpaRepository, ChallengeCustomRepository { @Query("select c from Challenge c where c.startDate > :date") List findAllByStartDateIsAfter(@Param("date") LocalDate date); diff --git a/src/main/java/igoMoney/BE/service/ChallengeService.java b/src/main/java/igoMoney/BE/service/ChallengeService.java index 586de66..3948696 100644 --- a/src/main/java/igoMoney/BE/service/ChallengeService.java +++ b/src/main/java/igoMoney/BE/service/ChallengeService.java @@ -36,10 +36,10 @@ public class ChallengeService { DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM월 dd일"); // 시작 안 한 챌린지 목록 조회 - public List getNotStartedChallengeList() { + public List getNotStartedChallengeList(Long lastId, int pageSize) { List responseList = new ArrayList<>(); - List challengeList = challengeRepository.findAllByStartDateIsAfter(LocalDate.now()); + List challengeList = challengeRepository.findAllNotStarted(pageSize, lastId, LocalDate.now()); for (Challenge challenge: challengeList){ ChallengeResponse challengeResponse = ChallengeResponse.builder() .id(challenge.getId()) diff --git a/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java b/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java new file mode 100644 index 0000000..67d5191 --- /dev/null +++ b/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java @@ -0,0 +1,56 @@ +package igoMoney.BE.service; + +import igoMoney.BE.domain.Challenge; +import igoMoney.BE.repository.ChallengeRepository; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.LocalDate; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +@Transactional +public class ChallengeServiceTest { + + @Autowired ChallengeService challengeService; + @Autowired ChallengeRepository challengeRepository; + @Autowired EntityManager em; + + @Test + public void 시작안한챌린지_조회() throws Exception { + //given + for (int i = 1; i <= 30; i++) { + challengeRepository.save(Challenge.builder() + .title(String.valueOf(i)) + .content("__") + .targetAmount(10000) + .startDate(LocalDate.now().plusDays(2)) + .build()); + } + + //when + List challenges = challengeRepository.findAllNotStarted(10, null, LocalDate.now()); // pageNo는 0부터 시작이라 1이면 두번째 페이지 조회 + + //then + assertEquals(challenges.size(),10); + assertEquals(30,(long) challenges.get(0).getId()); + assertEquals(21, (long) challenges.get(9).getId()); + + // [NoOffset 2번째 페이지] +// //when +// List challenges = challengeRepository.findAllNotStarted(10, 21L); // DESC. 20~11 +// +// //then +// assertThat(challenges).hasSize(10); +// assertThat(challenges.get(0).getId()).isEqualTo(20); +// assertThat(challenges.get(9).getId()).isEqualTo(11); + } +}