Skip to content

Commit

Permalink
feat: 알약 검색 API 로직 수정 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sejineer authored May 30, 2024
1 parent f7e424c commit 7d86108
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<PillSearchResponseDto>> {
val results = pillService.searchPill(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Pill> {
fun readPill(pillShape: String, frontMarking: String?, backMarking: String?, color: Color): List<Pill> {
val pillEntities =
pillRepository.findPillEntitiesByDrugShapeAndColorClass1OrPrintFrontOrPrintBack(
pillRepository.findPillEntities(
pillShape,
frontMarking,
backMarking,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class PillService(

fun searchPill(
pillShape: String,
frontMarking: String,
backMarking: String,
frontMarking: String?,
backMarking: String?,
color: Color,
): List<PillSearchResponseDto> {
val pillDomainResponse = pillReader.readPill(pillShape, frontMarking, backMarking, color)
Expand Down
26 changes: 26 additions & 0 deletions storage/db-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaCompile> {
options.generatedSourceOutputDirectory.set(generated)
}

sourceSets {
main {
kotlin.srcDirs += generated
}
}

tasks.named("clean") {
doLast {
generated.deleteRecursively()
}
}

kapt {
generateStubs = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mediscan.storage.db.core

interface PillQuerydslRepository {
fun findPillEntities(
shape: String,
frontMarking: String?,
backMarking: String?,
color: String,
): List<PillEntity>
}
Original file line number Diff line number Diff line change
@@ -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<PillEntity> {
val pillEntities: List<PillEntity>

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.mediscan.storage.db.core

import org.springframework.data.jpa.repository.JpaRepository

interface PillJpaRepository : JpaRepository<PillEntity, Long> {
interface PillRepository : JpaRepository<PillEntity, Long>, PillQuerydslRepository {
fun findPillEntitiesByDrugShapeAndColorClass1OrPrintFrontOrPrintBack(
shape: String,
frontMarking: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 7d86108

Please sign in to comment.