EventsController.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package controllers;
  2. import play.db.jpa.Transactional;
  3. import play.db.jpa.JPA;
  4. import play.mvc.*;
  5. import play.libs.Json;
  6. import java.util.List;
  7. import java.util.LinkedList;
  8. import java.util.Date;
  9. import models.Event;
  10. /**
  11. * This controller contains an action to handle HTTP requests
  12. * to the application's home page.
  13. */
  14. public class EventsController extends Controller {
  15. public Result get()
  16. {
  17. List<Event> res = new LinkedList<>();
  18. Event tmp = new Event();
  19. tmp.eventId = 1;
  20. tmp.userId = 1;
  21. tmp.name = "Hello World!";
  22. tmp.time = new Date();
  23. tmp.foodType = "BBQ";
  24. tmp.description = "Free BBQ here!";
  25. tmp.lat = 0;
  26. tmp.lng = 0;
  27. res.add(tmp);
  28. tmp = new Event();
  29. tmp.eventId = 2;
  30. tmp.userId = 1;
  31. tmp.name = "Free Pizza Knight";
  32. tmp.time = new Date();
  33. tmp.foodType = "Pizza";
  34. tmp.description = "Board games and Pizza!";
  35. tmp.lat = 0;
  36. tmp.lng = 0;
  37. res.add(tmp);
  38. tmp = new Event();
  39. tmp.eventId = 3;
  40. tmp.userId = 2;
  41. tmp.name = "Some other Event";
  42. tmp.time = new Date();
  43. tmp.foodType = "Other";
  44. tmp.description = "Really long\nDescription\nWith\nLots\nof\nnew\nlines. " +
  45. "ThisIsAReallyUnrrealisticallyLongWordThatIAmUsingAsATestCase.";
  46. tmp.lat = 0;
  47. tmp.lng = 0;
  48. res.add(tmp);
  49. return ok(Json.toJson(res));
  50. }
  51. @Transactional
  52. public Result create()
  53. {
  54. Event e = Json.fromJson(request().body().asJson(), Event.class);
  55. e.eventId = 0;
  56. JPA.em().persist(e);
  57. return created(Json.toJson(e));
  58. }
  59. @Transactional
  60. public Result update(long id)
  61. {
  62. Event e = Json.fromJson(request().body().asJson(), Event.class);
  63. e.eventId = id;
  64. if (!JPA.em().contains(e))
  65. {
  66. return notFound("No event with id "+id);
  67. }
  68. JPA.em().merge(e);
  69. return ok(Json.toJson(e));
  70. }
  71. @Transactional
  72. public Result delete(long id)
  73. {
  74. Event e = JPA.em().find(Event.class, id);
  75. if (e == null)
  76. {
  77. return notFound("No event with id "+id);
  78. }
  79. JPA.em().remove(e);
  80. return ok(Json.toJson(e));
  81. }
  82. }