Skip to content

Commit

Permalink
Fix GetMeals api
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 10, 2024
1 parent b7ad028 commit 9ac9e4b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bestswlkh0310.graduating.graduatingserver.api.meal

import com.bestswlkh0310.graduating.graduatingserver.api.meal.req.GetMealsReq
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*


Expand All @@ -9,5 +11,6 @@ class MealController(
private val mealService: MealService
) {
@GetMapping
fun getMeals() = mealService.getMeals()
fun getMeals(@Valid @RequestBody req: GetMealsReq = GetMealsReq.current()) =
mealService.getMeals(req)
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
package com.bestswlkh0310.graduating.graduatingserver.api.meal

import com.bestswlkh0310.graduating.graduatingserver.api.meal.req.GetMealsReq
import com.bestswlkh0310.graduating.graduatingserver.api.meal.res.MealRes
import com.bestswlkh0310.graduating.graduatingserver.core.global.safeSaveAll
import com.bestswlkh0310.graduating.graduatingserver.core.meal.MealRepository
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserAuthenticationHolder
import com.bestswlkh0310.graduating.graduatingserver.global.exception.CustomException
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.meal.NeisMealClient
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.time.LocalDate

@Service
class MealService(
private val mealRepository: MealRepository,
private val neisMealClient: NeisMealClient,
private val authenticationHolder: UserAuthenticationHolder
) {
fun getMeals(): List<MealRes> {
val school = authenticationHolder.current().school ?: throw CustomException(HttpStatus.NOT_FOUND, "Not found school")

val currentTime = LocalDate.now()
val schools = mealRepository.findBySchoolIdAndMealDate(school.id, currentTime)
if (schools.isNotEmpty()) {
return schools.map { MealRes.of(it) }
}

val meals = neisMealClient.getMeals(school = school)
mealRepository.safeSaveAll(meals)

return mealRepository.findBySchoolIdAndMealDate(school.id, currentTime)
.map { MealRes.of(it) }
}

fun deleteMealAll() {
mealRepository.deleteOldRecords()
}
interface MealService {
fun getMeals(req: GetMealsReq): List<MealRes>
fun deleteMealAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.bestswlkh0310.graduating.graduatingserver.api.meal

import com.bestswlkh0310.graduating.graduatingserver.api.meal.req.GetMealsReq
import com.bestswlkh0310.graduating.graduatingserver.api.meal.res.MealRes
import com.bestswlkh0310.graduating.graduatingserver.core.global.safeSaveAll
import com.bestswlkh0310.graduating.graduatingserver.core.meal.MealRepository
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserAuthenticationHolder
import com.bestswlkh0310.graduating.graduatingserver.global.exception.CustomException
import com.bestswlkh0310.graduating.graduatingserver.infra.neis.meal.NeisMealClient
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service

@Service
class MealServiceImpl(
private val mealRepository: MealRepository,
private val neisMealClient: NeisMealClient,
private val authenticationHolder: UserAuthenticationHolder
): MealService {
override fun getMeals(req: GetMealsReq): List<MealRes> {
val school = authenticationHolder.current().school ?: throw CustomException(HttpStatus.NOT_FOUND, "Not found school")

val time = req.toLocalDate()
val schools = mealRepository.findBySchoolIdAndMealDate(school.id, time)
if (schools.isNotEmpty()) {
return schools.map { MealRes.of(it) }
}

val meals = neisMealClient.getMeals(school = school)
mealRepository.safeSaveAll(meals)

return mealRepository.findBySchoolIdAndMealDate(school.id, time)
.map { MealRes.of(it) }
}

override fun deleteMealAll() {
mealRepository.deleteOldRecords()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bestswlkh0310.graduating.graduatingserver.api.meal.req

import jakarta.validation.constraints.Max
import jakarta.validation.constraints.Min
import java.time.LocalDate

data class GetMealsReq(
@Min(1900) @Max(2100)
val year: Int,
@Min(1) @Max(12)
val month: Int,
@Min(1) @Max(31)
val day: Int
) {
fun toLocalDate(): LocalDate = LocalDate.of(year, month, day)

companion object {
fun current(): GetMealsReq {
val now = LocalDate.now()
return GetMealsReq(
year = now.year,
month = now.monthValue,
day = now.dayOfMonth
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.bestswlkh0310.graduating.graduatingserver.infra.schedule

import com.bestswlkh0310.graduating.graduatingserver.api.meal.MealService
import com.bestswlkh0310.graduating.graduatingserver.api.meal.MealServiceImpl
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class MealScheduler(
private val mealService: MealService
private val mealService: MealServiceImpl
) {

@Scheduled(cron = "0 0 23 * * *", zone = "Asia/Seoul")
Expand Down

0 comments on commit 9ac9e4b

Please sign in to comment.