Skip to content

Commit

Permalink
imp: add querydsl and extension function
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 7, 2024
1 parent 39addf5 commit 5ad5fb4
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 21 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
kapt("org.springframework.boot:spring-boot-configuration-processor")

/** querydsl */
implementation("com.querydsl:querydsl-jpa:${DependencyVersion.QUERYDSL}:jakarta")
kapt("com.querydsl:querydsl-apt:${DependencyVersion.QUERYDSL}:jakarta")

/** kotlin */
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/DependencyVersion.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
object DependencyVersion {
/** querydsl */
const val QUERYDSL = "5.0.0"

/** external */
const val ARROW_FX = "1.2.4"
const val KOTLIN_LOGGING = "6.0.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.hero.alignlab.domain.user.domain.UserInfo
import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.InvalidRequestException
import com.hero.alignlab.exception.InvalidTokenException
import com.hero.alignlab.extension.coExecute
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import java.time.LocalDateTime
Expand Down Expand Up @@ -59,28 +60,35 @@ class AuthFacade(
throw InvalidRequestException(ErrorCode.DUPLICATED_USERNAME_ERROR)
}

val userInfo = userService.save(UserInfo(nickname = request.username))
val userInfo = txTemplates.writer.coExecute {
val userInfo = userService.save(UserInfo(nickname = request.username))

credentialUserInfoService.save(
CredentialUserInfo(
uid = userInfo.id,
username = request.username,
password = EncryptData.enc(request.password, encryptor)
credentialUserInfoService.save(
CredentialUserInfo(
uid = userInfo.id,
username = request.username,
password = EncryptData.enc(request.password, encryptor)
)
)
)

return SignUpResponse(
accessToken = jwtTokenService.createToken(userInfo.id, TOKEN_EXPIRED_DATE)
)
userInfo
}

val accessToken = jwtTokenService.createToken(userInfo.id, TOKEN_EXPIRED_DATE)

return SignUpResponse(accessToken)
}

suspend fun signIn(request: SignInRequest): SignInResponse {
val credentialUserInfo = credentialUserInfoService.findByUsernameAndPassword(request.username, request.password)
val credentialUserInfo = credentialUserInfoService.findByUsernameAndPassword(
username = request.username,
password = request.password
)

val user = userService.getUserByIdOrThrowSync(credentialUserInfo.uid)
val userInfo = userService.getUserByIdOrThrowSync(credentialUserInfo.uid)

return SignInResponse(
accessToken = jwtTokenService.createToken(user.id, TOKEN_EXPIRED_DATE)
)
val accessToken = jwtTokenService.createToken(userInfo.id, TOKEN_EXPIRED_DATE)

return SignInResponse(accessToken)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class UserService(
getUserByIdOrThrowSync(user.uid)
}

return UserInfoResponse(
uid = userInfo.id,
nickname = userInfo.nickname
)
return UserInfoResponse.from(userInfo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@ package com.hero.alignlab.domain.user.infrastructure

import com.hero.alignlab.common.encrypt.EncryptData
import com.hero.alignlab.domain.user.domain.CredentialUserInfo
import com.hero.alignlab.domain.user.domain.QCredentialUserInfo
import com.hero.alignlab.domain.user.domain.QUserInfo
import jakarta.persistence.EntityManager
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
@Repository
interface CredentialUserInfoRepository : JpaRepository<CredentialUserInfo, Long> {
interface CredentialUserInfoRepository : JpaRepository<CredentialUserInfo, Long>, CredentialUserInfoQRepository {
fun existsByUsername(username: String): Boolean

fun findByUsernameAndPassword(username: String, password: EncryptData): CredentialUserInfo?
}

@Transactional(readOnly = true)
interface CredentialUserInfoQRepository {

}

class CredentialUserInfoRepositoryImpl : CredentialUserInfoQRepository,
QuerydslRepositorySupport(CredentialUserInfo::class.java) {
@Autowired
@Qualifier("heroEntityManager")
override fun setEntityManager(entityManager: EntityManager) {
super.setEntityManager(entityManager)
}

private val qCredentialUserInfo = QCredentialUserInfo.credentialUserInfo
private val qUserInfo = QUserInfo.userInfo


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package com.hero.alignlab.domain.user.model.response

import com.hero.alignlab.domain.user.domain.UserInfo

data class UserInfoResponse(
val uid: Long,
val nickname: String,
)
) {
companion object {
fun from(user: UserInfo): UserInfoResponse {
return UserInfoResponse(
uid = user.id,
nickname = user.nickname
)
}
}
}
83 changes: 83 additions & 0 deletions src/main/kotlin/com/hero/alignlab/extension/QuerydslExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.hero.alignlab.extension

import com.hero.alignlab.exception.AlignlabException
import com.hero.alignlab.exception.ErrorCode
import com.querydsl.core.types.dsl.*
import com.querydsl.jpa.impl.JPAQuery
import org.springframework.data.domain.*
import org.springframework.data.jpa.repository.support.Querydsl
import java.time.LocalDateTime

fun <T> Querydsl?.execute(query: JPAQuery<T>, pageable: Pageable): Page<T> {
return this.takeUnless { querydsl -> querydsl == null }
?.let { queryDsl ->
queryDsl.applyPagination(pageable, query).run {
PageImpl(this.fetch(), pageable, this.fetchCount())
}
} ?: throw AlignlabException(ErrorCode.QUERY_DSL_NOT_EXISTS_ERROR)
}

fun <T> Querydsl?.executeSlice(query: JPAQuery<T>, pageable: Pageable): Slice<T> {
return this.takeUnless { querydsl -> querydsl == null }
?.let { queryDsl ->
queryDsl.applyPagination(pageable, query).run {
this.limit(pageable.pageSize + 1L)
.fetch()
}.run {
var hasNext = false
if (this.size > pageable.pageSize) {
hasNext = true
this.removeAt(pageable.pageSize)
}
SliceImpl(this, pageable, hasNext)
}
} ?: throw AlignlabException(ErrorCode.QUERY_DSL_NOT_EXISTS_ERROR)
}

fun StringPath.isEquals(parameter: String?): BooleanExpression? {
return parameter?.let { param -> this.eq(param) }
}

fun NumberPath<Long>.isEquals(parameter: Long?): BooleanExpression? {
return parameter?.let { param -> this.eq(param) }
}

fun StringPath.isContains(parameter: String?): BooleanExpression? {
return parameter?.let { param -> this.contains(param) }
}

fun NumberPath<Long>.isIn(parameters: Set<Long>?): BooleanExpression? {
return parameters.takeUnless { params -> params.isNullOrEmpty() }?.let { params -> this.`in`(params) }
}

fun <T : Enum<T>> EnumPath<T>.isIn(parameters: Set<T>?): BooleanExpression? {
return parameters?.takeIf { params -> params.isNotEmpty() }?.let { params -> this.`in`(params) }
}

fun NumberPath<Long>.isGoe(parameter: Long?): BooleanExpression? {
return parameter?.let { param -> this.goe(param) }
}

fun NumberExpression<Long>.isGoe(parameter: Long?): BooleanExpression? {
return parameter?.let { param -> this.goe(param) }
}

fun NumberExpression<Long>.isLoe(parameter: Long?): BooleanExpression? {
return parameter?.let { param -> this.loe(param) }
}

fun NumberPath<Long>.isLoe(parameter: Long?): BooleanExpression? {
return parameter?.let { param -> this.loe(param) }
}

fun NumberPath<Long>.isNotIn(parameters: Set<Long>?): BooleanExpression? {
return parameters.takeUnless { params -> params.isNullOrEmpty() }?.let { params -> this.notIn(params) }
}

fun DateTimePath<LocalDateTime>.isGoe(parameter: LocalDateTime?): BooleanExpression? {
return parameter?.let { param -> this.goe(param) }
}

fun DateTimePath<LocalDateTime>.isLoe(parameter: LocalDateTime?): BooleanExpression? {
return parameter?.let { param -> this.loe(param) }
}

0 comments on commit 5ad5fb4

Please sign in to comment.