diff --git a/restaurants/src/main/kotlin/RestaurantJob.kt b/restaurants/src/main/kotlin/RestaurantJob.kt index b5f2aae..4f0b1a9 100644 --- a/restaurants/src/main/kotlin/RestaurantJob.kt +++ b/restaurants/src/main/kotlin/RestaurantJob.kt @@ -20,6 +20,7 @@ package app.jopiter.restaurants import app.jopiter.restaurants.model.Restaurant import app.jopiter.restaurants.repository.RestaurantItemRepository +import org.slf4j.LoggerFactory import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Component import java.time.LocalDate.now @@ -31,6 +32,12 @@ class RestaurantJob( private val repository: RestaurantItemRepository ) { + private val logger = LoggerFactory.getLogger(this::class.java) + @Scheduled(fixedRate = 15_000) - fun run() = Restaurant.values().forEach { repository.get(it.id, setOf(now())) } + fun run() = Restaurant.entries.forEach { + logger.info("Starting scheduled execution to fetch $it") + repository.fetchFromUsp(it.id) + logger.info("Finished scheduled execution to fetch $it") + } } diff --git a/restaurants/src/main/kotlin/model/Restaurant.kt b/restaurants/src/main/kotlin/model/Restaurant.kt index b94ca59..3027caa 100644 --- a/restaurants/src/main/kotlin/model/Restaurant.kt +++ b/restaurants/src/main/kotlin/model/Restaurant.kt @@ -115,6 +115,6 @@ enum class Restaurant(val id: Int, val restaurantName: String) { Eel2(23, "EEL - Área II"),; companion object { - fun getById(id: Int) = values().first { it.id == id } + fun getById(id: Int) = entries.first { it.id == id } } } diff --git a/restaurants/src/main/kotlin/repository/RestaurantItemRepository.kt b/restaurants/src/main/kotlin/repository/RestaurantItemRepository.kt index 78640a5..075b82b 100644 --- a/restaurants/src/main/kotlin/repository/RestaurantItemRepository.kt +++ b/restaurants/src/main/kotlin/repository/RestaurantItemRepository.kt @@ -33,22 +33,14 @@ class RestaurantItemRepository( fun get(restaurantId: Int, dates: Set) = dates.flatMap { fetch(restaurantId, it) }.toSet() private fun fetch(restaurantId: Int, date: LocalDate) = - fetchFromPostgres(restaurantId, date).ifEmpty { fetchFromUsp(restaurantId, date) } - - private fun fetchFromUsp(restaurantId: Int, date: LocalDate): Set { - val items = if (YearWeek.from(date) == YearWeek.now()) { - uspRestaurantItemRepository.fetch(restaurantId) - } else emptySet() - - val requested= items.filter { it.date == date } + fetchFromPostgres(restaurantId, date) + fun fetchFromUsp(restaurantId: Int): Set { + val items = uspRestaurantItemRepository.fetch(restaurantId) postgresRestaurantItemRepository.put(items) - - return requested.toSet() + return items } - private fun fetchFromPostgres( - restaurantId: Int, - date: LocalDate - ) = postgresRestaurantItemRepository.get(restaurantId, date) + private fun fetchFromPostgres(restaurantId: Int, date: LocalDate) = + postgresRestaurantItemRepository.get(restaurantId, date) } diff --git a/restaurants/src/main/kotlin/repository/postgres/PostgresRestaurantItemRepository.kt b/restaurants/src/main/kotlin/repository/postgres/PostgresRestaurantItemRepository.kt index 72e26ce..0f84f17 100644 --- a/restaurants/src/main/kotlin/repository/postgres/PostgresRestaurantItemRepository.kt +++ b/restaurants/src/main/kotlin/repository/postgres/PostgresRestaurantItemRepository.kt @@ -44,18 +44,12 @@ class PostgresRestaurantItemRepository( } } - fun get(restaurantId: Int, date: LocalDate): Set = restaurantItems - .filter { it.date eq date } - .filter { it.restaurantId eq restaurantId } - .map { it.toItem() } - .toSet() + fun get(restaurantId: Int, date: LocalDate, period: Period? = null): Set { + val query = restaurantItems.filter { it.date eq date }.filter { it.restaurantId eq restaurantId } + if(period != null) query.filter { it.period eq period } - fun get(restaurantId: Int, date: LocalDate, period: Period): Set = restaurantItems - .filter { it.date eq date } - .filter { it.restaurantId eq restaurantId } - .filter { it.period eq period } - .map { it.toItem() } - .toSet() + return query.map { it.toItem() }.toSet() + } } object RestaurantItems : Table("restaurant_item") {