api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColumnController.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Controller;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Tqdev\PhpCrudApi\Column\DefinitionService;
  6. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  8. use Tqdev\PhpCrudApi\Record\ErrorCode;
  9. use Tqdev\PhpCrudApi\RequestUtils;
  10. class ColumnController
  11. {
  12. private $responder;
  13. private $reflection;
  14. private $definition;
  15. public function __construct(Router $router, Responder $responder, ReflectionService $reflection, DefinitionService $definition)
  16. {
  17. $router->register('GET', '/columns', array($this, 'getDatabase'));
  18. $router->register('GET', '/columns/*', array($this, 'getTable'));
  19. $router->register('GET', '/columns/*/*', array($this, 'getColumn'));
  20. $router->register('PUT', '/columns/*', array($this, 'updateTable'));
  21. $router->register('PUT', '/columns/*/*', array($this, 'updateColumn'));
  22. $router->register('POST', '/columns', array($this, 'addTable'));
  23. $router->register('POST', '/columns/*', array($this, 'addColumn'));
  24. $router->register('DELETE', '/columns/*', array($this, 'removeTable'));
  25. $router->register('DELETE', '/columns/*/*', array($this, 'removeColumn'));
  26. $this->responder = $responder;
  27. $this->reflection = $reflection;
  28. $this->definition = $definition;
  29. }
  30. public function getDatabase(ServerRequestInterface $request): ResponseInterface
  31. {
  32. $tables = [];
  33. foreach ($this->reflection->getTableNames() as $table) {
  34. $tables[] = $this->reflection->getTable($table);
  35. }
  36. $database = ['tables' => $tables];
  37. return $this->responder->success($database);
  38. }
  39. public function getTable(ServerRequestInterface $request): ResponseInterface
  40. {
  41. $tableName = RequestUtils::getPathSegment($request, 2);
  42. if (!$this->reflection->hasTable($tableName)) {
  43. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  44. }
  45. $table = $this->reflection->getTable($tableName);
  46. return $this->responder->success($table);
  47. }
  48. public function getColumn(ServerRequestInterface $request): ResponseInterface
  49. {
  50. $tableName = RequestUtils::getPathSegment($request, 2);
  51. $columnName = RequestUtils::getPathSegment($request, 3);
  52. if (!$this->reflection->hasTable($tableName)) {
  53. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  54. }
  55. $table = $this->reflection->getTable($tableName);
  56. if (!$table->hasColumn($columnName)) {
  57. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  58. }
  59. $column = $table->getColumn($columnName);
  60. return $this->responder->success($column);
  61. }
  62. public function updateTable(ServerRequestInterface $request): ResponseInterface
  63. {
  64. $tableName = RequestUtils::getPathSegment($request, 2);
  65. if (!$this->reflection->hasTable($tableName)) {
  66. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  67. }
  68. $success = $this->definition->updateTable($tableName, $request->getParsedBody());
  69. if ($success) {
  70. $this->reflection->refreshTables();
  71. }
  72. return $this->responder->success($success);
  73. }
  74. public function updateColumn(ServerRequestInterface $request): ResponseInterface
  75. {
  76. $tableName = RequestUtils::getPathSegment($request, 2);
  77. $columnName = RequestUtils::getPathSegment($request, 3);
  78. if (!$this->reflection->hasTable($tableName)) {
  79. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  80. }
  81. $table = $this->reflection->getTable($tableName);
  82. if (!$table->hasColumn($columnName)) {
  83. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  84. }
  85. $success = $this->definition->updateColumn($tableName, $columnName, $request->getParsedBody());
  86. if ($success) {
  87. $this->reflection->refreshTable($tableName);
  88. }
  89. return $this->responder->success($success);
  90. }
  91. public function addTable(ServerRequestInterface $request): ResponseInterface
  92. {
  93. $tableName = $request->getParsedBody()->name;
  94. if ($this->reflection->hasTable($tableName)) {
  95. return $this->responder->error(ErrorCode::TABLE_ALREADY_EXISTS, $tableName);
  96. }
  97. $success = $this->definition->addTable($request->getParsedBody());
  98. if ($success) {
  99. $this->reflection->refreshTables();
  100. }
  101. return $this->responder->success($success);
  102. }
  103. public function addColumn(ServerRequestInterface $request): ResponseInterface
  104. {
  105. $tableName = RequestUtils::getPathSegment($request, 2);
  106. if (!$this->reflection->hasTable($tableName)) {
  107. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  108. }
  109. $columnName = $request->getParsedBody()->name;
  110. $table = $this->reflection->getTable($tableName);
  111. if ($table->hasColumn($columnName)) {
  112. return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
  113. }
  114. $success = $this->definition->addColumn($tableName, $request->getParsedBody());
  115. if ($success) {
  116. $this->reflection->refreshTable($tableName);
  117. }
  118. return $this->responder->success($success);
  119. }
  120. public function removeTable(ServerRequestInterface $request): ResponseInterface
  121. {
  122. $tableName = RequestUtils::getPathSegment($request, 2);
  123. if (!$this->reflection->hasTable($tableName)) {
  124. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  125. }
  126. $success = $this->definition->removeTable($tableName);
  127. if ($success) {
  128. $this->reflection->refreshTables();
  129. }
  130. return $this->responder->success($success);
  131. }
  132. public function removeColumn(ServerRequestInterface $request): ResponseInterface
  133. {
  134. $tableName = RequestUtils::getPathSegment($request, 2);
  135. $columnName = RequestUtils::getPathSegment($request, 3);
  136. if (!$this->reflection->hasTable($tableName)) {
  137. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  138. }
  139. $table = $this->reflection->getTable($tableName);
  140. if (!$table->hasColumn($columnName)) {
  141. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  142. }
  143. $success = $this->definition->removeColumn($tableName, $columnName);
  144. if ($success) {
  145. $this->reflection->refreshTable($tableName);
  146. }
  147. return $this->responder->success($success);
  148. }
  149. }