fortunecontroller.cpp 2.7 KB

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