EventsController.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. @Transactional
  16. public Result get()
  17. {
  18. List<Event> res = JPA.em().createQuery("SELECT e from Event e", Event.class).getResultList();
  19. if (res == null || res.size() == 0)
  20. {
  21. return notFound(Json.toJson("No Events"));
  22. }
  23. return ok(Json.toJson(res));
  24. }
  25. @Transactional
  26. public Result create()
  27. {
  28. Event e = Json.fromJson(request().body().asJson(), Event.class);
  29. e.eventId = 0;
  30. JPA.em().persist(e);
  31. return created(Json.toJson(e));
  32. }
  33. @Transactional
  34. public Result update(long id)
  35. {
  36. Event e = Json.fromJson(request().body().asJson(), Event.class);
  37. Event e0 = JPA.em().find(Event.class, id);
  38. if (e0 == null)
  39. {
  40. return notFound("No event with id "+id);
  41. }
  42. e.eventId = id;
  43. JPA.em().merge(e);
  44. return ok(Json.toJson(e));
  45. }
  46. @Transactional
  47. public Result delete(long id)
  48. {
  49. Event e = JPA.em().find(Event.class, id);
  50. if (e == null)
  51. {
  52. return notFound("No event with id "+id);
  53. }
  54. JPA.em().remove(e);
  55. return ok(Json.toJson(e));
  56. }
  57. }