fortunecontroller.cpp 2.6 KB

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