Skip to content

Commit

Permalink
feat: 추천 백신 조회 API (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-beeen authored Mar 23, 2024
1 parent d573f9d commit f4e9861
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ interface InoculationRepository : JpaRepository<Inoculation, UUID> {
"and i.vaccination.vaccinationType = :vaccinationType " +
"order by i.date desc")
fun findInoculationsByMemberIdAndVaccinationTypeAndDiseaseName(memberId: UUID, vaccinationType: VaccinationType, diseaseName: String): List<Inoculation>?


@Query("select distinct i " +
"from Inoculation i " +
"where i.member.id =: memberId")
fun findDistinctLatestInoculationsByMemberId(memberId: UUID): List<Inoculation>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import java.util.*

@Service
class SearchService(
val vaccinationRepository: VaccinationRepository,
val inoculationRepository: InoculationRepository,
val memberRepository: MemberRepository,
val diseaseService: DiseaseService,
val vaccinationRepository: VaccinationRepository,
val inoculationRepository: InoculationRepository,
val memberRepository: MemberRepository,
val diseaseService: DiseaseService,
) {
private fun findAllVaccinations(): List<Vaccination> {
return vaccinationRepository.findAll()
}

fun searchDisease(
age: List<AgeCondition>,
condition: List<HealthCondition>,
age: List<AgeCondition>,
condition: List<HealthCondition>,
): List<DiseaseSearchResponse> {
val diseases = diseaseService.findAll()

Expand All @@ -40,53 +40,53 @@ class SearchService(
}

fun searchVaccination(
age: List<AgeCondition>,
condition: List<HealthCondition>,
type: VaccinationType,
age: List<AgeCondition>,
condition: List<HealthCondition>,
type: VaccinationType,
): List<VaccinationSearchResponse> {
val diseases = this.searchDisease(age, condition)
val vaccinations = findAllVaccinations()

return vaccinations.filter {
diseases.any { disease -> it.diseaseName.contains(disease.name) } &&
it.vaccinationType == type
it.vaccinationType == type
}.map { VaccinationSearchResponse.of(it) }
}

fun searchRecommendVaccination(memberId: UUID): List<VaccinationSearchResponse> {
val member =
memberRepository.findById(memberId).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}
memberRepository.findById(memberId).orElseThrow {
BusinessException(MemberError.NOT_FOUND)
}

val ageCondition =
AgeCondition.getAgeCondition(member.memberDetails?.birthday?.year!!.minus(LocalDate.now().year))
AgeCondition.getAgeCondition(member.memberDetails?.birthday?.year!!.minus(LocalDate.now().year))
val vaccinations = findAllVaccinations()
val inoculatedDiseaseName =
inoculationRepository.findDistinctDiseaseNameByMemberId(memberId).flatMap { it.split("·") }.toSet()
inoculationRepository.findDistinctDiseaseNameByMemberId(memberId).flatMap { it.split("·") }.toSet()
val recommendedVaccinations =
vaccinations.filter { vaccination -> !inoculatedDiseaseName.contains(vaccination.vaccineName) }
vaccinations.filter { vaccination -> !inoculatedDiseaseName.contains(vaccination.vaccineName) }

val healthProfiles = member.healthProfiles.map { it.healthCondition }.toList()

val diseases =
this.searchDisease(listOf(ageCondition), healthProfiles).filter { response ->
!inoculatedDiseaseName.contains(response.name)
}.toList()
this.searchDisease(listOf(ageCondition), healthProfiles).filter { response ->
!inoculatedDiseaseName.contains(response.name)
}.toList()
return filterByDisease(recommendedVaccinations, diseases)
}

private fun filterByDisease(
vaccinations: List<Vaccination>,
diseases: List<DiseaseSearchResponse>,
vaccinations: List<Vaccination>,
diseases: List<DiseaseSearchResponse>,
) = vaccinations.filter {
diseases.any { disease -> it.diseaseName.contains(disease.name) }
}.map { VaccinationSearchResponse.of(it) }

fun isMatched(
disease: Disease,
age: List<AgeCondition>,
condition: List<HealthCondition>,
disease: Disease,
age: List<AgeCondition>,
condition: List<HealthCondition>,
): Boolean {
var conditionValue = 0
condition.forEach {
Expand All @@ -99,10 +99,10 @@ class SearchService(
}

return disease.ageFilter and ageValue == ageValue ||
(
disease.conditionalAgeFilter and ageValue == ageValue &&
disease.healthConditionFilter and conditionValue > 0 &&
disease.forbiddenHealthConditionFilter and conditionValue == 0
)
(
disease.conditionalAgeFilter and ageValue == ageValue &&
disease.healthConditionFilter and conditionValue > 0 &&
disease.forbiddenHealthConditionFilter and conditionValue == 0
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,51 @@ package com.vacgom.backend.search.presentation
import com.vacgom.backend.disease.application.dto.request.FilterRequest
import com.vacgom.backend.disease.domain.constants.AgeCondition
import com.vacgom.backend.disease.domain.constants.HealthCondition
import com.vacgom.backend.global.security.annotation.AuthId
import com.vacgom.backend.inoculation.domain.constants.VaccinationType
import com.vacgom.backend.search.application.SearchService
import com.vacgom.backend.search.application.dto.VaccinationSearchResponse
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
@RequestMapping("/api/v1/search")
class SearchController(
val searchService: SearchService,
val searchService: SearchService,
) {
@GetMapping()
@GetMapping
fun disease(
@RequestBody body: FilterRequest,
@RequestParam type: String,
@RequestBody body: FilterRequest,
@RequestParam type: String,
): ResponseEntity<List<Any>> {
when (type) {
"essential" -> {
return ResponseEntity.ok(
searchService.searchDisease(
body.age.map { AgeCondition.valueOf(it) },
body.condition.map { HealthCondition.valueOf(it) },
),
searchService.searchDisease(
body.age.map { AgeCondition.valueOf(it) },
body.condition.map { HealthCondition.valueOf(it) },
),
)
}

"nation" -> {
return ResponseEntity.ok(
searchService.searchVaccination(
body.age.map { AgeCondition.valueOf(it) },
body.condition.map { HealthCondition.valueOf(it) },
VaccinationType.NATION,
),
searchService.searchVaccination(
body.age.map { AgeCondition.valueOf(it) },
body.condition.map { HealthCondition.valueOf(it) },
VaccinationType.NATION,
),
)
}

"extra" -> {
return ResponseEntity.ok(
searchService.searchVaccination(
body.age.map { AgeCondition.valueOf(it) },
body.condition.map { HealthCondition.valueOf(it) },
VaccinationType.EXTRA,
),
searchService.searchVaccination(
body.age.map { AgeCondition.valueOf(it) },
body.condition.map { HealthCondition.valueOf(it) },
VaccinationType.EXTRA,
),
)
}

Expand All @@ -53,4 +56,9 @@ class SearchController(
}
}
}

@GetMapping("/recommend")
fun recommendVaccination(@AuthId id: UUID): ResponseEntity<List<VaccinationSearchResponse>> {
return ResponseEntity.ok(searchService.searchRecommendVaccination(id))
}
}

0 comments on commit f4e9861

Please sign in to comment.