DatabaseController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\modules\site\controllers;
  3. use Psr\Http\Message\ResponseInterface;
  4. use PDO;
  5. use app\modules\site\models\World;
  6. use app\modules\site\models\Fortune;
  7. class DatabaseController extends \Piko\Controller
  8. {
  9. public function __construct(private PDO $db)
  10. {
  11. }
  12. /**
  13. * Returns as Json response a random record form the World table
  14. *
  15. * @return ResponseInterface
  16. */
  17. public function queryAction(): ResponseInterface
  18. {
  19. $world = new World($this->db);
  20. $id = mt_rand(1, 10000);
  21. $world->load($id);
  22. return $this->jsonResponse($world->toArray());
  23. }
  24. /**
  25. * Return as Json response a set of random records form the World table
  26. *
  27. * @param string $queries The number of queries requested to fetch records
  28. *
  29. * @return ResponseInterface
  30. */
  31. public function queriesAction(string $queries = ''): ResponseInterface
  32. {
  33. // Bound and sanitize $queries parameter
  34. $queries = is_numeric($queries) ? (int) $queries : 1;
  35. if ($queries < 1) {
  36. $queries = 1;
  37. } elseif ($queries > 500) {
  38. $queries = 500;
  39. }
  40. $rows = [];
  41. $world = new World($this->db);
  42. while ($queries) {
  43. $id = mt_rand(1, 10000);
  44. $rows[] = $world->load($id)->toArray();
  45. $queries--;
  46. }
  47. return $this->jsonResponse($rows);
  48. }
  49. /**
  50. * Retrieves, sorts, and renders the list of fortunes.
  51. *
  52. * Fetches all fortunes from the database, adds an additional fortune at request time,
  53. * sorts the list of fortunes by message text, and renders the result using the "fortunes" view.
  54. *
  55. * @return string The rendered Fortune page
  56. */
  57. public function fortunesAction(): string
  58. {
  59. $st = $this->db->prepare('SELECT * FROM fortune');
  60. $st->execute();
  61. $rows = $st->fetchAll(PDO::FETCH_CLASS, Fortune::class, [$this->db]);
  62. // Add a new record without saving it in the db
  63. $fortune = new Fortune($this->db);
  64. $fortune->message = 'Additional fortune added at request time.';
  65. $rows[] = $fortune;
  66. // Sort messages
  67. usort($rows, function($a, $b) {
  68. return strcmp($a->message, $b->message);
  69. });
  70. return $this->render('fortunes', [
  71. 'fortunes' => $rows
  72. ]);
  73. }
  74. /**
  75. * Updates a set of random records in the World table and returns them as a Json response.
  76. *
  77. * Fetches the specified number of World records,
  78. * assigns each a new randomNumber, saves, and returns the modified records.
  79. *
  80. * @param string $queries The number of records to update and return
  81. *
  82. * @return ResponseInterface
  83. */
  84. public function updatesAction(string $queries = ''): ResponseInterface
  85. {
  86. // Bound and sanitize $queries parameter
  87. $queries = is_numeric($queries) ? (int) $queries : 1;
  88. if ($queries < 1) {
  89. $queries = 1;
  90. } elseif ($queries > 500) {
  91. $queries = 500;
  92. }
  93. $rows = [];
  94. $world = new World($this->db);
  95. while ($queries) {
  96. $id = mt_rand(1, 10000);
  97. $world = $world->load($id);
  98. $world->randomnumber = mt_rand(1, 10000);
  99. $world->save();
  100. $rows[] = $world->toArray();
  101. $queries--;
  102. }
  103. return $this->jsonResponse($rows);
  104. }
  105. }