Skip to content

Commit

Permalink
Refactor neis code
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 4, 2024
1 parent f3df21b commit 5aa3112
Show file tree
Hide file tree
Showing 18 changed files with 437 additions and 315 deletions.
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class MealController(
private val mealService: MealService
) {
@GetMapping("/{schoolId}", "/{schoolId}/")
fun getMeals(@PathVariable("schoolId") schoolId: Long, ) = mealService.getMeals(schoolId)
fun getMeals(@PathVariable("schoolId") schoolId: Long) =
mealService.getMeals(schoolId)
}
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) {
}
}
}
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bestswlkh0310.graduating.graduatingserver.global.config
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.json.GsonHttpMessageConverter
import org.springframework.web.client.RestClient

@Configuration
Expand Down
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)
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5aa3112

Please sign in to comment.