From abb411f5610620be01353797f5753d19c12b106c Mon Sep 17 00:00:00 2001 From: sososo0 Date: Sun, 3 Mar 2024 19:05:00 +0900 Subject: [PATCH 1/8] =?UTF-8?q?chore:=20query=20dsl=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - build.gradle.kts에 query dsl 관련 설정 추가 --- build.gradle.kts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 79cf0beaa..63a3d777c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -72,6 +72,26 @@ tasks { } /******* End Spring Rest Docs *******/ +/******* Start Query DSL Build Options *******/ + +val querydslDir = "src/main/generated" + +sourceSets { + getByName("main").java.srcDirs(querydslDir) +} + +tasks.withType { + options.generatedSourceOutputDirectory.set(file(querydslDir)) +} + +tasks.named("clean") { + doLast { + file(querydslDir).deleteRecursively() + } +} + +/******* End Query DSL Build Options *******/ + dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-security") @@ -87,6 +107,12 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-mail") implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.1") implementation("org.springframework.boot:spring-boot-starter-aop") + val queryDslVersion = "5.0.0" + implementation("com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta") + + annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:jakarta") + annotationProcessor("jakarta.annotation:jakarta.annotation-api") + annotationProcessor("jakarta.persistence:jakarta.persistence-api") asciidoctorExt("org.springframework.restdocs:spring-restdocs-asciidoctor") From 6c6541439316bfedef563097668e3fd234997962 Mon Sep 17 00:00:00 2001 From: sososo0 Date: Sun, 3 Mar 2024 19:32:27 +0900 Subject: [PATCH 2/8] fixed utracked files --- gradlew | 0 gradlew.bat | 182 ++++++++++++++++++++++++++-------------------------- 2 files changed, 91 insertions(+), 91 deletions(-) mode change 100755 => 100644 gradlew diff --git a/gradlew b/gradlew old mode 100755 new mode 100644 diff --git a/gradlew.bat b/gradlew.bat index 53a6b238d..f127cfd49 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,91 +1,91 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega From 3ec9ff620b722c12edcc7f8dc0cdc8d993b2486b Mon Sep 17 00:00:00 2001 From: sososo0 Date: Sun, 3 Mar 2024 21:14:34 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20querydsl=20config=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JPA Factory를 bean 등록하기 위함 --- .../config/querydsl/QuerydslConfiguration.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/com/keeper/homepage/global/config/querydsl/QuerydslConfiguration.java diff --git a/src/main/java/com/keeper/homepage/global/config/querydsl/QuerydslConfiguration.java b/src/main/java/com/keeper/homepage/global/config/querydsl/QuerydslConfiguration.java new file mode 100644 index 000000000..fafd66332 --- /dev/null +++ b/src/main/java/com/keeper/homepage/global/config/querydsl/QuerydslConfiguration.java @@ -0,0 +1,18 @@ +package com.keeper.homepage.global.config.querydsl; + +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 QuerydslConfiguration { + @PersistenceContext + private EntityManager entityManager; + + @Bean + public JPAQueryFactory jpaQueryFactory() { + return new JPAQueryFactory(entityManager); + } +} From 8285634fdb3b0db85c939388075801a1c4acb305 Mon Sep 17 00:00:00 2001 From: sososo0 Date: Sun, 3 Mar 2024 23:51:53 +0900 Subject: [PATCH 4/8] =?UTF-8?q?refactor:=20attendance=20repository?= =?UTF-8?q?=EC=97=90=20querydsl=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit spring data jap custom repository 방식 사용 --- .../attendance/dao/AttendanceRepository.java | 9 +----- .../dao/AttendanceRepositoryCustom.java | 14 +++++++++ .../dao/AttendanceRepositoryImpl.java | 31 +++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryCustom.java create mode 100644 src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryImpl.java diff --git a/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepository.java b/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepository.java index 30935e5dd..8a252d445 100644 --- a/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepository.java +++ b/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepository.java @@ -8,10 +8,8 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; -public interface AttendanceRepository extends JpaRepository { +public interface AttendanceRepository extends JpaRepository, AttendanceRepositoryCustom { boolean existsByMemberAndDate(Member member, LocalDate date); @@ -23,9 +21,4 @@ public interface AttendanceRepository extends JpaRepository { List findAllByDateOrderByContinuousDayDesc(LocalDate date); - @Query("SELECT a FROM Attendance a " - + "WHERE a.member = :member " - + "AND a.date >= :localDate") - List findAllRecent(@Param("member") Member member, @Param("localDate") LocalDate localDate); - } diff --git a/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryCustom.java b/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryCustom.java new file mode 100644 index 000000000..cbaa815f5 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryCustom.java @@ -0,0 +1,14 @@ +package com.keeper.homepage.domain.attendance.dao; + +import com.keeper.homepage.domain.attendance.entity.Attendance; +import com.keeper.homepage.domain.member.entity.Member; +import org.springframework.data.repository.query.Param; + +import java.time.LocalDate; +import java.util.List; + +public interface AttendanceRepositoryCustom { + + List findAllRecent(@Param("member") Member member, @Param("localDate") LocalDate localDate); + +} diff --git a/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryImpl.java b/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryImpl.java new file mode 100644 index 000000000..73bfa5f5b --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/attendance/dao/AttendanceRepositoryImpl.java @@ -0,0 +1,31 @@ +package com.keeper.homepage.domain.attendance.dao; + + +import com.keeper.homepage.domain.attendance.entity.Attendance; +import com.keeper.homepage.domain.member.entity.Member; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.data.repository.query.Param; + +import java.time.LocalDate; +import java.util.List; + +import static com.keeper.homepage.domain.attendance.entity.QAttendance.*; + +@RequiredArgsConstructor +public class AttendanceRepositoryImpl implements AttendanceRepositoryCustom { + + private final JPAQueryFactory queryFactory; + + @Override + public List findAllRecent(@Param("member") Member member, @Param("localDate") LocalDate localDate) { + return queryFactory + .selectFrom(attendance) + .where( + attendance.member.eq(member) + .and(attendance.date.goe(localDate)) + ) + .fetch(); + } + +} From f4d0df47d1ad821873713646b33baa3fd5113aef Mon Sep 17 00:00:00 2001 From: sososo0 Date: Mon, 4 Mar 2024 00:26:25 +0900 Subject: [PATCH 5/8] =?UTF-8?q?refactor:=20comment=20repository=EC=97=90?= =?UTF-8?q?=20querydsl=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/comment/dao/CommentRepository.java | 7 +---- .../comment/dao/CommentRepositoryCustom.java | 10 +++++++ .../comment/dao/CommentRepositoryImpl.java | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryCustom.java create mode 100644 src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryImpl.java diff --git a/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepository.java b/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepository.java index 7e36e969a..a2c517178 100644 --- a/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepository.java +++ b/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepository.java @@ -7,11 +7,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -public interface CommentRepository extends JpaRepository { +public interface CommentRepository extends JpaRepository, CommentRepositoryCustom { - @Modifying - @Query("UPDATE Comment c " - + "SET c.member = :virtualMember " - + "WHERE c.member = :member") - void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember); } diff --git a/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryCustom.java b/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryCustom.java new file mode 100644 index 000000000..b47562199 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryCustom.java @@ -0,0 +1,10 @@ +package com.keeper.homepage.domain.comment.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import org.springframework.data.repository.query.Param; + +public interface CommentRepositoryCustom { + + void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember); + +} diff --git a/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryImpl.java b/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryImpl.java new file mode 100644 index 000000000..1e1ffcca1 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/comment/dao/CommentRepositoryImpl.java @@ -0,0 +1,26 @@ +package com.keeper.homepage.domain.comment.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.repository.query.Param; + +import static com.keeper.homepage.domain.comment.entity.QComment.*; + +@RequiredArgsConstructor +public class CommentRepositoryImpl implements CommentRepositoryCustom { + + private final JPAQueryFactory jpaQueryFactory; + + @Modifying + @Override + public void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember) { + jpaQueryFactory + .update(comment) + .set(comment.member, virtualMember) + .where(comment.member.eq(member)) + .execute(); + } + +} From aa27865b3889c17cd2b729bd1d76c243432aaa11 Mon Sep 17 00:00:00 2001 From: sososo0 Date: Mon, 4 Mar 2024 00:39:28 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor:=20election=20repository=EC=97=90?= =?UTF-8?q?=20query=20dsl=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../election/dao/ElectionRepository.java | 8 +----- .../dao/ElectionRepositoryCustom.java | 10 +++++++ .../election/dao/ElectionRepositoryImpl.java | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryCustom.java create mode 100644 src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java diff --git a/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepository.java b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepository.java index 4bf837c0f..a33e01b19 100644 --- a/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepository.java +++ b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepository.java @@ -10,16 +10,10 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -public interface ElectionRepository extends JpaRepository { +public interface ElectionRepository extends JpaRepository, ElectionRepositoryCustom { Optional findByIdAndIdNot(Long electionId, Long virtualId); Page findAllByIdNot(long virtualId, Pageable pageable); - @Modifying - @Query("UPDATE Election e " - + "SET e.member = :virtualMember " - + "WHERE e.member = :member") - void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember); - } diff --git a/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryCustom.java b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryCustom.java new file mode 100644 index 000000000..df0f84184 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryCustom.java @@ -0,0 +1,10 @@ +package com.keeper.homepage.domain.election.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import org.springframework.data.repository.query.Param; + +public interface ElectionRepositoryCustom { + + void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember); + +} diff --git a/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java new file mode 100644 index 000000000..a49882044 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java @@ -0,0 +1,26 @@ +package com.keeper.homepage.domain.election.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.repository.query.Param; + +import static com.keeper.homepage.domain.election.entity.QElection.*; + +@RequiredArgsConstructor +public class ElectionRepositoryImpl implements ElectionRepositoryCustom{ + + private final JPAQueryFactory jpaQueryFactory; + + @Modifying + @Override + public void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember) { + jpaQueryFactory + .update(election) + .set(election.member, virtualMember) + .where(election.member.eq(member)) + .execute(); + } + +} From 826a3f4e3c61793eaa95fef4292a8f120e0404db Mon Sep 17 00:00:00 2001 From: sososo0 Date: Mon, 4 Mar 2024 02:21:15 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20member=20repository=EC=97=90=20?= =?UTF-8?q?query=20dsl=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../election/dao/ElectionRepositoryImpl.java | 2 +- .../domain/member/dao/MemberRepository.java | 8 +---- .../member/dao/MemberRepositoryCustom.java | 12 +++++++ .../member/dao/MemberRepositoryImpl.java | 33 +++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryCustom.java create mode 100644 src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryImpl.java diff --git a/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java index a49882044..8349428ff 100644 --- a/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java +++ b/src/main/java/com/keeper/homepage/domain/election/dao/ElectionRepositoryImpl.java @@ -9,7 +9,7 @@ import static com.keeper.homepage.domain.election.entity.QElection.*; @RequiredArgsConstructor -public class ElectionRepositoryImpl implements ElectionRepositoryCustom{ +public class ElectionRepositoryImpl implements ElectionRepositoryCustom { private final JPAQueryFactory jpaQueryFactory; diff --git a/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepository.java b/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepository.java index 01646d956..83835494d 100644 --- a/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepository.java +++ b/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepository.java @@ -14,7 +14,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -public interface MemberRepository extends JpaRepository { +public interface MemberRepository extends JpaRepository, MemberRepositoryCustom { Optional findByProfileLoginId(LoginId profileLoginId); @@ -43,10 +43,4 @@ Optional findByProfileEmailAddressAndProfileLoginId(EmailAddress profile Page findAllByMemberTypeAndIdNot(MemberType type, long virtualId, Pageable pageable); - @Query("SELECT m FROM Member m " + - "JOIN FETCH m.memberType " + - "JOIN FETCH m.memberJob mj " + - "JOIN FETCH mj.memberJob " + - "WHERE m.id = :id") - Optional findMemberProfileWithFetchJoin(@Param("id") long id); } diff --git a/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryCustom.java b/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryCustom.java new file mode 100644 index 000000000..6b3fdae50 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryCustom.java @@ -0,0 +1,12 @@ +package com.keeper.homepage.domain.member.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import org.springframework.data.repository.query.Param; + +import java.util.Optional; + +public interface MemberRepositoryCustom { + + Optional findMemberProfileWithFetchJoin(@Param("id") long id); + +} diff --git a/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryImpl.java b/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryImpl.java new file mode 100644 index 000000000..74a18b119 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/member/dao/MemberRepositoryImpl.java @@ -0,0 +1,33 @@ +package com.keeper.homepage.domain.member.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.data.repository.query.Param; + +import java.util.Optional; + +import static com.keeper.homepage.domain.member.entity.QMember.*; +import static com.keeper.homepage.domain.member.entity.job.QMemberHasMemberJob.*; +import static com.keeper.homepage.domain.member.entity.job.QMemberJob.*; +import static com.keeper.homepage.domain.member.entity.type.QMemberType.*; + +@RequiredArgsConstructor +public class MemberRepositoryImpl implements MemberRepositoryCustom { + + private final JPAQueryFactory jpaQueryFactory; + + @Override + public Optional findMemberProfileWithFetchJoin(@Param("id") long id) { + return Optional.ofNullable( + jpaQueryFactory + .selectFrom(member) + .join(member.memberType, memberType).fetchJoin() + .join(member.memberJob, memberHasMemberJob).fetchJoin() + .join(memberHasMemberJob.memberJob, memberJob).fetchJoin() + .where(member.id.eq(id)) + .fetchOne() + ); + } + +} From 8f827256ae8de28c65fb0227c708f95ab53d104f Mon Sep 17 00:00:00 2001 From: sososo0 Date: Tue, 5 Mar 2024 02:11:31 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor:=20post=20repository=EC=97=90=20qu?= =?UTF-8?q?ery=20dsl=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit page nation 처리해야 하는 부분은 제외 --- .../domain/post/dao/PostRepository.java | 56 +-------- .../domain/post/dao/PostRepositoryCustom.java | 27 ++++ .../domain/post/dao/PostRepositoryImpl.java | 117 ++++++++++++++++++ 3 files changed, 145 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryCustom.java create mode 100644 src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryImpl.java diff --git a/src/main/java/com/keeper/homepage/domain/post/dao/PostRepository.java b/src/main/java/com/keeper/homepage/domain/post/dao/PostRepository.java index fc3b5bee2..9f2dc7238 100644 --- a/src/main/java/com/keeper/homepage/domain/post/dao/PostRepository.java +++ b/src/main/java/com/keeper/homepage/domain/post/dao/PostRepository.java @@ -13,30 +13,10 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -public interface PostRepository extends JpaRepository { +public interface PostRepository extends JpaRepository, PostRepositoryCustom { Optional findByIdAndIdNot(Long postId, Long virtualId); - /** - * 카테고리 + 공지글 + 등록시간 최신순 정렬 - * - * @param category 게시글 카테고리 - */ - @Query("SELECT p FROM Post p " - + "WHERE p.category = :category " - + "AND p.isNotice = true " - + "ORDER BY p.registerTime DESC") - List findAllNoticeByCategory(@Param("category") Category category); - - /** - * 임시 저장글 제외 + 등록 시간 최신순 정렬 - */ - @Query("SELECT p FROM Post p " - + "WHERE p.isTemp = false " - + "AND p.id <> 1 " // virtual post - + "ORDER BY p.registerTime DESC") - List findAllRecent(Pageable pageable); - /** * 카테고리 + 공지글 제외 + 임시글 제외 + 등록시간 최신순 정렬 * @@ -116,43 +96,9 @@ Page findAllRecentByCategoryAndTitleOrContent(@Param("category") Category Page findAllRecentByCategoryAndWriter(@Param("category") Category category, @Param("search") String search, Pageable pageable); - /** - * 임시 저장글 제외 + 등록 시간 최신순 정렬 + 날짜 사이의 게시글 - * - * @param startDate 가져올 시작 시간 - * @param endDate 가져올 끝 시간 - */ - @Query("SELECT p FROM Post p " + - "WHERE p.isTemp = false " + - "AND p.registerTime BETWEEN :startDate AND :endDate") - List findAllTrend(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate); - - - @Query("SELECT p FROM Post p " - + "WHERE p.id > :postId " - + "AND p.category = :category " - + "AND p.isTemp = false " - + "ORDER BY p.id ASC " - + "LIMIT 1") - Optional findNextPost(@Param("postId") Long postId, @Param("category") Category category); - - @Query("SELECT p FROM Post p " - + "WHERE p.id < :postId " - + "AND p.category = :category " - + "AND p.isTemp = false " - + "ORDER BY p.id DESC " - + "LIMIT 1") - Optional findPreviousPost(@Param("postId") Long postId, @Param("category") Category category); - Page findAllByMemberAndIsTempFalse(Member member, Pageable pageable); Page findAllByMemberAndIsTempTrue(Member member, Pageable pageable); - @Modifying - @Query("UPDATE Post p " - + "SET p.member = :virtualMember " - + "WHERE p.member = :member AND p.isTemp = false ") - void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember); - void deleteAllByMemberAndIsTempTrue(Member member); } diff --git a/src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryCustom.java b/src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryCustom.java new file mode 100644 index 000000000..efe8f2900 --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryCustom.java @@ -0,0 +1,27 @@ +package com.keeper.homepage.domain.post.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import com.keeper.homepage.domain.post.entity.Post; +import com.keeper.homepage.domain.post.entity.category.Category; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.query.Param; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +public interface PostRepositoryCustom { + + List findAllNoticeByCategory(@Param("category") Category category); + + List findAllRecent(Pageable pageable); + + List findAllTrend(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate); + + Optional findNextPost(@Param("postId") Long postId, @Param("category") Category category); + + Optional findPreviousPost(@Param("postId") Long postId, @Param("category") Category category); + + void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember); + +} diff --git a/src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryImpl.java b/src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryImpl.java new file mode 100644 index 000000000..161f8dd9d --- /dev/null +++ b/src/main/java/com/keeper/homepage/domain/post/dao/PostRepositoryImpl.java @@ -0,0 +1,117 @@ +package com.keeper.homepage.domain.post.dao; + +import com.keeper.homepage.domain.member.entity.Member; +import com.keeper.homepage.domain.post.entity.Post; +import com.keeper.homepage.domain.post.entity.category.Category; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.query.Param; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static com.keeper.homepage.domain.post.entity.QPost.*; + +@RequiredArgsConstructor +public class PostRepositoryImpl implements PostRepositoryCustom { + + private final JPAQueryFactory jpaQueryFactory; + + /** + * 카테고리 + 공지글 + 등록시간 최신순 정렬 + * + * @param category 게시글 카테고리 + */ + @Override + public List findAllNoticeByCategory(@Param("category") Category category) { + return jpaQueryFactory + .selectFrom(post) + .where( + post.category.eq(category) + .and(post.isNotice.isTrue()) + ) + .orderBy(post.registerTime.desc()) + .fetch(); + } + + /** + * 임시 저장글 제외 + 등록 시간 최신순 정렬 + */ + @Override + public List findAllRecent(Pageable pageable) { + return jpaQueryFactory + .selectFrom(post) + .where( + post.isTemp.isFalse() + .and(post.id.ne(1L)) + ) + .orderBy(post.registerTime.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + } + + /** + * 임시 저장글 제외 + 등록 시간 최신순 정렬 + 날짜 사이의 게시글 + * + * @param startDate 가져올 시작 시간 + * @param endDate 가져올 끝 시간 + */ + @Override + public List findAllTrend(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate) { + return jpaQueryFactory + .selectFrom(post) + .where( + post.isTemp.isFalse() + .and(post.registerTime.between(startDate, endDate)) + ) + .fetch(); + } + + @Override + public Optional findNextPost(@Param("postId") Long postId, @Param("category") Category category) { + return Optional.ofNullable( + jpaQueryFactory + .selectFrom(post) + .where( + post.id.gt(postId) + .and(post.category.eq(category)) + .and(post.isTemp.isFalse()) + ) + .orderBy(post.id.asc()) + .limit(1) + .fetchFirst() + ); + } + + @Override + public Optional findPreviousPost(@Param("postId") Long postId, @Param("category") Category category) { + return Optional.ofNullable( + jpaQueryFactory + .selectFrom(post) + .where( + post.id.lt(postId) + .and(post.category.eq(category)) + .and(post.isTemp.isFalse()) + ) + .orderBy(post.id.desc()) + .limit(1) + .fetchFirst() + ); + } + + @Override + public void updateVirtualMember(@Param("member") Member member, @Param("virtualMember") Member virtualMember) { + jpaQueryFactory + .update(post) + .set(post.member, virtualMember) + .where( + post.member.eq(member) + .and(post.isTemp.isFalse()) + ) + .execute(); + } + +}