Skip to content

Commit

Permalink
#2 Meal Week 조회 API 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
Qbeom0925 committed Aug 17, 2024
1 parent 24f3c1e commit 9d42df9
Show file tree
Hide file tree
Showing 29 changed files with 409 additions and 110 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ subprojects{
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("io.github.oshai:kotlin-logging-jvm:5.1.1")

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
}

}
Expand Down
4 changes: 3 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ plugins {


dependencies {

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
//mysql
implementation("mysql:mysql-connector-java:8.0.33")
}
20 changes: 11 additions & 9 deletions core/src/main/kotlin/com/core/adapter/in/web/MealController.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.core.adapter.`in`.web

import com.core.adapter.`in`.web.dto.AdapterMealDto
import com.core.application.port.`in`.GetMealUseCase
import com.core.application.port.`in`.GetWeekMealUseCase
import com.core.common.response.ApplicationResponse
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) {

@RestController("/api/v1/meals")
class MealController(private val getMealUseCase: GetMealUseCase) {

@GetMapping
fun getMeal() : ApplicationResponse<List<AdapterMealDto.GetMealRes>> {
val data = getMealUseCase.execute()
val toResponse = AdapterMealDto.GetMealRes.toResponse(data)
@GetMapping("/api/v1/meals/week")
fun getWeekMeal(
@RequestParam("restaurantName") restaurantName: String
) : ApplicationResponse<List<AdapterMealDto.GetWeekMealRes>> {
val data = getWeekMealUseCase(restaurantName)
val toResponse = AdapterMealDto.GetWeekMealRes.from(data)
return ApplicationResponse.ok(toResponse)
}

}
}
23 changes: 14 additions & 9 deletions core/src/main/kotlin/com/core/adapter/in/web/dto/AdapterMealDto.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.core.adapter.`in`.web.dto

import com.core.application.port.`in`.dto.ApplicationMealDto
import com.core.application.domain.meal.model.Meal
import java.math.BigDecimal
import java.time.LocalDate

class AdapterMealDto {

data class GetMealRes(
data class GetWeekMealRes(
val idx: Long,
val type: String,
val status: String,
val meals: String
val offeredAt: LocalDate,
val price: BigDecimal,
val meals: List<String>
) {
companion object {
fun toResponse(data: List<ApplicationMealDto.GetMealRes>) : List<GetMealRes>{
fun from(data: List<Meal>): List<GetWeekMealRes> {
return data.map {
GetMealRes(
GetWeekMealRes(
idx = it.idx,
type = it.type,
status = it.status,
meals = it.meals
type = it.type.value,
status = it.status.value,
offeredAt = it.offeredAt,
price = it.price,
meals = it.meals.split(",")
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.core.adapter.out.persistence

import com.core.adapter.out.persistence.mapper.MealMapper
import com.core.adapter.out.persistence.repository.MealRepository
import com.core.application.domain.meal.model.Meal
import com.core.application.port.out.GetWeekMealPort
import org.springframework.stereotype.Component
import java.time.LocalDate

@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)
val applicationMeals = mealMapper.toDomain(weekMeal);
return applicationMeals
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.core.adapter.out.persistence.entity

import com.core.common.BaseEntity
import jakarta.persistence.*

@Entity
@Table(name = "campus", indexes = [
Index(name = "campus_idx_name", columnList = "name"),
])
class CampusJpaEntity : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val idx: Long = 0

@Column(name = "name")
val name: String = "DEFAULT"

@Column(name = "address")
val address: String = "DEFAULT"

@Column(name = "is_deleted")
val isDeleted: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@ package com.core.adapter.out.persistence.entity

import com.core.adapter.out.persistence.enumerate.MealStatus
import com.core.adapter.out.persistence.enumerate.MealType
import com.core.common.BaseEntity
import jakarta.persistence.*
import org.hibernate.annotations.Comment
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Table(
name = "meal",
indexes = [Index(name = "meal_name_index", columnList = "name")]

name = "meals",
indexes = [Index(name = "meal_restaurant_idx", columnList = "restaurant_id")]
)
class MealJpaEntity {
class MealJpaEntity : BaseEntity(){
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var idx: Long = 0

@Column(name = "meals")
var meals: String = ""

@Column(name = "type", length = 20)
@Enumerated(EnumType.STRING)
var type: MealType? = MealType.LUNCH
var type: MealType = MealType.LUNCH

@Column(name = "status", length = 20)
@Enumerated(EnumType.STRING)
var status: MealStatus? = MealStatus.OPEN
var status: MealStatus = MealStatus.OPEN

@Comment("식사 가격")
@Column(name = "price")
var price: BigDecimal = BigDecimal.ZERO

@Column(name = "offered_at")
var offeredAt: LocalDate = LocalDate.now()

@Column(name = "is_deleted")
val isDeleted: Boolean = false

@ManyToOne
@JoinColumn(name = "meal_id")
var restaurantJpaEntity: RestaurantJpaEntity? = null
@JoinColumn(name = "restaurant_id")
var restaurantJpaEntity: RestaurantJpaEntity = RestaurantJpaEntity()


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.core.adapter.out.persistence.entity

import com.core.common.BaseEntity
import jakarta.persistence.*

@Entity
@Table(name = "restaurant", indexes = [
Index(name = "idx_name", columnList = "name"),
Index(name = "restaurant_idx_name", columnList = "name"),
])
class RestaurantJpaEntity {
class RestaurantJpaEntity : BaseEntity(){

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -18,9 +19,11 @@ class RestaurantJpaEntity {
@Column(name = "address")
val address: String = "DEFAULT"

@ManyToOne
@JoinColumn(name = "category_id")
val universityJpaEntity: UniversityJpaEntity? = null
@Column(name = "is_deleted")
val isDeleted: Boolean = false

@ManyToOne
@JoinColumn(name = "campus_id")
val campusJpaEntity: CampusJpaEntity = CampusJpaEntity()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.core.adapter.out.persistence.entity

class ScrapJpaEntity {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.core.adapter.out.persistence.entity

class StoreJpaEntity {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.core.adapter.out.persistence.entity

class UserJpaEntity {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.core.adapter.out.persistence.mapper

import com.core.adapter.out.persistence.entity.MealJpaEntity
import com.core.adapter.out.persistence.enumerate.MealStatus
import com.core.adapter.out.persistence.enumerate.MealType
import com.core.application.domain.meal.model.Meal
import org.springframework.stereotype.Component
import java.math.BigDecimal
import java.time.LocalDateTime

@Component
data class MealMapper(
var idx: Long = 0,
var meals: String = "",
var type: MealType = MealType.LUNCH,
var status: MealStatus = MealStatus.OPEN,
var price: BigDecimal = BigDecimal.ZERO,
var offeredAt: LocalDateTime = LocalDateTime.now(),
val isDeleted: Boolean = false,
val updatedAt: LocalDateTime = LocalDateTime.now(),
val createdAt: LocalDateTime = LocalDateTime.now(),
var restaurantName: String = "DEFAULT",
) {
fun toDomain(mealJpaEntities: List<MealJpaEntity>): List<Meal> {
return mealJpaEntities.map {
Meal(
idx = it.idx,
meals = it.meals,
type = it.type,
status = it.status,
price = it.price,
offeredAt = it.offeredAt,
isDeleted = it.isDeleted,
restaurantName = it.restaurantJpaEntity.name,
updatedAt = it.updatedAt,
createdAt = it.createdAt,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package com.core.adapter.out.persistence.repository

interface MealRepository {
import com.core.adapter.out.persistence.entity.MealJpaEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.time.LocalDate
import java.time.LocalDateTime

@Repository
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
""")
fun getWeekMeal(restaurantName: String, start: LocalDate, end: LocalDate): List<MealJpaEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.core.application.domain.meal.model

import java.time.LocalDateTime

data class Campus(
val idx: Long = 0,
val name: String = "DEFAULT",
val address: String = "DEFAULT",
val isDeleted: Boolean = false,
val updatedDate: LocalDateTime = LocalDateTime.now(),
val createdDate: LocalDateTime = LocalDateTime.now(),
) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.core.application.domain.meal.model

import jakarta.annotation.Generated
import jakarta.persistence.*
import com.core.adapter.out.persistence.enumerate.MealStatus
import com.core.adapter.out.persistence.enumerate.MealType
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime


data class Meal(
val idx: Long,
val type: String,
val status: String,
val meals: String
var idx: Long = 0,
var meals: String = "제공된 학식 정보가 없습니다.",
var type: MealType = MealType.LUNCH,
var status: MealStatus = MealStatus.OPEN,
var price: BigDecimal = BigDecimal.ZERO,
var offeredAt: LocalDate = LocalDate.now(),
val isDeleted: Boolean = false,
var restaurantName: String = "DEFAULT",
val updatedAt: LocalDateTime = LocalDateTime.now(),
val createdAt: LocalDateTime = LocalDateTime.now(),
) {


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.core.application.domain.meal.model

import java.time.LocalDateTime

data class Restaurant(
val idx: Long = 0,
val name: String = "DEFAULT",
val address: String = "DEFAULT",
val isDeleted: Boolean = false,
val campusJpaEntity: Campus = Campus(),
val updatedDate: LocalDateTime = LocalDateTime.now(),
val createdDate: LocalDateTime = LocalDateTime.now(),
) {
}
Loading

0 comments on commit 9d42df9

Please sign in to comment.