worldcontroller.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. QVariantList 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. renderJson(worlds);
  35. }
  36. void WorldController::random()
  37. {
  38. int id = Tf::random(9999) + 1;
  39. World world = World::get(id);
  40. renderJson(world.toVariantMap());
  41. }
  42. void WorldController::entry()
  43. {
  44. renderEntry();
  45. }
  46. void WorldController::create()
  47. {
  48. if (httpRequest().method() != Tf::Post) {
  49. return;
  50. }
  51. QVariantMap form = httpRequest().formItems("world");
  52. World world = World::create(form);
  53. if (!world.isNull()) {
  54. QString notice = "Created successfully.";
  55. tflash(notice);
  56. redirect(urla("show", world.id()));
  57. } else {
  58. QString error = "Failed to create.";
  59. texport(error);
  60. renderEntry(form);
  61. }
  62. }
  63. void WorldController::renderEntry(const QVariantMap &world)
  64. {
  65. texport(world);
  66. render("entry");
  67. }
  68. void WorldController::edit(const QString &pk)
  69. {
  70. World world = World::get(pk.toUInt());
  71. if (!world.isNull()) {
  72. renderEdit(world.toVariantMap());
  73. } else {
  74. redirect(urla("entry"));
  75. }
  76. }
  77. void WorldController::save(const QString &pk)
  78. {
  79. if (httpRequest().method() != Tf::Post) {
  80. return;
  81. }
  82. QString error;
  83. World world = World::get(pk.toUInt());
  84. if (world.isNull()) {
  85. error = "Original data not found. It may have been updated/removed by another transaction.";
  86. tflash(error);
  87. redirect(urla("edit", pk));
  88. return;
  89. }
  90. QVariantMap form = httpRequest().formItems("world");
  91. world.setProperties(form);
  92. if (world.save()) {
  93. QString notice = "Updated successfully.";
  94. tflash(notice);
  95. redirect(urla("show", pk));
  96. } else {
  97. error = "Failed to update.";
  98. texport(error);
  99. renderEdit(form);
  100. }
  101. }
  102. void WorldController::renderEdit(const QVariantMap &world)
  103. {
  104. texport(world);
  105. render("edit");
  106. }
  107. void WorldController::updates()
  108. {
  109. updates("1");
  110. }
  111. void WorldController::updates(const QString &num)
  112. {
  113. QVariantList worlds;
  114. int d = qMin(qMax(num.toInt(), 1), 500);
  115. World world;
  116. for (int i = 0; i < d; ++i) {
  117. int id = Tf::random(9999) + 1;
  118. world = World::get(id);
  119. world.setRandomNumber( Tf::random(9999) + 1 );
  120. world.update();
  121. worlds << world.toVariantMap();
  122. }
  123. renderJson(worlds);
  124. }
  125. void WorldController::remove(const QString &pk)
  126. {
  127. if (httpRequest().method() != Tf::Post) {
  128. return;
  129. }
  130. World world = World::get(pk.toUInt());
  131. world.remove();
  132. redirect(urla("index"));
  133. }
  134. // Don't remove below this line
  135. T_REGISTER_CONTROLLER(worldcontroller)