Skip to content

Commit e35e349

Browse files
Andreas NordahlAndreas Nordahl
Andreas Nordahl
authored and
Andreas Nordahl
committed
tasks tutorial complete
1 parent d9316a6 commit e35e349

File tree

8 files changed

+134
-14
lines changed

8 files changed

+134
-14
lines changed

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: target/start -Dhttp.port=${PORT} -DapplyEvolutions.default=true -Ddb.default.url=${DATABASE_URL} -Ddb.default.driver=org.postgresql.Driver

app/controllers/Application.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
package controllers;
22

3+
import models.Task;
34
import play.*;
5+
import play.data.Form;
46
import play.mvc.*;
57

68
import views.html.*;
79

810
public class Application extends Controller {
9-
11+
12+
static Form<Task> taskForm = Form.form(Task.class);
13+
1014
public static Result index() {
11-
return ok(index.render("Your new application is ready."));
15+
return redirect(routes.Application.tasks());
1216
}
13-
17+
18+
public static Result tasks() {
19+
return ok(
20+
views.html.index.render(Task.all(), taskForm)
21+
);
22+
}
23+
24+
public static Result newTask() {
25+
Form<Task> filledForm = taskForm.bindFromRequest();
26+
if(filledForm.hasErrors()) {
27+
return badRequest(
28+
views.html.index.render(Task.all(), filledForm)
29+
);
30+
} else {
31+
Task.create(filledForm.get());
32+
return redirect(routes.Application.tasks());
33+
}
34+
}
35+
36+
public static Result deleteTask(Long id) {
37+
Task.delete(id);
38+
return redirect(routes.Application.tasks());
39+
}
40+
1441
}

app/models/Task.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package models;
2+
3+
import play.data.validation.Constraints.Required;
4+
import play.db.ebean.Model;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import java.util.List;
9+
10+
@Entity
11+
public class Task extends Model {
12+
13+
@Id
14+
public Long id;
15+
16+
@Required
17+
public String label;
18+
19+
public static Finder<Long,Task> find = new Finder(
20+
Long.class, Task.class
21+
);
22+
23+
public static List<Task> all() {
24+
return find.all();
25+
}
26+
27+
public static void create(Task task) {
28+
task.save();
29+
}
30+
31+
public static void delete(Long id) {
32+
find.ref(id).delete();
33+
}
34+
35+
}

app/views/index.scala.html

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
@(message: String)
1+
@(tasks: List[Task], taskForm: Form[Task])
2+
3+
@import helper._
4+
5+
@main("Todo list") {
6+
7+
<h1>@tasks.size() task(s)</h1>
8+
9+
<ul>
10+
@for(task <- tasks) {
11+
<li>
12+
@task.label
13+
14+
@form(routes.Application.deleteTask(task.id)) {
15+
<input type="submit" value="Delete">
16+
}
17+
</li>
18+
}
19+
</ul>
20+
21+
<h2>Add a new task</h2>
22+
23+
@form(routes.Application.newTask()) {
24+
25+
@inputText(taskForm("label"))
26+
27+
<input type="submit" value="Create">
228

3-
@main("Welcome to Play 2.1") {
4-
5-
@play20.welcome(message, style = "Java")
6-
729
}
30+
31+
}

conf/application.conf

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ application.langs="en"
3232
# ~~~~~
3333
# You can declare as many datasources as you want.
3434
# By convention, the default datasource is named `default`
35-
#
36-
# db.default.driver=org.h2.Driver
37-
# db.default.url="jdbc:h2:mem:play"
35+
36+
db.default.driver=org.h2.Driver
37+
db.default.url="jdbc:h2:mem:play"
3838
# db.default.user=sa
3939
# db.default.password=""
4040
#
@@ -50,8 +50,8 @@ application.langs="en"
5050
# ~~~~~
5151
# You can declare as many Ebean servers as you want.
5252
# By convention, the default server is named `default`
53-
#
54-
# ebean.default="models.*"
53+
54+
ebean.default="models.*"
5555

5656
# Logger
5757
# ~~~~~

conf/evolutions/default/1.sql

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# --- Created by Ebean DDL
2+
# To stop Ebean DDL generation, remove this comment and start using Evolutions
3+
4+
# --- !Ups
5+
6+
create table task (
7+
id bigint not null,
8+
label varchar(255),
9+
constraint pk_task primary key (id))
10+
;
11+
12+
create sequence task_seq;
13+
14+
15+
16+
17+
# --- !Downs
18+
19+
SET REFERENTIAL_INTEGRITY FALSE;
20+
21+
drop table if exists task;
22+
23+
SET REFERENTIAL_INTEGRITY TRUE;
24+
25+
drop sequence if exists task_seq;
26+

conf/routes

+5
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
# Home page
66
GET / controllers.Application.index()
77

8+
# Tasks
9+
GET /tasks controllers.Application.tasks()
10+
POST /tasks controllers.Application.newTask()
11+
POST /tasks/:id/delete controllers.Application.deleteTask(id: Long)
12+
813
# Map static resources from the /public folder to the /assets URL path
914
GET /assets/*file controllers.Assets.at(path="/public", file)

project/Build.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ object ApplicationBuild extends Build {
88
val appVersion = "1.0-SNAPSHOT"
99

1010
val appDependencies = Seq(
11-
// Add your project dependencies here,
11+
"postgresql" % "postgresql" % "8.4-702.jdbc4",
12+
1213
javaCore,
1314
javaJdbc,
1415
javaEbean
@@ -18,4 +19,5 @@ object ApplicationBuild extends Build {
1819
// Add your own project settings here
1920
)
2021

22+
2123
}

0 commit comments

Comments
 (0)