-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from Kusitms-29th-ASAP/feature/tranlate/menu
feat: 메뉴 번역 기능 추가
- Loading branch information
Showing
20 changed files
with
321 additions
and
32 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
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
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
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
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
27 changes: 13 additions & 14 deletions
27
src/main/kotlin/com/asap/asapbackend/domain/menu/domain/model/Menu.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,25 +1,24 @@ | ||
package com.asap.asapbackend.domain.menu.domain.model | ||
|
||
import com.asap.asapbackend.domain.school.domain.model.School | ||
import com.asap.asapbackend.global.domain.BaseDateEntity | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.FetchType | ||
import jakarta.persistence.ManyToOne | ||
import org.hibernate.annotations.JdbcTypeCode | ||
import org.hibernate.type.SqlTypes | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
|
||
@Entity | ||
class Menu( | ||
school: School, | ||
id: Long = 0L, | ||
schoolId: Long, | ||
day: LocalDate, | ||
foods: List<Food> | ||
) : BaseDateEntity() { | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
val school: School = school | ||
foods: List<Food>, | ||
createdAt: LocalDateTime = LocalDateTime.now(), | ||
updatedAt: LocalDateTime = LocalDateTime.now() | ||
){ | ||
val id: Long = id | ||
|
||
val schoolId: Long = schoolId | ||
|
||
val day: LocalDate = day | ||
|
||
@JdbcTypeCode(SqlTypes.JSON) | ||
val foods: List<Food> = foods | ||
|
||
val createdAt: LocalDateTime = createdAt | ||
val updatedAt: LocalDateTime = updatedAt | ||
} |
5 changes: 3 additions & 2 deletions
5
src/main/kotlin/com/asap/asapbackend/domain/menu/domain/repository/MenuRepository.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,11 +1,12 @@ | ||
package com.asap.asapbackend.domain.menu.domain.repository | ||
|
||
import com.asap.asapbackend.domain.menu.domain.model.Menu | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.time.LocalDate | ||
|
||
interface MenuRepository : JpaRepository<Menu, Long> { | ||
interface MenuRepository{ | ||
fun findBySchoolIdAndDay(schoolId: Long, day: LocalDate): Menu? | ||
|
||
fun findBySchoolIdAndDayInOrderByDayAsc(schoolId: Long, day: List<LocalDate>): List<Menu> | ||
|
||
fun saveAll(menus: List<Menu>): List<Menu> | ||
} |
19 changes: 13 additions & 6 deletions
19
src/main/kotlin/com/asap/asapbackend/domain/menu/domain/service/MenuAppender.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,17 +1,24 @@ | ||
package com.asap.asapbackend.domain.menu.domain.service | ||
|
||
import com.asap.asapbackend.batch.menu.MenuInfoProvider | ||
import com.asap.asapbackend.domain.menu.domain.model.Allergy | ||
import com.asap.asapbackend.domain.menu.domain.model.Food | ||
import com.asap.asapbackend.domain.menu.domain.model.Menu | ||
import com.asap.asapbackend.domain.menu.domain.repository.MenuRepository | ||
import com.asap.asapbackend.domain.menu.event.MultiMenuCreateEvent | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.stereotype.Service | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
@Service | ||
class MenuAppender( | ||
private val menuRepository: MenuRepository | ||
private val menuRepository: MenuRepository, | ||
private val applicationEventPublisher: ApplicationEventPublisher, | ||
private val objectMapper: ObjectMapper | ||
) { | ||
fun addMenu(menus: List<Menu>) { | ||
menuRepository.saveAll(menus) | ||
fun addMenus(menus: List<Menu>) { | ||
val savedMenu = menuRepository.saveAll(menus) | ||
val event = MultiMenuCreateEvent(savedMenu.toSet()) | ||
applicationEventPublisher.publishEvent(event) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/asap/asapbackend/domain/menu/event/MenuCreateEvent.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,8 @@ | ||
package com.asap.asapbackend.domain.menu.event | ||
|
||
import com.asap.asapbackend.domain.menu.domain.model.Menu | ||
|
||
data class MultiMenuCreateEvent( | ||
val menus: Set<Menu> | ||
) { | ||
} |
3 changes: 2 additions & 1 deletion
3
src/main/kotlin/com/asap/asapbackend/infrastructure/jpa/BaseEntity.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
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
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/com/asap/asapbackend/infrastructure/jpa/menu/MenuMapper.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,49 @@ | ||
package com.asap.asapbackend.infrastructure.jpa.menu | ||
|
||
import com.asap.asapbackend.domain.menu.domain.model.Food | ||
import com.asap.asapbackend.domain.menu.domain.model.Menu | ||
import com.asap.asapbackend.infrastructure.jpa.menu.entity.FoodEntity | ||
import com.asap.asapbackend.infrastructure.jpa.menu.entity.MenuEntity | ||
|
||
|
||
object MenuMapper { | ||
fun toModel(menuEntity: MenuEntity): Menu { | ||
return Menu( | ||
id = menuEntity.id, | ||
schoolId = menuEntity.schoolId, | ||
day = menuEntity.day, | ||
foods = toFoodModel(menuEntity.foods), | ||
createdAt = menuEntity.createdAt, | ||
updatedAt = menuEntity.updatedAt | ||
) | ||
} | ||
|
||
private fun toFoodModel(foodEntities: List<FoodEntity>): List<Food>{ | ||
return foodEntities.map { | ||
Food( | ||
name = it.name, | ||
allergies = it.allergies | ||
) | ||
} | ||
} | ||
|
||
fun toEntity(menu: Menu): MenuEntity { | ||
return MenuEntity( | ||
id = menu.id, | ||
schoolId = menu.schoolId, | ||
day = menu.day, | ||
foods = toFoodEntity(menu.foods), | ||
createdAt = menu.createdAt, | ||
updatedAt = menu.updatedAt | ||
) | ||
} | ||
|
||
private fun toFoodEntity(foods: List<Food>): List<FoodEntity>{ | ||
return foods.map { | ||
FoodEntity( | ||
name = it.name, | ||
allergies = it.allergies | ||
) | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/com/asap/asapbackend/infrastructure/jpa/menu/MenuRepositoryImpl.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,59 @@ | ||
package com.asap.asapbackend.infrastructure.jpa.menu | ||
|
||
import com.asap.asapbackend.domain.menu.domain.model.Menu | ||
import com.asap.asapbackend.domain.menu.domain.repository.MenuRepository | ||
import com.asap.asapbackend.global.util.LanguageExtractor | ||
import com.asap.asapbackend.infrastructure.jpa.MultiLanguageId | ||
import com.asap.asapbackend.infrastructure.jpa.menu.entity.MenuEntity | ||
import com.asap.asapbackend.infrastructure.jpa.menu.repository.MenuJpaRepository | ||
import com.asap.asapbackend.infrastructure.jpa.menu.repository.TranslatedMenuJpaRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Repository | ||
import java.time.LocalDate | ||
|
||
@Repository | ||
class MenuRepositoryImpl( | ||
private val menuJpaRepository: MenuJpaRepository, | ||
private val translatedMenuJpaRepository: TranslatedMenuJpaRepository, | ||
private val languageExtractor: LanguageExtractor | ||
) : MenuRepository { | ||
override fun findBySchoolIdAndDay(schoolId: Long, day: LocalDate): Menu? { | ||
val menu = menuJpaRepository.findBySchoolIdAndDay(schoolId, day) ?: return null | ||
val targetLanguageMenuId = getMenusMultiLanguageId(menu) | ||
translatedMenuJpaRepository.findByIdOrNull(targetLanguageMenuId)?.let { | ||
menu.changeFoods(it.foods) | ||
} | ||
return MenuMapper.toModel(menu) | ||
} | ||
|
||
|
||
|
||
override fun findBySchoolIdAndDayInOrderByDayAsc(schoolId: Long, day: List<LocalDate>): List<Menu> { | ||
val menus = menuJpaRepository.findBySchoolIdAndDayInOrderByDayAsc(schoolId, day) | ||
val menuFoods = | ||
translatedMenuJpaRepository.findAllByIdIn(getMenusMultiLanguageIds(menus)) | ||
.associate { it.id.id to it.foods } | ||
return menus.map { | ||
it.changeFoods(menuFoods[it.id]) | ||
MenuMapper.toModel(it) | ||
} | ||
} | ||
|
||
override fun saveAll(menus: List<Menu>) : List<Menu>{ | ||
val savedMenus = menuJpaRepository.saveAll(menus.map { MenuMapper.toEntity(it) }) | ||
return savedMenus.map { | ||
MenuMapper.toModel(it) | ||
} | ||
} | ||
|
||
private fun getMenusMultiLanguageId(menu: MenuEntity): MultiLanguageId { | ||
val targetLanguageMenuId = MultiLanguageId(menu.id, languageExtractor.getRequestLanguage()) | ||
return targetLanguageMenuId | ||
} | ||
|
||
private fun getMenusMultiLanguageIds(menus: List<MenuEntity>): List<MultiLanguageId> { | ||
return menus.map { | ||
MultiLanguageId(it.id, languageExtractor.getRequestLanguage()) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/asap/asapbackend/infrastructure/jpa/menu/entity/FoodEntity.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,10 @@ | ||
package com.asap.asapbackend.infrastructure.jpa.menu.entity | ||
|
||
import com.asap.asapbackend.domain.menu.domain.model.Allergy | ||
|
||
|
||
data class FoodEntity( | ||
val name: String, | ||
val allergies: List<Allergy> | ||
) { | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/asap/asapbackend/infrastructure/jpa/menu/entity/MenuEntity.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,50 @@ | ||
package com.asap.asapbackend.infrastructure.jpa.menu.entity | ||
|
||
import com.asap.asapbackend.infrastructure.jpa.BaseEntity | ||
import jakarta.persistence.* | ||
import org.hibernate.annotations.JdbcTypeCode | ||
import org.hibernate.type.SqlTypes | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
|
||
@Entity | ||
@Table( | ||
name = "menu", | ||
indexes = [ | ||
Index(name = "idx_menu_school_id", columnList = "school_id"), | ||
Index(name = "idx_menu_school_and_day", columnList = "school_id, day", unique = true) | ||
] | ||
) | ||
class MenuEntity( | ||
id: Long, | ||
schoolId: Long, | ||
day: LocalDate, | ||
foods: List<FoodEntity>, | ||
createdAt: LocalDateTime, | ||
updatedAt: LocalDateTime | ||
) : BaseEntity(createdAt, updatedAt) { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = id | ||
|
||
@Column( | ||
nullable = false, | ||
name = "school_id" | ||
|
||
) | ||
val schoolId: Long = schoolId | ||
|
||
val day: LocalDate = day | ||
|
||
@JdbcTypeCode(value = SqlTypes.JSON) | ||
var foods: List<FoodEntity> = foods | ||
|
||
|
||
|
||
fun changeFoods(foods: List<FoodEntity>?){ | ||
foods?.let { | ||
this.foods = foods | ||
} | ||
} | ||
} |
Oops, something went wrong.