Skip to content

Commit

Permalink
♻️ 데이터 리스트가 빈 리스트일때 함수 실행하지 않도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Huuuunee committed Aug 18, 2023
1 parent 86abff9 commit 1a94387
Showing 1 changed file with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,50 @@ class SignUpUseCase(
signUpData.projects.forEach {
val project = projectService.save(project = toProjectModel(it, studentId = student.id))
projectTechStackValid(techStacks, it.techStacks, project.id)
projectLinkService.saveAll(it.links.map { linkRequestData ->
toProjectLinkModel(projectLink = linkRequestData, projectId = project.id)
})
imageService.saveAll(it.previewImages.map { url ->
toImageModel(url = url, projectId = project.id)
})
saveAllIfNotEmpty(
it.links,
{ projectLink -> toProjectLinkModel(projectLink = projectLink, projectId = project.id) },
projectLinkService::saveAll
)
saveAllIfNotEmpty(
it.previewImages,
{ previewImage -> toImageModel(url = previewImage, projectId = project.id) },
imageService::saveAll
)
}

regionService.saveAll(signUpData.region.map { toRegionModel(it, studentId = student.id) }, student, user)
saveAllIfNotEmpty(
signUpData.region,
{ toRegionModel(region = it, studentId = student.id) },
regionService::saveAll
)

languageCertificateService.saveAll(signUpData.languageCertificate.map {
toLanguageCertificate(
languageCertificate = it,
studentId = student.id
)
}, student, user)
saveAllIfNotEmpty(
signUpData.languageCertificate,
{ toLanguageCertificate(languageCertificate = it, studentId = student.id) },
languageCertificateService::saveAll
)

certificateService.saveAll(
signUpData.certificate.map { toCertificate(certificate = it, studentId = student.id) },
student,
user
saveAllIfNotEmpty(
signUpData.certificate,
{ toCertificate(certificate = it, studentId = student.id) },
certificateService::saveAll
)

prizeService.saveAll(
signUpData.prizes
.map {
toPrizeModel(prize = it, studentId = student.id)
}
saveAllIfNotEmpty(
signUpData.prizes,
{ toPrizeModel(prize = it, studentId = student.id) },
prizeService::saveAll
)
}

private fun <T, R> saveAllIfNotEmpty(dataList: List<T>, transform: (T) -> R, saveFunction: (List<R>) -> Unit) {
dataList
.takeIf { it.isNotEmpty() }
?.map { transform(it) }
?.let { saveFunction(it) }
}

private fun toImageModel(url: String, projectId: Long): Image =
Image(
id = 0,
Expand Down Expand Up @@ -202,7 +215,11 @@ class SignUpUseCase(
studentId = studentId
)

private fun projectTechStackValid(stack: MutableList<TechStack>, projectTechStacks: List<String>, projectId: Long) {
private fun projectTechStackValid(
stack: MutableList<TechStack>,
projectTechStacks: List<String>,
projectId: Long
) {
for (stackItem in projectTechStacks) {
val techStackData = stack.find { it.stack == stackItem }
if (techStackData == null) {
Expand All @@ -218,15 +235,20 @@ class SignUpUseCase(
}
}

private fun studentTechStackValid(stack: MutableList<TechStack>, studentTechStacks: List<String>, studentId: UUID) {
private fun studentTechStackValid(
stack: MutableList<TechStack>,
studentTechStacks: List<String>,
studentId: UUID
) {
for (stackItem in studentTechStacks) {
val techStackData = stack.find { it.stack == stackItem }
if (techStackData == null) {
val techStack = techStackService.save(toStackModel(stackItem))
stack.add(0, techStack)
studentTechStackService.save(toStudentTechStackModel(studentId, techStack.id))
} else {
val techStack = techStackService.save(techStack = techStackData.copy(count = techStackData.count + 1))
val techStack =
techStackService.save(techStack = techStackData.copy(count = techStackData.count + 1))
stack.add(0, techStack)
studentTechStackService.save(toStudentTechStackModel(studentId, techStack.id))
}
Expand Down

0 comments on commit 1a94387

Please sign in to comment.