package com.wyattjmiller.routes import com.wyattjmiller.entities.RecipeDraft import com.wyattjmiller.repositories.RecipeDao import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.request.* 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) } } private fun Route.createRecipe() { post("/recipe") { val req = call.receive() val res = rec.createRecipe(req) call.respond(res) } } private fun Route.updateRecipe() { put("/recipe/{id?}") { val req = call.receive() val id = call.parameters["id"]?.toIntOrNull() ?: return@put call.respond( HttpStatusCode.BadRequest,"Missing recipe identifier :(" ) if (rec.updateRecipe(id, req)) { call.respond(HttpStatusCode.OK) } else { call.respond(HttpStatusCode.NotFound, "No recipe found :(") } } } fun Application.recipeRoutes() { routing { getAllRecipes() getRecipe() createRecipe() updateRecipe() } } } }