-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3df21b
commit 5aa3112
Showing
18 changed files
with
437 additions
and
315 deletions.
There are no files selected for viewing
41 changes: 11 additions & 30 deletions
41
...ver/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/meal/MealService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,38 @@ | ||
package com.bestswlkh0310.graduating.graduatingserver.api.meal | ||
|
||
import com.bestswlkh0310.graduating.graduatingserver.api.meal.res.MealRes | ||
import com.bestswlkh0310.graduating.graduatingserver.core.meal.MealEntity | ||
import com.bestswlkh0310.graduating.graduatingserver.core.global.safeSaveAll | ||
import com.bestswlkh0310.graduating.graduatingserver.core.meal.MealRepository | ||
import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolRepository | ||
import com.bestswlkh0310.graduating.graduatingserver.core.school.getBy | ||
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.NeisMealHelper | ||
import jakarta.persistence.PersistenceException | ||
import kotlinx.coroutines.runBlocking | ||
import mu.KLogger | ||
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.meal.NeisMealClient | ||
import org.springframework.stereotype.Service | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class MealService( | ||
private val mealRepository: MealRepository, | ||
private val schoolRepository: SchoolRepository, | ||
private val neisMealService: NeisMealHelper, | ||
private val logger: KLogger, | ||
private val neisMealClient: NeisMealClient, | ||
) { | ||
|
||
fun getMeals(schoolId: Long): List<MealRes> { | ||
val school = schoolRepository.getBy(schoolId) | ||
|
||
val currentTime = LocalDate.now() | ||
val schools = mealRepository.findBySchoolIdAndMealDate(schoolId, currentTime) | ||
val included = schools.any { | ||
it.mealDate == currentTime && it.school.id == schoolId | ||
} | ||
if (included) { | ||
if (schools.isNotEmpty()) { | ||
return schools.map { MealRes.of(it) } | ||
} | ||
// This code is really suck. | ||
return runBlocking { | ||
val result = arrayListOf<MealEntity>() | ||
neisMealService.getMeals( | ||
school = school, | ||
).forEach { | ||
try { | ||
result.add( | ||
mealRepository.save(it) | ||
) | ||
} catch (e: PersistenceException) { | ||
logger.error("duplicate mealEntity.") | ||
} | ||
} | ||
return@runBlocking result | ||
.filter { it.mealDate == currentTime } | ||
.map { MealRes.of(it) } | ||
} | ||
|
||
val meals = neisMealClient.getMeals(school = school) | ||
mealRepository.safeSaveAll(meals) | ||
|
||
return mealRepository.findBySchoolIdAndMealDate(schoolId, currentTime) | ||
.map { MealRes.of(it) } | ||
} | ||
|
||
fun deleteMealAll() { | ||
mealRepository.deleteAll() | ||
mealRepository.deleteOldRecords() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/global/safeSaveAll.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.bestswlkh0310.graduating.graduatingserver.core.global | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
fun <T : Any, ID> JpaRepository<T, ID>.safeSaveAll(entities: Iterable<T>) { | ||
entities.forEach { | ||
try { | ||
save(it) | ||
} catch (_: Exception) { | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/meal/MealRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package com.bestswlkh0310.graduating.graduatingserver.core.meal | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Modifying | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.stereotype.Repository | ||
import java.time.LocalDate | ||
|
||
@Repository | ||
interface MealRepository: JpaRepository<MealEntity, Long> { | ||
fun findBySchoolIdAndMealDate(schoolId: Long, mealDate: LocalDate): List<MealEntity> | ||
|
||
@Modifying | ||
@Query("DELETE FROM MealEntity e WHERE e.mealDate < CURRENT_DATE") | ||
fun deleteOldRecords() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 19 additions & 7 deletions
26
...in/kotlin/com/bestswlkh0310/graduating/graduatingserver/global/loader/NeisSchoolLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,38 @@ | ||
package com.bestswlkh0310.graduating.graduatingserver.global.loader | ||
|
||
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.NeisScheduleHelper | ||
import com.bestswlkh0310.graduating.graduatingserver.core.graduating.GraduatingRepository | ||
import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolRepository | ||
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.schedule.NeisScheduleClient | ||
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.school.NeisSchoolClient | ||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.boot.CommandLineRunner | ||
|
||
|
||
//@Component | ||
class DataLoader( | ||
private val neisService: NeisScheduleHelper | ||
class FetchSchoolRunner( | ||
private val neisSchoolClient: NeisSchoolClient, | ||
private val schoolRepository: SchoolRepository | ||
) : CommandLineRunner { | ||
override fun run(vararg args: String?) { | ||
neisService.importCsv("/Users/hhhello0507/Downloads/school.csv") | ||
val schools = neisSchoolClient.importCsv("/Users/hhhello0507/Downloads/school.csv") | ||
schoolRepository.saveAll(schools) | ||
} | ||
} | ||
|
||
//@Component | ||
class FetchSchool( | ||
private val neisService: NeisScheduleHelper | ||
class FetchScheduleRunner( | ||
private val neisScheduleClient: NeisScheduleClient, | ||
private val schoolRepository: SchoolRepository, | ||
private val graduatingRepository: GraduatingRepository, | ||
) : CommandLineRunner { | ||
override fun run(vararg args: String?) { | ||
runBlocking { | ||
neisService.getSchoolsDate() | ||
val graduatingEntities = schoolRepository.findAll() | ||
.map { school -> | ||
neisScheduleClient.getSchoolGraduatingDays(school) | ||
} | ||
.flatten() | ||
graduatingRepository.saveAll(graduatingEntities) | ||
} | ||
} | ||
} |
77 changes: 0 additions & 77 deletions
77
...rc/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/infra/neis/NeisMealHelper.kt
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
...r/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/infra/neis/NeisMealRes.kt
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
...n/kotlin/com/bestswlkh0310/graduating/graduatingserver/infra/neis/NeisMealRowResMapper.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.