worldcontroller.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include "worldcontroller.h"
  2. #include "world.h"
  3. #include "pworld.h"
  4. #include "mngworld.h"
  5. #include <TCache>
  6. void WorldController::index()
  7. {
  8. QList<World> worldList = World::getAll();
  9. texport(worldList);
  10. render();
  11. }
  12. void WorldController::plain()
  13. {
  14. setContentType(QByteArrayLiteral("text/plain"));
  15. renderText(QStringLiteral("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 = std::min(std::max(num.toInt(), 1), 500);
  31. for (int i = 0; i < d; ++i) {
  32. int id = Tf::random(1, 10000);
  33. worlds << World::get(id).toVariantMap();
  34. }
  35. renderJson(worlds);
  36. }
  37. void WorldController::cached_queries()
  38. {
  39. cached_queries("1");
  40. }
  41. void WorldController::cached_queries(const QString &num)
  42. {
  43. constexpr int SECONDS = 60 * 10; // cache time
  44. QVariantList worlds;
  45. QVariantMap world;
  46. int d = std::min(std::max(num.toInt(), 1), 500);
  47. for (int i = 0; i < d; ++i) {
  48. int id = Tf::random(1, 10000);
  49. auto key = QByteArray::number(id);
  50. auto randomNumber = Tf::cache()->get(key); // Gets from cache
  51. if (randomNumber.isEmpty()) {
  52. auto w = World::get(id);
  53. worlds << w.toVariantMap();
  54. // Cache the value
  55. Tf::cache()->set(key, QByteArray::number(w.randomNumber()), SECONDS);
  56. } else {
  57. world.insert("id", id);
  58. world.insert("randomNumber", randomNumber.toInt());
  59. worlds << world;
  60. }
  61. }
  62. renderJson(worlds);
  63. }
  64. void WorldController::random()
  65. {
  66. int id = Tf::random(1, 10000);
  67. World world = World::get(id);
  68. renderJson(world.toVariantMap());
  69. }
  70. void WorldController::entry()
  71. {
  72. renderEntry();
  73. }
  74. void WorldController::create()
  75. {
  76. if (httpRequest().method() != Tf::Post) {
  77. return;
  78. }
  79. QVariantMap form = httpRequest().formItems("world");
  80. World world = World::create(form);
  81. if (!world.isNull()) {
  82. QString notice = "Created successfully.";
  83. tflash(notice);
  84. redirect(urla("show", world.id()));
  85. } else {
  86. QString error = "Failed to create.";
  87. texport(error);
  88. renderEntry(form);
  89. }
  90. }
  91. void WorldController::renderEntry(const QVariantMap &world)
  92. {
  93. texport(world);
  94. render("entry");
  95. }
  96. void WorldController::edit(const QString &pk)
  97. {
  98. World world = World::get(pk.toUInt());
  99. if (!world.isNull()) {
  100. renderEdit(world.toVariantMap());
  101. } else {
  102. redirect(urla("entry"));
  103. }
  104. }
  105. void WorldController::save(const QString &pk)
  106. {
  107. if (httpRequest().method() != Tf::Post) {
  108. return;
  109. }
  110. QString error;
  111. World world = World::get(pk.toUInt());
  112. if (world.isNull()) {
  113. error = "Original data not found. It may have been updated/removed by another transaction.";
  114. tflash(error);
  115. redirect(urla("edit", pk));
  116. return;
  117. }
  118. QVariantMap form = httpRequest().formItems("world");
  119. world.setProperties(form);
  120. if (world.save()) {
  121. QString notice = "Updated successfully.";
  122. tflash(notice);
  123. redirect(urla("show", pk));
  124. } else {
  125. error = "Failed to update.";
  126. texport(error);
  127. renderEdit(form);
  128. }
  129. }
  130. void WorldController::renderEdit(const QVariantMap &world)
  131. {
  132. texport(world);
  133. render("edit");
  134. }
  135. void WorldController::updates()
  136. {
  137. updates("1");
  138. }
  139. void WorldController::updates(const QString &num)
  140. {
  141. QVariantList worlds;
  142. int d = std::min(std::max(num.toInt(), 1), 500);
  143. World world;
  144. for (int i = 0; i < d; ++i) {
  145. int id = Tf::random(1, 10000);
  146. world = World::get(id);
  147. world.setRandomNumber( Tf::random(1, 10000) );
  148. world.update();
  149. worlds << world.toVariantMap();
  150. }
  151. renderJson(worlds);
  152. }
  153. void WorldController::remove(const QString &pk)
  154. {
  155. if (httpRequest().method() != Tf::Post) {
  156. return;
  157. }
  158. World world = World::get(pk.toUInt());
  159. world.remove();
  160. redirect(urla("index"));
  161. }
  162. /*
  163. * PostgreSQL
  164. */
  165. void WorldController::prandom()
  166. {
  167. int id = Tf::random(1, 10000);
  168. PWorld world = PWorld::get(id);
  169. renderJson(world.toVariantMap());
  170. }
  171. void WorldController::pqueries()
  172. {
  173. pqueries("1");
  174. }
  175. void WorldController::pqueries(const QString &num)
  176. {
  177. QVariantList worlds;
  178. int d = std::min(std::max(num.toInt(), 1), 500);
  179. for (int i = 0; i < d; ++i) {
  180. int id = Tf::random(1, 10000);
  181. worlds << PWorld::get(id).toVariantMap();
  182. }
  183. renderJson(worlds);
  184. }
  185. void WorldController::cached_pqueries()
  186. {
  187. cached_pqueries("1");
  188. }
  189. void WorldController::cached_pqueries(const QString &num)
  190. {
  191. constexpr int SECONDS = 60 * 30; // cache time
  192. QVariantList worlds;
  193. QVariantMap world;
  194. int d = std::min(std::max(num.toInt(), 1), 500);
  195. for (int i = 0; i < d; ++i) {
  196. int id = Tf::random(1, 10000);
  197. auto key = QByteArray::number(id);
  198. auto randomNumber = Tf::cache()->get(key); // Gets from cache
  199. if (randomNumber.isEmpty()) {
  200. auto w = PWorld::get(id);
  201. worlds << w.toVariantMap();
  202. // Cache the value
  203. Tf::cache()->set(key, QByteArray::number(w.randomNumber()), SECONDS);
  204. } else {
  205. world.insert("id", id);
  206. world.insert("randomnumber", randomNumber.toInt());
  207. worlds << world;
  208. }
  209. }
  210. renderJson(worlds);
  211. }
  212. void WorldController::pupdates(const QString &num)
  213. {
  214. QVariantList worlds;
  215. int d = std::min(std::max(num.toInt(), 1), 500);
  216. PWorld world;
  217. for (int i = 0; i < d; ++i) {
  218. int id = Tf::random(1, 10000);
  219. world = PWorld::get(id);
  220. world.setRandomNumber( Tf::random(1, 10000) );
  221. world.update();
  222. worlds << world.toVariantMap();
  223. }
  224. renderJson(worlds);
  225. }
  226. void WorldController::pupdates()
  227. {
  228. pupdates("1");
  229. }
  230. /*
  231. * MongoDB
  232. */
  233. void WorldController::mqueries()
  234. {
  235. mqueries("1");
  236. }
  237. void WorldController::mqueries(const QString &num)
  238. {
  239. QVariantList worlds;
  240. int d = std::min(std::max(num.toInt(), 1), 500);
  241. for (int i = 0; i < d; ++i) {
  242. QString id = QString::number(Tf::random(1, 10000));
  243. worlds << MngWorld::get(id).toVariantMap();
  244. }
  245. renderJson(worlds);
  246. }
  247. void WorldController::cached_mqueries()
  248. {
  249. cached_mqueries("1");
  250. }
  251. void WorldController::cached_mqueries(const QString &num)
  252. {
  253. constexpr int SECONDS = 60 * 10; // cache time
  254. QVariantList worlds;
  255. QVariantMap world;
  256. int d = std::min(std::max(num.toInt(), 1), 500);
  257. for (int i = 0; i < d; ++i) {
  258. int id = Tf::random(1, 10000);
  259. QByteArray key = QByteArray::number(id);
  260. auto randomNumber = Tf::cache()->get(key); // Gets from cache
  261. if (randomNumber.isEmpty()) {
  262. auto w = MngWorld::get(key);
  263. worlds << w.toVariantMap();
  264. // Cache the value
  265. Tf::cache()->set(key, QByteArray::number(w.randomNumber()), SECONDS);
  266. } else {
  267. world.insert("id", id);
  268. world.insert("randomNumber", randomNumber.toInt());
  269. worlds << world;
  270. }
  271. }
  272. renderJson(worlds);
  273. }
  274. void WorldController::mrandom()
  275. {
  276. QString id = QString::number(Tf::random(1, 10000));
  277. auto world = MngWorld::get(id);
  278. renderJson(world.toVariantMap());
  279. }
  280. void WorldController::mupdates()
  281. {
  282. mupdates("1");
  283. }
  284. void WorldController::mupdates(const QString &num)
  285. {
  286. QVariantList worlds;
  287. int d = std::min(std::max(num.toInt(), 1), 500);
  288. MngWorld world;
  289. for (int i = 0; i < d; ++i) {
  290. QString id = QString::number(Tf::random(1, 10000));
  291. world = MngWorld::get(id);
  292. world.setRandomNumber( Tf::random(1, 10000) );
  293. world.update();
  294. worlds << world.toVariantMap();
  295. }
  296. renderJson(worlds);
  297. }
  298. // Don't remove below this line
  299. T_DEFINE_CONTROLLER(WorldController)