worldcontroller.cpp 4.2 KB

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