Procházet zdrojové kódy

Set up memory database to use for testing and configured Event API to use (not GET for testing purposes)

Thomas Flucke před 9 roky
rodič
revize
abde86dd64

+ 24 - 5
server/app/controllers/EventsController.java

@@ -1,10 +1,10 @@
 package controllers;
 
+import play.db.jpa.Transactional;
+import play.db.jpa.JPA;
 import play.mvc.*;
 import play.libs.Json;
 
-import views.html.*;
-
 import java.util.List;
 import java.util.LinkedList;
 import java.util.Date;
@@ -54,19 +54,38 @@ public class EventsController extends Controller {
 	return ok(Json.toJson(res));
     }
 
+    @Transactional
     public Result create()
     {
-	return notFound();
+    	Event e = Json.fromJson(request().body().asJson(), Event.class);
+    	e.eventId = 0;
+		JPA.em().persist(e);
+		return created(Json.toJson(e));
     }
 
+    @Transactional
     public Result update(long id)
     {
-	return notFound();
+    	Event e = Json.fromJson(request().body().asJson(), Event.class);
+    	e.eventId = id;
+    	if (!JPA.em().contains(e))
+    	{
+    		return notFound("No event with id "+id);
+    	}
+		JPA.em().merge(e);
+		return ok(Json.toJson(e));
     }
 
+    @Transactional
     public Result delete(long id)
     {
-	return notFound();
+    	Event e = JPA.em().find(Event.class, id);
+    	if (e == null)
+    	{
+    		return notFound("No event with id "+id);
+    	}
+		JPA.em().remove(e);
+		return ok(Json.toJson(e));
     }
 
 }

+ 22 - 0
server/app/models/Event.java

@@ -0,0 +1,22 @@
+package models;
+
+import java.util.Date;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+
+@Entity
+public class Event
+{
+	@Id
+	@GeneratedValue(strategy=GenerationType.IDENTITY)
+    public long eventId;
+    public long userId;
+    public String name;
+    public Date time;
+    public String foodType;
+    public String description;
+    public double lat;
+    public double lng;
+}

+ 7 - 0
server/build.sbt

@@ -9,3 +9,10 @@ scalaVersion := "2.11.11"
 libraryDependencies += javaJdbc
 libraryDependencies += cache
 libraryDependencies += javaWs
+
+libraryDependencies ++= Seq(
+  javaJpa,
+  "org.hibernate" % "hibernate-entitymanager" % "5.1.0.Final" // replace by your jpa implementation
+)
+
+PlayKeys.externalizeResources := false

+ 16 - 0
server/conf/META-INF/persistence.xml

@@ -0,0 +1,16 @@
+<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
+             version="2.1">
+
+    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
+        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
+        <non-jta-data-source>DefaultDS</non-jta-data-source>
+        <properties>
+            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
+            <property name="schemaUpdate" value="true" />
+            <property name="hibernate.hbm2ddl.auto" value="update" />
+        </properties>
+    </persistence-unit>
+
+</persistence>

+ 8 - 2
server/conf/application.conf

@@ -304,6 +304,8 @@ play.evolutions {
   #db.default.enabled = false
 }
 
+play.evolutions.autoApply=true
+
 ## Database Connection Pool
 # https://www.playframework.com/documentation/latest/SettingsJDBC
 # ~~~~~
@@ -326,6 +328,8 @@ play.db {
   }
 }
 
+jpa.default=defaultPersistenceUnit
+
 ## JDBC Datasource
 # https://www.playframework.com/documentation/latest/JavaDatabase
 # https://www.playframework.com/documentation/latest/ScalaDatabase
@@ -343,8 +347,10 @@ db {
   # By convention, the default datasource is named `default`
 
   # https://www.playframework.com/documentation/latest/Developing-with-the-H2-Database
-  #default.driver = org.h2.Driver
-  #default.url = "jdbc:h2:mem:play"
+  default.jndiName=DefaultDS
+  default.driver = org.h2.Driver
+  default.url = "jdbc:h2:mem:play;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS TEST"
+  default.autoCreate = true
   #default.username = sa
   #default.password = ""