Skip to content

Commit

Permalink
#2 Meal Week 조회 API response 기존 규칙 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
Qbeom0925 committed Aug 18, 2024
1 parent 9d42df9 commit dcdb1b6
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 30 deletions.
2 changes: 2 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
//mysql
implementation("mysql:mysql-connector-java:8.0.33")
//swagger
implementation ("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package com.core.adapter.`in`.web
import com.core.adapter.`in`.web.dto.AdapterMealDto
import com.core.application.port.`in`.GetWeekMealUseCase
import com.core.common.response.ApplicationResponse
import io.swagger.v3.oas.annotations.Operation
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
class MealController(val getWeekMealUseCase: GetWeekMealUseCase) {


@GetMapping("/api/v1/meals/week")
@Operation(summary = "주간 식단 조회")
fun getWeekMeal(
@RequestParam("restaurantName") restaurantName: String
) : ApplicationResponse<List<AdapterMealDto.GetWeekMealRes>> {
val data = getWeekMealUseCase(restaurantName)
@RequestParam("restaurantIdx") restaurantIdx: Long
) : ApplicationResponse<List<AdapterMealDto.GroupedWeekMealRes>> {
val data = getWeekMealUseCase(restaurantIdx)
val toResponse = AdapterMealDto.GetWeekMealRes.from(data)
return ApplicationResponse.ok(toResponse)
}
Expand Down
18 changes: 14 additions & 4 deletions core/src/main/kotlin/com/core/adapter/in/web/dto/AdapterMealDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import java.math.BigDecimal
import java.time.LocalDate

class AdapterMealDto {
data class GroupedWeekMealRes(
val offeredAt: LocalDate,
val getWeekMealRes: List<GetWeekMealRes>
)

data class GetWeekMealRes(
val idx: Long,
val type: String,
Expand All @@ -14,19 +19,24 @@ class AdapterMealDto {
val meals: List<String>
) {
companion object {
fun from(data: List<Meal>): List<GetWeekMealRes> {
return data.map {
fun from(data: List<Meal>): List<GroupedWeekMealRes> {
val groupedData = data.map {
GetWeekMealRes(
idx = it.idx,
type = it.type.value,
status = it.status.value,
offeredAt = it.offeredAt,
price = it.price,
meals = it.meals.split(",")
meals = it.meals.split(",").map { meal -> meal.trim() }
)
}.groupBy { it.offeredAt }
return groupedData.map { (date, meals) ->
GroupedWeekMealRes(
offeredAt = date,
getWeekMealRes = meals
)
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import com.core.application.port.out.GetWeekMealPort
import org.springframework.stereotype.Component
import java.time.LocalDate

@Component
class MealPersistenceAdapter(
@Component class MealPersistenceAdapter(
private val mealRepository: MealRepository,
private val mealMapper: MealMapper
) : GetWeekMealPort {
override fun invoke(restaurantName: String, start: LocalDate, end: LocalDate): List<Meal> {
val weekMeal = mealRepository.getWeekMeal(restaurantName, start, end)
override fun invoke(restaurantIdx: Long, start: LocalDate, end: LocalDate): List<Meal> {
val weekMeal = mealRepository.getWeekMeal(restaurantIdx, start, end)
val applicationMeals = mealMapper.toDomain(weekMeal);
return applicationMeals
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ interface MealRepository: JpaRepository<MealJpaEntity, Long> {
@Query("""
SELECT m
FROM MealJpaEntity m
left join RestaurantJpaEntity r on r.name = :restaurantName
WHERE m.isDeleted = false and m.offeredAt between :start and :end
WHERE m.isDeleted = false and m.offeredAt between :start and :end and m.restaurantJpaEntity.idx = :restaurantIdx
""")
fun getWeekMeal(restaurantName: String, start: LocalDate, end: LocalDate): List<MealJpaEntity>
fun getWeekMeal(restaurantIdx: Long, start: LocalDate, end: LocalDate): List<MealJpaEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class MealReadService(
* @param restaurantName
* @return List<ApplicationMealDto.GetWeekMealRes>
*/
override fun invoke(restaurantName: String): List<Meal> {
override fun invoke(restaurantIdx: Long): List<Meal> {
val (startDay, endDay) = getWeekRange()
val meals = getWeekMealPort(restaurantName, startDay, endDay)
val meals = getWeekMealPort(restaurantIdx, startDay, endDay)
return meals
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.core.application.domain.meal.model.Meal

interface GetWeekMealUseCase {

operator fun invoke(campusName: String): List<Meal>
operator fun invoke(restaurantIdx: Long): List<Meal>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.time.LocalDate

interface GetWeekMealPort {

operator fun invoke(restaurantName: String,
operator fun invoke(restaurantIdx: Long,
start: LocalDate,
end: LocalDate): List<Meal>
}
20 changes: 20 additions & 0 deletions core/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/myongsik?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 1q2w3e4r!
jpa:
hibernate:
ddl-auto: update
show_sql: true
properties:
hibernate:
format_sql: true
highlight_sql: true

logging:
level:
org:
hibernate:
SQL: debug
orm.jdbc.bind: trace
13 changes: 1 addition & 12 deletions core/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@ server:
port: 8080

spring:
datasource:
url: jdbc:mysql://localhost:3306/myongsik?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 1q2w3e4r!

jpa:
show_sql: true
hibernate:
ddl-auto: update
ddl-auto: none
properties:
hibernate:
format_sql: true
highlight_sql: true
default_batch_fetch_size: 20
open-in-view: false
logging:
level:
org:
hibernate:
SQL: debug
orm.jdbc.bind: trace

0 comments on commit dcdb1b6

Please sign in to comment.