worldcontroller.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "worldcontroller.h"
  2. #include "world.h"
  3. #include "mngworld.h"
  4. WorldController::WorldController(const WorldController &)
  5. : ApplicationController()
  6. { }
  7. void WorldController::index()
  8. {
  9. QList<World> worldList = World::getAll();
  10. texport(worldList);
  11. render();
  12. }
  13. void WorldController::plain()
  14. {
  15. renderText(QLatin1String("Hello, World!"));
  16. }
  17. void WorldController::show(const QString &pk)
  18. {
  19. World world = World::get(pk.toUInt());
  20. texport(world);
  21. render();
  22. }
  23. void WorldController::queries()
  24. {
  25. queries("1");
  26. }
  27. void WorldController::queries(const QString &num)
  28. {
  29. QVariantList worlds;
  30. int d = qMin(qMax(num.toInt(), 1), 500);
  31. for (int i = 0; i < d; ++i) {
  32. int id = Tf::random(9999) + 1;
  33. worlds << World::get(id).toVariantMap();
  34. }
  35. renderJson(worlds);
  36. }
  37. void WorldController::random()
  38. {
  39. int id = Tf::random(9999) + 1;
  40. World world = World::get(id);
  41. renderJson(world.toVariantMap());
  42. }
  43. void WorldController::entry()
  44. {
  45. renderEntry();
  46. }
  47. void WorldController::create()
  48. {
  49. if (httpRequest().method() != Tf::Post) {
  50. return;
  51. }
  52. QVariantMap form = httpRequest().formItems("world");
  53. World world = World::create(form);
  54. if (!world.isNull()) {
  55. QString notice = "Created successfully.";
  56. tflash(notice);
  57. redirect(urla("show", world.id()));
  58. } else {
  59. QString error = "Failed to create.";
  60. texport(error);
  61. renderEntry(form);
  62. }
  63. }
  64. void WorldController::renderEntry(const QVariantMap &world)
  65. {
  66. texport(world);
  67. render("entry");
  68. }
  69. void WorldController::edit(const QString &pk)
  70. {
  71. World world = World::get(pk.toUInt());
  72. if (!world.isNull()) {
  73. renderEdit(world.toVariantMap());
  74. } else {
  75. redirect(urla("entry"));
  76. }
  77. }
  78. void WorldController::save(const QString &pk)
  79. {
  80. if (httpRequest().method() != Tf::Post) {
  81. return;
  82. }
  83. QString error;
  84. World world = World::get(pk.toUInt());
  85. if (world.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("world");
  92. world.setProperties(form);
  93. if (world.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 WorldController::renderEdit(const QVariantMap &world)
  104. {
  105. texport(world);
  106. render("edit");
  107. }
  108. void WorldController::updates()
  109. {
  110. updates("1");
  111. }
  112. void WorldController::updates(const QString &num)
  113. {
  114. QVariantList worlds;
  115. int d = qMin(qMax(num.toInt(), 1), 500);
  116. World world;
  117. for (int i = 0; i < d; ++i) {
  118. int id = Tf::random(9999) + 1;
  119. world = World::get(id);
  120. world.setRandomNumber( Tf::random(9999) + 1 );
  121. world.update();
  122. worlds << world.toVariantMap();
  123. }
  124. renderJson(worlds);
  125. }
  126. void WorldController::remove(const QString &pk)
  127. {
  128. if (httpRequest().method() != Tf::Post) {
  129. return;
  130. }
  131. World world = World::get(pk.toUInt());
  132. world.remove();
  133. redirect(urla("index"));
  134. }
  135. /*
  136. MongoDB
  137. */
  138. void WorldController::mqueries()
  139. {
  140. mqueries("1");
  141. }
  142. void WorldController::mqueries(const QString &num)
  143. {
  144. QVariantList worlds;
  145. int d = qMin(qMax(num.toInt(), 1), 500);
  146. for (int i = 0; i < d; ++i) {
  147. QString id = QString::number(Tf::random(9999) + 1);
  148. worlds << MngWorld::get(id).toVariantMap();
  149. }
  150. renderJson(worlds);
  151. }
  152. void WorldController::mrandom()
  153. {
  154. int id = Tf::random(9999) + 1;
  155. World world = World::get(id);
  156. renderJson(world.toVariantMap());
  157. }
  158. void WorldController::mupdates()
  159. {
  160. mupdates("1");
  161. }
  162. void WorldController::mupdates(const QString &num)
  163. {
  164. QVariantList worlds;
  165. int d = qMin(qMax(num.toInt(), 1), 500);
  166. MngWorld world;
  167. for (int i = 0; i < d; ++i) {
  168. QString id = QString::number(Tf::random(9999) + 1);
  169. world = MngWorld::get(id);
  170. world.setRandomNumber( Tf::random(9999) + 1 );
  171. world.update();
  172. worlds << world.toVariantMap();
  173. }
  174. renderJson(worlds);
  175. }
  176. // Don't remove below this line
  177. T_REGISTER_CONTROLLER(worldcontroller)