Skip to content

Instantly share code, notes, and snippets.

@hiroakit
Last active December 25, 2015 14:49
Show Gist options
  • Save hiroakit/6993702 to your computer and use it in GitHub Desktop.
Save hiroakit/6993702 to your computer and use it in GitHub Desktop.
このGistにはJavaToDoList ( http://www.playframework-ja.org/documentation/2.1.5/JavaTodoList ) の内容を、データベースをPostgreSQLに変更する際に必要なコード修正について記述しました。(修正が必要な3ファイルの内容を一つにまとめています)
// Task.javaを修正する
package models;
import java.util.*;
import play.db.ebean.*;
import play.data.validation.Constraints.*;
import javax.persistence.*;
@Entity
public class Task extends Model {
@Id
private Long id;
@Required
private String label;
public Long getId()
{
return this.id;
}
public void setId(Long id)
{
this.id = id;
}
public String getLabel()
{
return this.label;
}
public void setLabel(String label)
{
this.label = label;
}
public static Finder<Long,Task> find = new Finder(
Long.class, Task.class
);
public static List<Task> all() {
return find.all();
}
public static void create (Task task) {
task.save();
}
public static void delete (Long id) {
find.ref(id).delete();
}
}
// Application.javaを修正する
package controllers;
import play.*;
import play.mvc.*;
import play.data.*;
import views.html.*;
import models.*;
public class Application extends Controller {
static Form<Task> taskForm = new Form(Task.class);
public static Result index() {
return redirect(routes.Application.tasks());
// return ok(index.render("Your new application is ready."));
}
public static Result tasks() {
return ok(
views.html.index.render(Task.all(), taskForm)
);
}
public static Result newTask() {
Form<Task> filledForm = taskForm.bindFromRequest();
if(filledForm.hasErrors()) {
return badRequest (
views.html.index.render(Task.all(), filledForm)
);
} else {
Task.create(filledForm.get());
return redirect(routes.Application.tasks());
}
}
public static Result deleteTask(Long id) {
Task.delete(id);
return redirect(routes.Application.tasks());
}
}
// index.scala.htmlを修正する
@(tasks: List[Task], taskForm: Form[Task])
@import helper._
@main("Todo list") {
<h1>@tasks.size() task(s)</h1>
<ul>
@for(task <- tasks) {
<li>
@task.getLabel
@form(routes.Application.deleteTask(task.getId)) {
<input type="submit" value="Delete">
}
</li>
}
</ul>
<h2>Add a new task</h2>
@form(routes.Application.newTask()) {
@inputText(taskForm("label"))
<input type="submit" value="Create">
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment