Skip to content

Instantly share code, notes, and snippets.

@guillaumebort
Created September 23, 2011 21:23
Show Gist options
  • Save guillaumebort/1238475 to your computer and use it in GitHub Desktop.
Save guillaumebort/1238475 to your computer and use it in GitHub Desktop.
@(id:Long,taskForm:play.data.Form[models.Task])
@main {
<h1>Edit task @id</h1>
@helper.html.form(action = routes.Tasks.update(id)) {
@helper.html.inputText(taskForm("name"))
@helper.html.inputText(taskForm("dueDate"))
@helper.html.checkbox(taskForm("done"))
<p>
<a href="@routes.Tasks.list()">Cancel</a> <input type="submit">
</p>
}
<hr>
@helper.html.form(action = routes.Tasks.delete(id)) {
<input type="submit" value="Delete this task">
}
}
@(taskForm:play.data.Form[models.Task])
@main {
<h1>New task</h1>
@helper.html.form(action = routes.Tasks.save()) {
@helper.html.inputText(taskForm("name"))
@helper.html.inputText(taskForm("dueDate"))
@helper.html.checkbox(taskForm("done"))
<p>
<a href="@routes.Tasks.list()">Cancel</a> <input type="submit">
</p>
}
}
@(body:Html)
<html>
<head>
<title>TODO List</title>
<link rel="stylesheet" type="text/css" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
</head>
<body>
<header>
<h1>TODO List</h1>
</header>
<section>
@body
</section>
<footer>
<a href="@routes.Tasks.list()">Home</a>
</footer>
</body>
</html>
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
GET / controllers.Tasks.list()
GET /tasks/new controllers.Tasks.create()
GET /tasks/:id controllers.Tasks.edit(id:Long)
POST /tasks controllers.Tasks.save()
POST /tasks/:id controllers.Tasks.update(id:Long)
POST /tasks/:id/delete controllers.Tasks.delete(id:Long)
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;
@Entity
public class Task extends Model {
@Id
public Long id;
@Constraints.Required
public String name;
public boolean done;
@Formats.DateTime(pattern="dd/MM/yyyy")
public Date dueDate = new Date();
public static Finder<Long,Task> find = new Finder(Long.class, Task.class);
}
package controllers;
import play.mvc.*;
import play.data.*;
import java.util.*;
import models.*;
import views.html.*;
public class Tasks extends Controller {
public static Result list() {
return ok(list.render(Task.find.all()));
}
public static Result edit(Long id) {
return ok(editForm.render(id, form(Task.find.byId(id))));
}
public static Result create() {
return ok(form.render(form(Task.class)));
}
public static Result save() {
Form<Task> taskForm = form(Task.class).bind();
if(taskForm.hasErrors()) {
return badRequest(form.render(taskForm));
} else {
taskForm.get().save();
return redirect(routes.Tasks.list());
}
}
public static Result update(Long id) {
Form<Task> taskForm = form(Task.class).bind();
if(taskForm.hasErrors()) {
return badRequest(editForm.render(id, taskForm));
} else {
taskForm.get().update(id);
return redirect(routes.Tasks.list());
}
}
public static Result delete(Long id) {
Task.find.ref(id).delete();
return redirect(routes.Tasks.list());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment