File tree 8 files changed +134
-14
lines changed
8 files changed +134
-14
lines changed Original file line number Diff line number Diff line change
1
+ web : target/start -Dhttp.port=${PORT} -DapplyEvolutions.default=true -Ddb.default.url=${DATABASE_URL} -Ddb.default.driver=org.postgresql.Driver
Original file line number Diff line number Diff line change 1
1
package controllers ;
2
2
3
+ import models .Task ;
3
4
import play .*;
5
+ import play .data .Form ;
4
6
import play .mvc .*;
5
7
6
8
import views .html .*;
7
9
8
10
public class Application extends Controller {
9
-
11
+
12
+ static Form <Task > taskForm = Form .form (Task .class );
13
+
10
14
public static Result index () {
11
- return ok ( index . render ( "Your new application is ready." ));
15
+ return redirect ( routes . Application . tasks ( ));
12
16
}
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
+
14
41
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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 ">
2
28
3
- @main("Welcome to Play 2.1") {
4
-
5
- @play20.welcome(message, style = "Java")
6
-
7
29
}
30
+
31
+ }
Original file line number Diff line number Diff line change @@ -32,9 +32,9 @@ application.langs="en"
32
32
# ~~~~~
33
33
# You can declare as many datasources as you want.
34
34
# 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"
38
38
# db.default.user=sa
39
39
# db.default.password=""
40
40
#
@@ -50,8 +50,8 @@ application.langs="en"
50
50
# ~~~~~
51
51
# You can declare as many Ebean servers as you want.
52
52
# By convention, the default server is named `default`
53
- #
54
- # ebean.default="models.*"
53
+
54
+ ebean.default="models.*"
55
55
56
56
# Logger
57
57
# ~~~~~
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 5
5
# Home page
6
6
GET / controllers.Application.index()
7
7
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
+
8
13
# Map static resources from the /public folder to the /assets URL path
9
14
GET /assets/*file controllers.Assets.at(path="/public", file)
Original file line number Diff line number Diff line change @@ -8,7 +8,8 @@ object ApplicationBuild extends Build {
8
8
val appVersion = " 1.0-SNAPSHOT"
9
9
10
10
val appDependencies = Seq (
11
- // Add your project dependencies here,
11
+ " postgresql" % " postgresql" % " 8.4-702.jdbc4" ,
12
+
12
13
javaCore,
13
14
javaJdbc,
14
15
javaEbean
@@ -18,4 +19,5 @@ object ApplicationBuild extends Build {
18
19
// Add your own project settings here
19
20
)
20
21
22
+
21
23
}
You can’t perform that action at this time.
0 commit comments