-
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
b7ad028
commit 9ac9e4b
Showing
5 changed files
with
75 additions
and
36 deletions.
There are no files selected for viewing
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
37 changes: 4 additions & 33 deletions
37
...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,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() | ||
} |
38 changes: 38 additions & 0 deletions
38
...src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/meal/MealServiceImpl.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,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() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/meal/req/GetMealsReq.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,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 | ||
) | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...main/kotlin/com/bestswlkh0310/graduating/graduatingserver/infra/schedule/MealScheduler.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