worldcontroller.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. renderText(QLatin1String("Hello, World!"));
  13. }
  14. void WorldController::show(const QString &pk)
  15. {
  16. World world = World::get(pk.toUInt());
  17. texport(world);
  18. render();
  19. }
  20. void WorldController::queries()
  21. {
  22. queries("1");
  23. }
  24. void WorldController::queries(const QString &num)
  25. {
  26. QVariantList worlds;
  27. int d = qMin(qMax(num.toInt(), 1), 500);
  28. for (int i = 0; i < d; ++i) {
  29. int id = Tf::random(9999) + 1;
  30. worlds << World::get(id).toVariantMap();
  31. }
  32. renderJson(worlds);
  33. }
  34. void WorldController::random()
  35. {
  36. int id = Tf::random(9999) + 1;
  37. World world = World::get(id);
  38. renderJson(world.toVariantMap());
  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()
  106. {
  107. updates("1");
  108. }
  109. void WorldController::updates(const QString &num)
  110. {
  111. QVariantList worlds;
  112. int d = qMin(qMax(num.toInt(), 1), 500);
  113. World world;
  114. for (int i = 0; i < d; ++i) {
  115. int id = Tf::random(9999) + 1;
  116. world = World::get(id);
  117. world.setRandomNumber( Tf::random(9999) + 1 );
  118. world.update();
  119. worlds << world.toVariantMap();
  120. }
  121. renderJson(worlds);
  122. }
  123. void WorldController::remove(const QString &pk)
  124. {
  125. if (httpRequest().method() != Tf::Post) {
  126. return;
  127. }
  128. World world = World::get(pk.toUInt());
  129. world.remove();
  130. redirect(urla("index"));
  131. }
  132. /*
  133. MongoDB
  134. */
  135. void WorldController::mqueries()
  136. {
  137. mqueries("1");
  138. }
  139. void WorldController::mqueries(const QString &num)
  140. {
  141. QVariantList worlds;
  142. int d = qMin(qMax(num.toInt(), 1), 500);
  143. for (int i = 0; i < d; ++i) {
  144. QString id = QString::number(Tf::random(9999) + 1);
  145. worlds << MngWorld::get(id).toVariantMap();
  146. }
  147. renderJson(worlds);
  148. }
  149. void WorldController::mrandom()
  150. {
  151. int id = Tf::random(9999) + 1;
  152. World world = World::get(id);
  153. renderJson(world.toVariantMap());
  154. }
  155. void WorldController::mupdates()
  156. {
  157. mupdates("1");
  158. }
  159. void WorldController::mupdates(const QString &num)
  160. {
  161. QVariantList worlds;
  162. int d = qMin(qMax(num.toInt(), 1), 500);
  163. MngWorld world;
  164. for (int i = 0; i < d; ++i) {
  165. QString id = QString::number(Tf::random(9999) + 1);
  166. world = MngWorld::get(id);
  167. world.setRandomNumber( Tf::random(9999) + 1 );
  168. world.update();
  169. worlds << world.toVariantMap();
  170. }
  171. renderJson(worlds);
  172. }
  173. // Don't remove below this line
  174. T_DEFINE_CONTROLLER(WorldController)