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.0KB

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