fortunecontroller.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "fortunecontroller.h"
  2. #include "fortune.h"
  3. FortuneController::FortuneController(const FortuneController &)
  4. : ApplicationController()
  5. { }
  6. void FortuneController::index()
  7. {
  8. QList<Fortune> fortuneList = Fortune::getAll();
  9. texport(fortuneList);
  10. render();
  11. }
  12. void FortuneController::show(const QString &pk)
  13. {
  14. Fortune fortune = Fortune::get(pk.toUInt());
  15. texport(fortune);
  16. render();
  17. }
  18. void FortuneController::entry()
  19. {
  20. renderEntry();
  21. }
  22. void FortuneController::create()
  23. {
  24. if (httpRequest().method() != Tf::Post) {
  25. return;
  26. }
  27. QVariantMap form = httpRequest().formItems("fortune");
  28. Fortune fortune = Fortune::create(form);
  29. if (!fortune.isNull()) {
  30. QString notice = "Created successfully.";
  31. tflash(notice);
  32. redirect(urla("show", fortune.id()));
  33. } else {
  34. QString error = "Failed to create.";
  35. texport(error);
  36. renderEntry(form);
  37. }
  38. }
  39. void FortuneController::renderEntry(const QVariantMap &fortune)
  40. {
  41. texport(fortune);
  42. render("entry");
  43. }
  44. void FortuneController::edit(const QString &pk)
  45. {
  46. Fortune fortune = Fortune::get(pk.toUInt());
  47. if (!fortune.isNull()) {
  48. renderEdit(fortune.toVariantMap());
  49. } else {
  50. redirect(urla("entry"));
  51. }
  52. }
  53. void FortuneController::save(const QString &pk)
  54. {
  55. if (httpRequest().method() != Tf::Post) {
  56. return;
  57. }
  58. QString error;
  59. Fortune fortune = Fortune::get(pk.toUInt());
  60. if (fortune.isNull()) {
  61. error = "Original data not found. It may have been updated/removed by another transaction.";
  62. tflash(error);
  63. redirect(urla("edit", pk));
  64. return;
  65. }
  66. QVariantMap form = httpRequest().formItems("fortune");
  67. fortune.setProperties(form);
  68. if (fortune.save()) {
  69. QString notice = "Updated successfully.";
  70. tflash(notice);
  71. redirect(urla("show", pk));
  72. } else {
  73. error = "Failed to update.";
  74. texport(error);
  75. renderEdit(form);
  76. }
  77. }
  78. void FortuneController::renderEdit(const QVariantMap &fortune)
  79. {
  80. texport(fortune);
  81. render("edit");
  82. }
  83. void FortuneController::remove(const QString &pk)
  84. {
  85. if (httpRequest().method() != Tf::Post) {
  86. return;
  87. }
  88. Fortune fortune = Fortune::get(pk.toUInt());
  89. fortune.remove();
  90. redirect(urla("index"));
  91. }
  92. // Don't remove below this line
  93. T_REGISTER_CONTROLLER(fortunecontroller)