fortunecontroller.cpp 3.1 KB

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