Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

275 예약할 학생 검색 QueryDLS 적용 #282

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ dependencies {
// cloudwatch appender
implementation(Dependencies.CLOUDWATCH_APPENDER)

// queryDsl
implementation(Dependencies.QUERY_DSL)
kapt(Dependencies.QUERYDSL_APT)

// jakarta
kapt(Dependencies.JAKARTA_ANNOTATION_API)
kapt(Dependencies.JAKARTA_PERSISTENCE_API)
}

tasks.withType<KotlinCompile> {
Expand All @@ -63,7 +70,6 @@ tasks.withType<KotlinCompile> {
}
}


tasks.withType<Test> {
useJUnitPlatform()
}
Expand Down
8 changes: 8 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ object Dependencies {
// cloudwatch appender
const val CLOUDWATCH_APPENDER = "ca.pjer:logback-awslogs-appender:${DependencyVersions.CLOUDWATCH_APPENDER_VERSION}"

// queryDSL
const val QUERY_DSL = "com.querydsl:querydsl-jpa:${DependencyVersions.QUERY_DSL_VERSION}:jakarta"
const val QUERYDSL_APT = "com.querydsl:querydsl-apt:${DependencyVersions.QUERY_DSL_APT_VERSION}:jakarta"

// jakarta
const val JAKARTA_ANNOTATION_API = "jakarta.annotation:jakarta.annotation-api"
const val JAKARTA_PERSISTENCE_API = "jakarta.persistence:jakarta.persistence-api"

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface QueryUserService {
fun queryCurrentUser(): User
fun queryAllUserByNameContainingOrderBySchoolNumber(keyword: String): List<User>
fun queryAllUserByRoleOrderBySchoolNumber(role: UserRole): List<User>
fun queryAllUserByNameContainingAndRoleInOrderBySchoolNumber(keyword: String, role: List<UserRole>): List<User>
fun searchStudent(keyword: String): List<User>
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class QueryUserServiceImpl(
override fun queryAllUserByRoleOrderBySchoolNumber(role: UserRole): List<User> =
queryUserPort.queryAllUserByRoleOrderBySchoolNumber(role)

override fun queryAllUserByNameContainingAndRoleInOrderBySchoolNumber(keyword: String, role: List<UserRole>) =
queryUserPort.queryAllUserByNameContainingAndRoleInOrderBySchoolNumber(keyword, role)
override fun searchStudent(keyword: String): List<User> =
queryUserPort.searchStudent(keyword)

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface QueryUserPort {
fun queryCurrentUser(): User
fun queryAllUserByNameContainingOrderBySchoolNumber(keyword: String): List<User>
fun queryAllUserByRoleOrderBySchoolNumber(role: UserRole): List<User>
fun queryAllUserByNameContainingAndRoleInOrderBySchoolNumber(keyword: String, role: List<UserRole>): List<User>
fun searchStudent(keyword: String): List<User>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package team.msg.hiv2.domain.user.application.usecase

import team.msg.hiv2.domain.user.application.service.UserService
import team.msg.hiv2.domain.user.domain.constant.UserRole
import team.msg.hiv2.domain.user.presentation.data.request.SearchUserKeywordRequest
import team.msg.hiv2.domain.user.presentation.data.response.StudentResponse
import team.msg.hiv2.global.annotation.usecase.ReadOnlyUseCase
Expand All @@ -12,7 +11,7 @@ class SearchReservationUsersByNameKeywordUseCase(
) {

fun execute(request: SearchUserKeywordRequest): List<StudentResponse> {
val users = userService.queryAllUserByNameContainingAndRoleInOrderBySchoolNumber(request.keyword, listOf(UserRole.ROLE_STUDENT, UserRole.ROLE_ADMIN))
val users = userService.searchStudent(request.keyword)

return users.map { StudentResponse.of(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UserPersistenceAdapter(
override fun queryAllUserByRoleOrderBySchoolNumber(role: UserRole): List<User> =
userRepository.findAllByRoleOrderBySchoolNumberAsc(role).map { userMapper.toDomain(it)!! }

override fun queryAllUserByNameContainingAndRoleInOrderBySchoolNumber(keyword: String, role: List<UserRole>): List<User> =
userRepository.findAllByNameContainingAndRoleInOrderBySchoolNumberAsc(keyword, role).map { userMapper.toDomain(it)!! }
override fun searchStudent(keyword: String): List<User> =
userRepository.searchStudent(keyword).map { userMapper.toDomain(it)!! }

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package team.msg.hiv2.domain.user.persistence.repository
import org.springframework.data.repository.CrudRepository
import team.msg.hiv2.domain.user.domain.constant.UserRole
import team.msg.hiv2.domain.user.persistence.entity.UserJpaEntity
import team.msg.hiv2.domain.user.persistence.repository.custom.CustomUserRepository
import java.util.*

interface UserRepository : CrudRepository<UserJpaEntity, UUID> {
interface UserRepository : CrudRepository<UserJpaEntity, UUID>, CustomUserRepository {

fun findByEmail(email: String): UserJpaEntity?
fun existsByEmail(email: String): Boolean
fun findAllByNameContainingOrderBySchoolNumberAsc(keyword: String): List<UserJpaEntity>
fun findAllByRoleOrderBySchoolNumberAsc(role: UserRole): List<UserJpaEntity>
fun findAllByNameContainingAndRoleInOrderBySchoolNumberAsc(keyword: String,role: List<UserRole>): List<UserJpaEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team.msg.hiv2.domain.user.persistence.repository.custom

import team.msg.hiv2.domain.user.persistence.entity.UserJpaEntity

interface CustomUserRepository {
fun searchStudent(keyword: String): List<UserJpaEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package team.msg.hiv2.domain.user.persistence.repository.custom

import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.jpa.impl.JPAQueryFactory
import team.msg.hiv2.domain.user.domain.constant.UserRole
import team.msg.hiv2.domain.user.persistence.entity.QUserJpaEntity.userJpaEntity
import team.msg.hiv2.domain.user.persistence.entity.UserJpaEntity

class CustomUserRepositoryImpl (
private val jpaQueryFactory: JPAQueryFactory
) : CustomUserRepository {

override fun searchStudent(keyword: String): List<UserJpaEntity> =
jpaQueryFactory
.selectFrom(userJpaEntity)
.where(roleCondition())
.where(keywordLike(keyword))
.orderBy(userJpaEntity.schoolNumber.asc())
.fetch()

private fun roleCondition(): BooleanExpression =
userJpaEntity.role.eq(UserRole.ROLE_STUDENT)
.or(userJpaEntity.role.eq(UserRole.ROLE_ADMIN))

private fun keywordLike(keyword: String): BooleanExpression? =
if(keyword == "") null
else userJpaEntity.name.contains(keyword).or(userJpaEntity.schoolNumber.contains(keyword))

}
18 changes: 18 additions & 0 deletions src/main/kotlin/team/msg/hiv2/global/config/QueryDslConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package team.msg.hiv2.global.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
class QueryDslConfig {

@PersistenceContext
private lateinit var entityManager: EntityManager

@Bean
fun jpaQueryFactory() = JPAQueryFactory(entityManager)

}
Loading