worldcontroller.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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()
  23. {
  24. queries("1");
  25. }
  26. void WorldController::queries(const QString &num)
  27. {
  28. QList<QVariantMap> worlds;
  29. int d = qMin(qMax(num.toInt(), 1), 500);
  30. for (int i = 0; i < d; ++i) {
  31. int id = Tf::random(9999) + 1;
  32. worlds << World::get(id).toVariantMap();
  33. }
  34. setContentType("application/json; charset=UTF-8");
  35. renderText(jsonEncode(worlds), false);
  36. }
  37. void WorldController::random()
  38. {
  39. int id = Tf::random(9999) + 1;
  40. World world = World::get(id);
  41. setContentType("application/json; charset=UTF-8");
  42. renderText(jsonEncode(world.toVariantMap()), false);
  43. }
  44. void WorldController::entry()
  45. {
  46. renderEntry();
  47. }
  48. void WorldController::create()
  49. {
  50. if (httpRequest().method() != Tf::Post) {
  51. return;
  52. }
  53. QVariantMap form = httpRequest().formItems("world");
  54. World world = World::create(form);
  55. if (!world.isNull()) {
  56. QString notice = "Created successfully.";
  57. tflash(notice);
  58. redirect(urla("show", world.id()));
  59. } else {
  60. QString error = "Failed to create.";
  61. texport(error);
  62. renderEntry(form);
  63. }
  64. }
  65. void WorldController::renderEntry(const QVariantMap &world)
  66. {
  67. texport(world);
  68. render("entry");
  69. }
  70. void WorldController::edit(const QString &pk)
  71. {
  72. World world = World::get(pk.toUInt());
  73. if (!world.isNull()) {
  74. renderEdit(world.toVariantMap());
  75. } else {
  76. redirect(urla("entry"));
  77. }
  78. }
  79. void WorldController::save(const QString &pk)
  80. {
  81. if (httpRequest().method() != Tf::Post) {
  82. return;
  83. }
  84. QString error;
  85. World world = World::get(pk.toUInt());
  86. if (world.isNull()) {
  87. error = "Original data not found. It may have been updated/removed by another transaction.";
  88. tflash(error);
  89. redirect(urla("edit", pk));
  90. return;
  91. }
  92. QVariantMap form = httpRequest().formItems("world");
  93. world.setProperties(form);
  94. if (world.save()) {
  95. QString notice = "Updated successfully.";
  96. tflash(notice);
  97. redirect(urla("show", pk));
  98. } else {
  99. error = "Failed to update.";
  100. texport(error);
  101. renderEdit(form);
  102. }
  103. }
  104. void WorldController::renderEdit(const QVariantMap &world)
  105. {
  106. texport(world);
  107. render("edit");
  108. }
  109. void WorldController::updates(const QString &num)
  110. {
  111. QList<QVariantMap> worlds;
  112. int d = num.toInt();
  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. setContentType("application/json; charset=UTF-8");
  122. renderText(jsonEncode(worlds), false);
  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. // Don't remove below this line
  134. T_REGISTER_CONTROLLER(worldcontroller)