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 7, 2024
1 parent 2306663 commit 41dd92b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.*
class MealController(
private val mealService: MealService
) {
@GetMapping("/{schoolId}", "/{schoolId}/")
fun getMeals(@PathVariable("schoolId") schoolId: Long) =
mealService.getMeals(schoolId)
@GetMapping
fun getMeals() = mealService.getMeals()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ 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.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 schoolRepository: SchoolRepository,
private val neisMealClient: NeisMealClient,
private val sessionHolder: UserAuthenticationHolder
) {

fun getMeals(schoolId: Long): List<MealRes> {
val school = schoolRepository.getBy(schoolId)
fun getMeals(): List<MealRes> {
val school = sessionHolder.current().school ?: throw CustomException(HttpStatus.NOT_FOUND, "Not found school")

val currentTime = LocalDate.now()
val schools = mealRepository.findBySchoolIdAndMealDate(schoolId, currentTime)
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(schoolId, currentTime)
return mealRepository.findBySchoolIdAndMealDate(school.id, currentTime)
.map { MealRes.of(it) }
}

Expand Down

0 comments on commit 41dd92b

Please sign in to comment.