Index.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\controllers;
  3. use mako\http\routing\Controller;
  4. use mako\view\ViewFactory;
  5. use mako\http\response\builders\JSON;
  6. use app\models\World;
  7. use app\models\Fortune;
  8. /**
  9. * Welcome controller.
  10. */
  11. class Index extends Controller
  12. {
  13. /**
  14. * plaintext route.
  15. *
  16. * @return string
  17. */
  18. public function plaintext(): string
  19. {
  20. $this->response->setType('text/plain');
  21. return 'Hello, World!';
  22. }
  23. /**
  24. * json route.
  25. *
  26. * @return string
  27. */
  28. public function json(): JSON
  29. {
  30. return $this->jsonResponse(['message' => 'Hello, World!']);
  31. }
  32. /**
  33. * db route.
  34. *
  35. * @return string
  36. */
  37. public function db(): JSON
  38. {
  39. $id = mt_rand(1, 10000);
  40. $World = new World;
  41. $world = $World->get($id);
  42. // $world = [
  43. // 'id' => $World->id,
  44. // 'randomNumber' => $World->randomNumber
  45. // ];
  46. return $this->jsonResponse($world);
  47. }
  48. /**
  49. * queries route.
  50. *
  51. * @return string
  52. */
  53. public function queries(): JSON
  54. {
  55. $queries = $this->request->getQuery()->get('queries');
  56. if (is_numeric($queries)) {
  57. $queries = max(1, min($queries, 500));
  58. } else {
  59. $queries = 1;
  60. }
  61. $worlds = [];
  62. $World = new World;
  63. for ($i = 0; $i < $queries; ++$i) {
  64. $id = mt_rand(1, 10000);
  65. $world = $World->get($id);
  66. $worlds[] = $world;
  67. }
  68. return $this->jsonResponse($worlds);
  69. }
  70. /**
  71. * fortunes route.
  72. *
  73. * @param \mako\view\ViewFactory $view View factory
  74. * @return string
  75. */
  76. public function fortunes(ViewFactory $view): string
  77. {
  78. $Fortune = new Fortune;
  79. $fortunes = $Fortune->all()->getItems();
  80. $fortunes[] = (object) [ 'id' => 0, 'message' => 'Additional fortune added at request time.' ];
  81. usort($fortunes, function ($left, $right) {
  82. return $left->message <=> $right->message;
  83. });
  84. return $view->render('fortunes', [ 'fortunes' => $fortunes ]);
  85. }
  86. /**
  87. * update route.
  88. */
  89. public function update(): JSON
  90. {
  91. $queries = $this->request->getQuery()->get('queries');
  92. if (is_numeric($queries)) {
  93. $queries = max(1, min($queries, 500));
  94. } else {
  95. $queries = 1;
  96. }
  97. $worlds = [];
  98. $World = new World;
  99. for ($i = 0; $i < $queries; ++$i) {
  100. $id = mt_rand(1, 10000);
  101. $random_number = mt_rand(1, 10000);
  102. $world = $World->get($id);
  103. $world->randomNumber = $random_number;
  104. $world->save();
  105. $worlds[] = $world;
  106. }
  107. return $this->jsonResponse($worlds);
  108. }
  109. }