diff --git a/src/main/scala/TaskController.scala b/src/main/scala/TaskController.scala index 26492c2..158d580 100644 --- a/src/main/scala/TaskController.scala +++ b/src/main/scala/TaskController.scala @@ -1,37 +1,39 @@ import com.greenfossil.thorium.{*, given} -import com.greenfossil.data.mapping.{*, given} +import com.greenfossil.data.mapping.Mapping +import com.greenfossil.data.mapping.Mapping.* +import com.linecorp.armeria.common.MediaType import com.linecorp.armeria.server.annotation.{Get, Post, Param} import java.util.UUID object TaskController: - private val titleForm = mapping[String]("title" -> text) + private val titleField = Mapping("title", text) @Get("/health") - def health = Action: request => - Ok("""{"status":"ok"}""").as("application/json") + def health = Action: _ => + Ok("""{"status":"ok"}""").as(MediaType.JSON) @Get("/") - def index = Action: request => - Ok(renderPage()).as("text/html; charset=utf-8") + def index = Action: _ => + Ok(renderPage()).as(MediaType.HTML_UTF_8) @Post("/tasks") - def create = Action: implicit request => - titleForm.bindFromRequest().fold( - _ => Redirect("/"), - title => - if title.trim.nonEmpty then TaskStore.add(title) - Redirect("/") - ) + def create = Action: request => + val bound = titleField.bindFromRequest()(using request) + if bound.hasErrors then Redirect("/") + else + val title = bound.typedValueOpt.get + if title.trim.nonEmpty then TaskStore.add(title) + Redirect("/") @Post("/tasks/:id/toggle") - def toggle(@Param id: String) = Action: request => + def toggle(@Param id: String) = Action: _ => try TaskStore.toggle(UUID.fromString(id)) catch case _: IllegalArgumentException => () Redirect("/") @Post("/tasks/:id/delete") - def delete(@Param id: String) = Action: request => + def delete(@Param id: String) = Action: _ => try TaskStore.delete(UUID.fromString(id)) catch case _: IllegalArgumentException => () Redirect("/")