worldcontroller.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(const QString &num)
  23. {
  24. QList<QVariantMap> worlds;
  25. int d = num.toInt();
  26. for (int i = 0; i < d; ++i) {
  27. int id = Tf::random(9999) + 1;
  28. World world = World::get(id);
  29. worlds << world.toVariantMap();
  30. }
  31. setContentType("application/json");
  32. renderText(jsonEncode(worlds), false);
  33. }
  34. void WorldController::random()
  35. {
  36. int id = Tf::random(9999) + 1;
  37. World world = World::get(id);
  38. setContentType("application/json");
  39. renderText(jsonEncode(world.toVariantMap()), false);
  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(const QString &num)
  107. {
  108. QList<QVariantMap> worlds;
  109. int d = num.toInt();
  110. for (int i = 0; i < d; ++i) {
  111. int id = Tf::random(9999) + 1;
  112. World world = World::get(id);
  113. world.setRandomnumber( Tf::random(9999) + 1 );
  114. world.update();
  115. worlds << world.toVariantMap();
  116. }
  117. setContentType("application/json");
  118. renderText(jsonEncode(worlds), false);
  119. }
  120. void WorldController::remove(const QString &pk)
  121. {
  122. if (httpRequest().method() != Tf::Post) {
  123. return;
  124. }
  125. World world = World::get(pk.toUInt());
  126. world.remove();
  127. redirect(urla("index"));
  128. }
  129. // Don't remove below this line
  130. T_REGISTER_CONTROLLER(worldcontroller)