diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 33d75e6..f5a730a 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -23,8 +23,19 @@ object Application extends Controller { Ok(views.html.index(Task.all(), taskForm)) } - def newTask = TODO + def newTask = Action { implicit request => + taskForm.bindFromRequest.fold( + errors => BadRequest(views.html.index(Task.all(), errors)), + label => { + Task.create(label) + Redirect(routes.Application.tasks) + } + ) + } - def deleteTask(id:Long) = TODO + def deleteTask(id:Long) = Action { + Task.delete(id) + Redirect(routes.Application.tasks) + } } \ No newline at end of file diff --git a/app/models/Task.scala b/app/models/Task.scala index 10e46fa..15b4ee5 100644 --- a/app/models/Task.scala +++ b/app/models/Task.scala @@ -1,11 +1,39 @@ package models +import anorm._ +import anorm.SqlParser._ + +import play.api.db._ +import play.api.Play.current + case class Task(id:Long, label:String) object Task { - def all(): List[Task] = Nil + + val task = { + get[Long]("id") ~ + get[String]("label") map { + case id~label => Task(id, label) + } + } + + def all(): List[Task] = DB.withConnection { implicit c => + SQL("select * from task").as(task *) + } - def create(label:String) {} + def create(label:String) { + DB.withConnection { implicit c => + SQL("insert into task (label) values ({label})").on( + 'label -> label + ).executeUpdate() + } + } - def delete(id:Long) {} + def delete(id:Long) { + DB.withConnection { implicit c => + SQL("delete from task where id = {id}").on( + 'id -> id + ).executeUpdate() + } + } } \ No newline at end of file