Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cafe-05-end 셋팅 #4

Open
wants to merge 1 commit into
base: lecture/cafe-05-start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/kotlin/com/example/Application.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example

import com.example.config.configureDatabase
import com.example.config.configureDependencyInjection
import com.example.config.configureRouting
import com.example.config.configureSerialization
import com.example.domain.CafeMenuTable
Expand All @@ -13,8 +14,7 @@ fun main(args: Array<String>) {

fun Application.module() {
configureDatabase()
configureDependencyInjection()
configureSerialization()

val cafeMenuRepository = CafeMenuRepository(CafeMenuTable)
configureRouting(cafeMenuRepository)
configureRouting()
}
28 changes: 28 additions & 0 deletions src/main/kotlin/com/example/config/DependencyInjection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.config

import com.example.domain.CafeMenuTable
import com.example.domain.CafeOrderTable
import com.example.domain.CafeUserTable
import com.example.domain.repository.CafeMenuRepository
import com.example.domain.repository.CafeOrderRepository
import com.example.domain.repository.CafeUserRepository
import com.example.service.MenuService
import io.ktor.server.application.*
import org.koin.dsl.module
import org.koin.ktor.plugin.Koin
import org.koin.logger.slf4jLogger

val appModule = module {
single { CafeMenuRepository(CafeMenuTable) }
single { CafeUserRepository(CafeUserTable) }
single { CafeOrderRepository(CafeOrderTable) }

single { MenuService(get()) }
}

fun Application.configureDependencyInjection() {
install(Koin) {
slf4jLogger()
modules(appModule)
}
}
10 changes: 7 additions & 3 deletions src/main/kotlin/com/example/config/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package com.example.config

import com.example.domain.repository.CafeMenuRepository
import com.example.service.MenuService
import com.example.shared.CafeOrderStatus
import com.example.shared.dto.OrderDto
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.koin.ktor.ext.inject
import java.time.LocalDateTime


fun Application.configureRouting(cafeMenuRepository: CafeMenuRepository) {
fun Application.configureRouting() {
val menuService by inject<MenuService>()

routing {
get("/") {
call.respondText("Hello World!")
}

route("/api") {
get("/menus") {
val list = cafeMenuRepository.findAll()
val list = menuService.findAll()
call.respond(list)
}
post("/orders") {
val request = call.receive<OrderDto.CreateRequest>()
val selectedMenu = cafeMenuRepository.read(request.menuId)!!
val selectedMenu = menuService.getMenu(request.menuId)
val order = OrderDto.DisplayResponse(
orderCode = "ordercode1",
menuName = selectedMenu.name,
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/com/example/service/MenuService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.service

import com.example.domain.model.CafeMenu
import com.example.domain.repository.CafeMenuRepository

class MenuService(
private val cafeMenuRepository: CafeMenuRepository
) {
fun findAll(): List<CafeMenu> {
return cafeMenuRepository.findAll()
}

fun getMenu(id: Long): CafeMenu {
return cafeMenuRepository.read(id)
?: throw IllegalArgumentException("Menu not found")
}
}