diff --git a/core/core-api/src/main/kotlin/org/mediscan/core/api/controller/PillIController.kt b/core/core-api/src/main/kotlin/org/mediscan/core/api/controller/PillIController.kt index 24d47f4..aa8e7a0 100644 --- a/core/core-api/src/main/kotlin/org/mediscan/core/api/controller/PillIController.kt +++ b/core/core-api/src/main/kotlin/org/mediscan/core/api/controller/PillIController.kt @@ -41,8 +41,8 @@ class PillIController( @GetMapping("/pill/search") fun searchPill( @RequestParam pillShape: String, - @RequestParam frontMarking: String, - @RequestParam backMarking: String, + @RequestParam(required = false) frontMarking: String?, + @RequestParam(required = false) backMarking: String?, @RequestParam color: Color, ): ApiResponse> { val results = pillService.searchPill( diff --git a/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillReader.kt b/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillReader.kt index d349873..169b2da 100644 --- a/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillReader.kt +++ b/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillReader.kt @@ -1,16 +1,16 @@ package org.mediscan.core.api.domain import org.mediscan.core.enums.Color -import org.mediscan.storage.db.core.PillJpaRepository +import org.mediscan.storage.db.core.PillRepository import org.springframework.stereotype.Component @Component class PillReader( - private val pillRepository: PillJpaRepository, + private val pillRepository: PillRepository, ) { - fun readPill(pillShape: String, frontMarking: String, backMarking: String, color: Color): List { + fun readPill(pillShape: String, frontMarking: String?, backMarking: String?, color: Color): List { val pillEntities = - pillRepository.findPillEntitiesByDrugShapeAndColorClass1OrPrintFrontOrPrintBack( + pillRepository.findPillEntities( pillShape, frontMarking, backMarking, diff --git a/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillService.kt b/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillService.kt index 1c93644..8e11d81 100644 --- a/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillService.kt +++ b/core/core-api/src/main/kotlin/org/mediscan/core/api/domain/PillService.kt @@ -36,8 +36,8 @@ class PillService( fun searchPill( pillShape: String, - frontMarking: String, - backMarking: String, + frontMarking: String?, + backMarking: String?, color: Color, ): List { val pillDomainResponse = pillReader.readPill(pillShape, frontMarking, backMarking, color) diff --git a/storage/db-core/build.gradle.kts b/storage/db-core/build.gradle.kts index 756cffb..6aefb17 100644 --- a/storage/db-core/build.gradle.kts +++ b/storage/db-core/build.gradle.kts @@ -8,4 +8,30 @@ dependencies { api("org.springframework.boot:spring-boot-starter-data-jpa") runtimeOnly("com.mysql:mysql-connector-j") runtimeOnly("com.h2database:h2") + implementation("com.querydsl:querydsl-jpa:5.0.0:jakarta") + kapt("com.querydsl:querydsl-apt:5.0.0:jakarta") + kapt("jakarta.annotation:jakarta.annotation-api") + kapt("jakarta.persistence:jakarta.persistence-api") +} + +val generated = file("src/main/generated") + +tasks.withType { + options.generatedSourceOutputDirectory.set(generated) +} + +sourceSets { + main { + kotlin.srcDirs += generated + } +} + +tasks.named("clean") { + doLast { + generated.deleteRecursively() + } +} + +kapt { + generateStubs = true } diff --git a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillEntity.kt b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillEntity.kt index 66392fe..d050635 100644 --- a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillEntity.kt +++ b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillEntity.kt @@ -40,4 +40,17 @@ class PillEntity( val markCodeBack: String, val ediCode: String, val bizrno: String, -) +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is PillEntity) return false + + if (itemSeq != other.itemSeq) return false + + return true + } + + override fun hashCode(): Int { + return itemSeq.hashCode() + } +} diff --git a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillQuerydslRepository.kt b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillQuerydslRepository.kt new file mode 100644 index 0000000..bcd52c8 --- /dev/null +++ b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillQuerydslRepository.kt @@ -0,0 +1,10 @@ +package org.mediscan.storage.db.core + +interface PillQuerydslRepository { + fun findPillEntities( + shape: String, + frontMarking: String?, + backMarking: String?, + color: String, + ): List +} diff --git a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillQuerydslRepositoryImpl.kt b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillQuerydslRepositoryImpl.kt new file mode 100644 index 0000000..01d47f9 --- /dev/null +++ b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillQuerydslRepositoryImpl.kt @@ -0,0 +1,39 @@ +package org.mediscan.storage.db.core + +import com.querydsl.jpa.impl.JPAQueryFactory +import org.mediscan.storage.db.core.QPillEntity.pillEntity + +class PillQuerydslRepositoryImpl( + private val queryFactory: JPAQueryFactory, +) : PillQuerydslRepository { + override fun findPillEntities( + shape: String, + frontMarking: String?, + backMarking: String?, + color: String, + ): List { + val pillEntities: List + + val result1 = queryFactory.selectFrom(pillEntity) + .where( + pillEntity.drugShape.eq(shape) + .and(frontMarking?.let { pillEntity.printFront.eq(it) } ?: pillEntity.printFront.isNull) + .and(backMarking?.let { pillEntity.printBack.eq(it) } ?: pillEntity.printBack.isNull) + .and(pillEntity.colorClass1.eq(color)), + ) + .fetch() + + val result2 = queryFactory.selectFrom(pillEntity) + .where( + pillEntity.drugShape.eq(shape) + .and(backMarking?.let { pillEntity.printFront.eq(it) } ?: pillEntity.printFront.isNull) + .and(frontMarking?.let { pillEntity.printBack.eq(it) } ?: pillEntity.printBack.isNull) + .and(pillEntity.colorClass1.eq(color)), + ) + .fetch() + + pillEntities = result1 + result2 + pillEntities.distinct() + return pillEntities + } +} diff --git a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillJpaRepository.kt b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillRepository.kt similarity index 81% rename from storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillJpaRepository.kt rename to storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillRepository.kt index 9a55cac..bc8abea 100644 --- a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillJpaRepository.kt +++ b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/PillRepository.kt @@ -2,7 +2,7 @@ package org.mediscan.storage.db.core import org.springframework.data.jpa.repository.JpaRepository -interface PillJpaRepository : JpaRepository { +interface PillRepository : JpaRepository, PillQuerydslRepository { fun findPillEntitiesByDrugShapeAndColorClass1OrPrintFrontOrPrintBack( shape: String, frontMarking: String, diff --git a/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/config/CoreQuerydslConfig.kt b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/config/CoreQuerydslConfig.kt new file mode 100644 index 0000000..9ee8965 --- /dev/null +++ b/storage/db-core/src/main/kotlin/org/mediscan/storage/db/core/config/CoreQuerydslConfig.kt @@ -0,0 +1,16 @@ +package org.mediscan.storage.db.core.config + +import com.querydsl.jpa.impl.JPAQueryFactory +import jakarta.persistence.EntityManager +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class CoreQuerydslConfig( + val em: EntityManager, +) { + @Bean + fun queryFactory(): JPAQueryFactory { + return JPAQueryFactory(em) + } +}