EventsController.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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();
  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. e.eventId = id;
  38. if (!JPA.em().contains(e))
  39. {
  40. return notFound("No event with id "+id);
  41. }
  42. JPA.em().merge(e);
  43. return ok(Json.toJson(e));
  44. }
  45. @Transactional
  46. public Result delete(long id)
  47. {
  48. Event e = JPA.em().find(Event.class, id);
  49. if (e == null)
  50. {
  51. return notFound("No event with id "+id);
  52. }
  53. JPA.em().remove(e);
  54. return ok(Json.toJson(e));
  55. }
  56. }