worldcontroller.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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::plain()
  13. {
  14. renderText(QLatin1String("Hello, World!"));
  15. }
  16. void WorldController::show(const QString &pk)
  17. {
  18. World world = World::get(pk.toUInt());
  19. texport(world);
  20. render();
  21. }
  22. void WorldController::queries(const QString &num)
  23. {
  24. QList<QVariantMap> worlds;
  25. int d = num.toInt();
  26. for (int i = 0; i < d; ++i) {
  27. int id = Tf::random(9999) + 1;
  28. worlds << World::get(id).toVariantMap();
  29. }
  30. setContentType("application/json; charset=UTF-8");
  31. renderText(jsonEncode(worlds), false);
  32. }
  33. void WorldController::random()
  34. {
  35. int id = Tf::random(9999) + 1;
  36. World world = World::get(id);
  37. setContentType("application/json; charset=UTF-8");
  38. renderText(jsonEncode(world.toVariantMap()), false);
  39. }
  40. void WorldController::entry()
  41. {
  42. renderEntry();
  43. }
  44. void WorldController::create()
  45. {
  46. if (httpRequest().method() != Tf::Post) {
  47. return;
  48. }
  49. QVariantMap form = httpRequest().formItems("world");
  50. World world = World::create(form);
  51. if (!world.isNull()) {
  52. QString notice = "Created successfully.";
  53. tflash(notice);
  54. redirect(urla("show", world.id()));
  55. } else {
  56. QString error = "Failed to create.";
  57. texport(error);
  58. renderEntry(form);
  59. }
  60. }
  61. void WorldController::renderEntry(const QVariantMap &world)
  62. {
  63. texport(world);
  64. render("entry");
  65. }
  66. void WorldController::edit(const QString &pk)
  67. {
  68. World world = World::get(pk.toUInt());
  69. if (!world.isNull()) {
  70. renderEdit(world.toVariantMap());
  71. } else {
  72. redirect(urla("entry"));
  73. }
  74. }
  75. void WorldController::save(const QString &pk)
  76. {
  77. if (httpRequest().method() != Tf::Post) {
  78. return;
  79. }
  80. QString error;
  81. World world = World::get(pk.toUInt());
  82. if (world.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("world");
  89. world.setProperties(form);
  90. if (world.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 WorldController::renderEdit(const QVariantMap &world)
  101. {
  102. texport(world);
  103. render("edit");
  104. }
  105. void WorldController::updates(const QString &num)
  106. {
  107. QList<QVariantMap> worlds;
  108. int d = num.toInt();
  109. World world;
  110. for (int i = 0; i < d; ++i) {
  111. int id = Tf::random(9999) + 1;
  112. world = World::get(id);
  113. world.setRandomNumber( Tf::random(9999) + 1 );
  114. world.update();
  115. worlds << world.toVariantMap();
  116. }
  117. setContentType("application/json; charset=UTF-8");
  118. renderText(jsonEncode(worlds), false);
  119. }
  120. void WorldController::remove(const QString &pk)
  121. {
  122. if (httpRequest().method() != Tf::Post) {
  123. return;
  124. }
  125. World world = World::get(pk.toUInt());
  126. world.remove();
  127. redirect(urla("index"));
  128. }
  129. // Don't remove below this line
  130. T_REGISTER_CONTROLLER(worldcontroller)