Browse Source

Flesh out the controller and model

Use Anorm's SQL Parser to map between queries and objects
master
unknown 11 years ago
parent
commit
b30af1102d
  1. 15
      app/controllers/Application.scala
  2. 34
      app/models/Task.scala

15
app/controllers/Application.scala

@ -23,8 +23,19 @@ object Application extends Controller {
Ok(views.html.index(Task.all(), taskForm)) 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)
}
} }

34
app/models/Task.scala

@ -1,11 +1,39 @@
package models package models
import anorm._
import anorm.SqlParser._
import play.api.db._
import play.api.Play.current
case class Task(id:Long, label:String) case class Task(id:Long, label:String)
object Task { 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()
}
}
} }