fortunecontroller.cpp 3.2 KB

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