From 968aa5fab0f3ecb5dad55dc856286003cc1a7fd9 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Wed, 4 May 2022 14:32:01 -0400 Subject: [PATCH] added recipe route --- .../com/wyattjmiller/routes/RecipeRoute.kt | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/kotlin/com/wyattjmiller/routes/RecipeRoute.kt diff --git a/src/main/kotlin/com/wyattjmiller/routes/RecipeRoute.kt b/src/main/kotlin/com/wyattjmiller/routes/RecipeRoute.kt new file mode 100644 index 0000000..b85cb51 --- /dev/null +++ b/src/main/kotlin/com/wyattjmiller/routes/RecipeRoute.kt @@ -0,0 +1,40 @@ +package com.wyattjmiller.routes + +import com.wyattjmiller.repositories.RecipeDao +import io.ktor.http.* +import io.ktor.server.application.* +import io.ktor.server.response.* +import io.ktor.server.routing.* + +class RecipeRoute { + companion object { + private val rec = RecipeDao() + + private fun Route.getAllRecipes() { + get("/recipe") { + call.respond(rec.getAllRecipies()) + } + } + + private fun Route.getRecipe() { + get("/recipe/{id?}") { + val id = call.parameters["id"]?.toIntOrNull() ?: return@get call.respond( + HttpStatusCode.BadRequest, "Missing recipe identifier :(" + ) + + val res = rec.getRecipe(id) ?: return@get call.respond( + HttpStatusCode.NotFound, "No recipe found :(" + ) + + call.respond(res) + } + } + + fun Application.recipeRoutes() { + routing { + getAllRecipes() + getRecipe() + } + } + } +}