worldcontroller.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "worldcontroller.h"
  2. #include "world.h"
  3. WorldController::WorldController(const WorldController &)
  4. : ApplicationController()
  5. { }
  6. void WorldController::index()
  7. {
  8. QList<World> worldList = World::getAll();
  9. texport(worldList);
  10. render();
  11. }
  12. void WorldController::show(const QString &pk)
  13. {
  14. World world = World::get(pk.toUInt());
  15. texport(world);
  16. render();
  17. }
  18. void WorldController::queries(const QString &num)
  19. {
  20. QList<QVariantMap> worlds;
  21. int d = num.toInt();
  22. for (int i = 0; i < d; ++i) {
  23. int id = Tf::random(9999) + 1;
  24. World world = World::get(id);
  25. worlds << world.toVariantMap();
  26. }
  27. setContentType("application/json");
  28. renderText(jsonEncode(worlds), false);
  29. }
  30. void WorldController::random()
  31. {
  32. int id = Tf::random(9999) + 1;
  33. World world = World::get(id);
  34. setContentType("application/json");
  35. renderText(jsonEncode(world.toVariantMap()), false);
  36. }
  37. void WorldController::entry()
  38. {
  39. renderEntry();
  40. }
  41. void WorldController::create()
  42. {
  43. if (httpRequest().method() != Tf::Post) {
  44. return;
  45. }
  46. QVariantMap form = httpRequest().formItems("world");
  47. World world = World::create(form);
  48. if (!world.isNull()) {
  49. QString notice = "Created successfully.";
  50. tflash(notice);
  51. redirect(urla("show", world.id()));
  52. } else {
  53. QString error = "Failed to create.";
  54. texport(error);
  55. renderEntry(form);
  56. }
  57. }
  58. void WorldController::renderEntry(const QVariantMap &world)
  59. {
  60. texport(world);
  61. render("entry");
  62. }
  63. void WorldController::edit(const QString &pk)
  64. {
  65. World world = World::get(pk.toUInt());
  66. if (!world.isNull()) {
  67. renderEdit(world.toVariantMap());
  68. } else {
  69. redirect(urla("entry"));
  70. }
  71. }
  72. void WorldController::save(const QString &pk)
  73. {
  74. if (httpRequest().method() != Tf::Post) {
  75. return;
  76. }
  77. QString error;
  78. World world = World::get(pk.toUInt());
  79. if (world.isNull()) {
  80. error = "Original data not found. It may have been updated/removed by another transaction.";
  81. tflash(error);
  82. redirect(urla("edit", pk));
  83. return;
  84. }
  85. QVariantMap form = httpRequest().formItems("world");
  86. world.setProperties(form);
  87. if (world.save()) {
  88. QString notice = "Updated successfully.";
  89. tflash(notice);
  90. redirect(urla("show", pk));
  91. } else {
  92. error = "Failed to update.";
  93. texport(error);
  94. renderEdit(form);
  95. }
  96. }
  97. void WorldController::renderEdit(const QVariantMap &world)
  98. {
  99. texport(world);
  100. render("edit");
  101. }
  102. void WorldController::remove(const QString &pk)
  103. {
  104. if (httpRequest().method() != Tf::Post) {
  105. return;
  106. }
  107. World world = World::get(pk.toUInt());
  108. world.remove();
  109. redirect(urla("index"));
  110. }
  111. // Don't remove below this line
  112. T_REGISTER_CONTROLLER(worldcontroller)