code dump

This commit is contained in:
Wyatt J. Miller 2023-11-13 07:30:43 -05:00
parent 562fef9d92
commit f61adb428d
2 changed files with 35 additions and 28 deletions

View File

@ -1,24 +1,25 @@
package com.wyattjmiller.entities package com.wyattjmiller.entities
import com.wyattjmiller.data.Authors
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
@SerialName("Recipe") @SerialName("Recipe")
data class Recipe( data class Recipe(
val id: Int?, val id: Int?,
val name: String, val name: String,
val desc: String, val desc: String,
val ingredients: String, val ingredients: String,
var createdTimestamp: String, var createdTimestamp: String,
val author: Int, val author: Int,
val deletedTimestamp: String,
) )
@Serializable @Serializable
@SerialName("RecipeDraft") @SerialName("RecipeDraft")
data class RecipeDraft( data class RecipeDraft(
val name: String, val name: String,
val desc: String, val desc: String,
val ingredients: String, val ingredients: String,
val author: Int, val author: Int,
) )

View File

@ -13,20 +13,24 @@ class AuthorRoute {
private val author = AuthorDao() private val author = AuthorDao()
private fun Route.getAllAuthors() { private fun Route.getAllAuthors() {
get("/author") { get("/author") { call.respond(author.getAllAuthors()) }
call.respond(author.getAllAuthors())
}
} }
private fun Route.getAuthor() { private fun Route.getAuthor() {
get("/author/{id?}") { get("/author/{id?}") {
val id = call.parameters["id"]?.toInt() ?: return@get call.respond( val id =
HttpStatusCode.BadRequest, "Missing author identifier :(" call.parameters["id"]?.toInt()
) ?: return@get call.respond(
HttpStatusCode.BadRequest,
"Missing author identifier :("
)
val res = author.getAuthor(id) ?: return@get call.respond( val res =
HttpStatusCode.NotFound, "No recipe found :(" author.getAuthor(id)
) ?: return@get call.respond(
HttpStatusCode.NotFound,
"No recipe found :("
)
call.respond(res) call.respond(res)
} }
@ -35,7 +39,7 @@ class AuthorRoute {
private fun Route.createAuthor() { private fun Route.createAuthor() {
post("/author") { post("/author") {
val req = call.receive<AuthorDraft>() val req = call.receive<AuthorDraft>()
val res = author.createAuthor(req) val _res = author.createAuthor(req)
call.respond(HttpStatusCode.Created) call.respond(HttpStatusCode.Created)
} }
} }
@ -43,16 +47,17 @@ class AuthorRoute {
private fun Route.updateAuthor() { private fun Route.updateAuthor() {
put("/author/{id?}") { put("/author/{id?}") {
val req = call.receive<AuthorDraft>() val req = call.receive<AuthorDraft>()
val id = call.parameters["id"]?.toIntOrNull() ?: return@put call.respond( val id =
HttpStatusCode.BadRequest, "Missing author identifier :(" call.parameters["id"]?.toIntOrNull()
) ?: return@put call.respond(
HttpStatusCode.BadRequest,
"Missing author identifier :("
)
if (author.updateAuthor(id, req)) { if (author.updateAuthor(id, req)) {
call.respond(HttpStatusCode.Created) call.respond(HttpStatusCode.Created)
} else { } else {
call.respond( call.respond(HttpStatusCode.NotFound, "No author found :(")
HttpStatusCode.NotFound, "No author found :("
)
} }
} }
} }
@ -67,3 +72,4 @@ class AuthorRoute {
} }
} }
} }