EventsController.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 models.Event;
  8. /**
  9. * This controller contains an action to handle HTTP requests
  10. * to the application's home page.
  11. */
  12. public class EventsController extends Controller {
  13. @Transactional
  14. public Result get()
  15. {
  16. List<Event> res = JPA.em().createQuery("SELECT e from Event e", Event.class).getResultList();
  17. if (res == null || res.isEmpty())
  18. {
  19. return notFound(Json.toJson("No Events"));
  20. }
  21. return ok(Json.toJson(res));
  22. }
  23. @Transactional
  24. public Result create()
  25. {
  26. Event e = Json.fromJson(request().body().asJson(), Event.class);
  27. e.setEventId(0);
  28. JPA.em().persist(e);
  29. return created(Json.toJson(e));
  30. }
  31. @Transactional
  32. public Result update(long id)
  33. {
  34. Event e = Json.fromJson(request().body().asJson(), Event.class);
  35. Event e0 = JPA.em().find(Event.class, id);
  36. if (e0 == null)
  37. {
  38. return notFound("No event with id "+id);
  39. }
  40. e.setEventId(id);
  41. JPA.em().merge(e);
  42. return ok(Json.toJson(e));
  43. }
  44. @Transactional
  45. public Result delete(long id)
  46. {
  47. Event e = JPA.em().find(Event.class, id);
  48. if (e == null)
  49. {
  50. return notFound("No event with id "+id);
  51. }
  52. JPA.em().remove(e);
  53. return ok(Json.toJson(e));
  54. }
  55. }