Skip to content

Commit fd426ee

Browse files
committed
Add table create and delte
1 parent 16eb868 commit fd426ee

File tree

18 files changed

+71
-184
lines changed

18 files changed

+71
-184
lines changed

src/Tqdev/PhpCrudAdmin/Admin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Tqdev\PhpCrudAdmin\Controller\ColumnController;
1616
use Tqdev\PhpCrudAdmin\Controller\TableController;
1717
use Tqdev\PhpCrudAdmin\Column\ColumnService;
18+
use Tqdev\PhpCrudAdmin\Column\TableService;
1819

1920
class Admin implements RequestHandlerInterface
2021
{
@@ -40,8 +41,8 @@ public function __construct(Config $config)
4041
new ColumnController($router, $responder, $columns);
4142
break;
4243
case 'tables':
43-
$columns = new ColumnService($api, $definition);
44-
new ColumnController($router, $responder, $columns);
44+
$tables = new TableService($api, $definition);
45+
new TableController($router, $responder, $tables);
4546
break;
4647
}
4748
}

src/Tqdev/PhpCrudAdmin/Client/CrudApi.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public function deleteColumn(string $table, string $column)
4141
return $this->call('DELETE', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), []);
4242
}
4343

44+
public function createTable(array $data)
45+
{
46+
return $this->call('POST', '/columns', [], $data);
47+
}
48+
49+
public function deleteTable(string $table)
50+
{
51+
return $this->call('DELETE', '/columns/' . rawurlencode($table), []);
52+
}
53+
4454
private function call(string $method, string $path, array $args = [], $data = false)
4555
{
4656
$query = rtrim('?' . preg_replace('|%5B[0-9]+%5D|', '', http_build_query($args)), '?');

src/Tqdev/PhpCrudAdmin/Column/TableService.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Tqdev\PhpCrudAdmin\Table;
3+
namespace Tqdev\PhpCrudAdmin\Column;
44

55
use Tqdev\PhpCrudAdmin\Client\CrudApi;
6-
use Tqdev\PhpCrudAdmin\Table\DefinitionService;
6+
use Tqdev\PhpCrudAdmin\Column\DefinitionService;
77
use Tqdev\PhpCrudAdmin\Document\TemplateDocument;
88

99
class TableService
@@ -22,10 +22,29 @@ public function hasTable(string $action): bool
2222
return $this->definition->hasTable($action);
2323
}
2424

25+
private function makeForm(array $table): array
26+
{
27+
$form = array();
28+
foreach ($table as $key => $value) {
29+
$form[$key] = array('value' => $value, 'type' => 'text', 'required' => false);
30+
switch ($key) {
31+
case 'name':
32+
$form[$key]['required'] = true;
33+
break;
34+
}
35+
}
36+
return $form;
37+
}
38+
2539
public function createForm(string $action): TemplateDocument
2640
{
41+
$table = array('name' => '');
42+
43+
$form = $this->makeForm($table);
44+
2745
$variables = array(
2846
'action' => $action,
47+
'form' => $form,
2948
);
3049

3150
return new TemplateDocument('layouts/default', 'table/create', $variables);
@@ -50,7 +69,7 @@ public function deleteForm(string $action, string $name): TemplateDocument
5069

5170
$variables = array(
5271
'action' => $action,
53-
'name' => $table['name'],
72+
'table' => $table['name'],
5473
);
5574

5675
return new TemplateDocument('layouts/default', 'table/delete', $variables);
@@ -62,7 +81,7 @@ public function delete(string $action, string $name): TemplateDocument
6281

6382
$variables = array(
6483
'action' => $action,
65-
'name' => $name,
84+
'table' => $name,
6685
'success' => $success,
6786
);
6887

src/Tqdev/PhpCrudAdmin/Controller/TableController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(Router $router, Responder $responder, TableService $
2929
public function createForm(ServerRequestInterface $request): ResponseInterface
3030
{
3131
$action = RequestUtils::getPathSegment($request, 3);
32-
$result = $this->service->createTable($action);
32+
$result = $this->service->createForm($action);
3333
return $this->responder->success($result);
3434
}
3535

@@ -40,6 +40,7 @@ public function create(ServerRequestInterface $request): ResponseInterface
4040
if ($table === null) {
4141
return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
4242
}
43+
$table['columns'] = [["name" => "id", "type" => "bigint", "pk" => true]];
4344
$result = $this->service->create($action, $table);
4445
return $this->responder->success($result);
4546
}
@@ -51,7 +52,7 @@ public function deleteForm(ServerRequestInterface $request): ResponseInterface
5152
if (!$this->service->hasTable($name, 'read')) {
5253
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $name);
5354
}
54-
$result = $this->service->deleteTable($action, $name);
55+
$result = $this->service->deleteForm($action, $name);
5556
return $this->responder->success($result);
5657
}
5758

templates/column/home.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

templates/column/list.html

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ <h2>{{table}}: list</h2>
33
<table class="table">
44
<thead><tr>
55
<th>name</th>
6-
<th></th>
76
<th>type</th>
87
<th>length</th>
98
<th>precision</th>
@@ -14,19 +13,26 @@ <h2>{{table}}: list</h2>
1413
</tr></thead><tbody>
1514
{{for:column:columns}}
1615
<tr>
17-
<td>{{column.name}}</td>
18-
<td style="border-right: 2px solid #ddd; width: 40px;">
19-
<a class="btn btn-default btn-xs" href="{{base}}/admin/columns/{{table}}/read/{{column.name}}">+</a>
20-
</td>
16+
<td><a href="{{base}}/admin/columns/{{table}}/read/{{column.name}}">{{column.name}}</a></td>
2117
<td>{{column.type}}</td>
2218
<td>{{column.length|or("-")}}</td>
2319
<td>{{column.precision|or("-")}}</td>
2420
<td>{{column.scale|or("-")}}</td>
2521
<td>{{column.nullable|bool("yes","-")}}</td>
2622
<td>{{column.pk|bool("yes","-")}}</td>
27-
<td>{{column.fk|or("-")}}</td>
23+
<td>
24+
{{if:column.fk}}
25+
<a href="{{base}}/admin/columns/{{column.fk}}/list">{{column.fk}}</a>
26+
{{else}}
27+
-
28+
{{endif}}
29+
</td>
2830
</tr>
2931
{{endfor}}
3032
</tbody></table>
3133

32-
<p><a href="{{base}}/admin/columns/{{table}}/create" class="btn btn-primary">Add</a></p>
34+
35+
<p>
36+
<a class="btn btn-primary" href="{{base}}/admin/columns/{{table}}/create">Add column</a>
37+
<a class="btn btn-danger" href="{{base}}/admin/tables/delete/{{table}}">Delete table</a>
38+
</p>

templates/column/read.html

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,65 @@
11
<h2>{{table}}: view</h2>
22
<table class="table">
3-
<thead><tr><th>key</th><th></th><th>value</th></tr></thead>
3+
<thead><tr><th>key</th><th>value</th></tr></thead>
44
<tbody>
55
<tr>
66
<td>
77
name
8-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
9-
&nbsp;
108
</td><td>
119
{{column.name}}
1210
</td>
1311
</tr>
1412
<tr>
1513
<td>
1614
type
17-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
18-
&nbsp;
1915
</td><td>
2016
{{column.type}}
2117
</td>
2218
</tr>
2319
<tr>
2420
<td>
2521
length
26-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
27-
&nbsp;
2822
</td><td>
2923
{{column.length|or("-")}}
3024
</td>
3125
</tr>
3226
<tr>
3327
<td>
3428
precision
35-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
36-
&nbsp;
3729
</td><td>
3830
{{column.precision|or("-")}}
3931
</td>
4032
</tr>
4133
<tr>
4234
<td>
4335
scale
44-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
45-
&nbsp;
4636
</td><td>
4737
{{column.scale|or("-")}}
4838
</td>
4939
</tr>
5040
<tr>
5141
<td>
5242
nullable
53-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
54-
&nbsp;
5543
</td><td>
5644
{{column.nullable|bool("yes","-")}}
5745
</td>
5846
</tr>
5947
<tr>
6048
<td>
6149
pk
62-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
63-
&nbsp;
6450
</td><td>
6551
{{column.pk|bool("yes","-")}}
6652
</td>
6753
</tr>
6854
<tr>
6955
<td>
7056
fk
71-
</td><td style="border-right: 2px solid #ddd; width: 40px;">
57+
</td><td>
7258
{{if:column.fk}}
73-
<a class="btn btn-default btn-xs" href="{{base}}/{{column.fk}}/list"> + </a>
59+
<a href="{{base}}/admin/columns/{{column.fk}}/list">{{column.fk}}</a>
7460
{{else}}
75-
&nbsp;
61+
-
7662
{{endif}}
77-
</td><td>
78-
{{column.fk}}
7963
</td>
8064
</tr>
8165
{{endfor}}

templates/layouts/default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="container">
1111
<div class="row">
1212
<div class="col-md-3">
13-
<h3><a href="{{base}}/">PHP-CRUD-ADMIN</a></h3>
13+
<h3><a href="{{base}}/admin/tables/list">PHP-CRUD-ADMIN</a></h3>
1414
</div>
1515
</div>
1616
<div class="row">

templates/layouts/error.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="container">
1111
<div class="row">
1212
<div class="col-md-3">
13-
<h3><a href="{{base}}/">PHP-CRUD-ADMIN</a></h3>
13+
<h3><a href="{{base}}/admin/tables/list">PHP-CRUD-ADMIN</a></h3>
1414
</div>
1515
</div>
1616
<div class="row">

templates/table/create.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h2>{{table}}: create</h2>
1+
<h2>create table</h2>
22

33
<form class="form-horizontal" method="post">
44
{{for:field:key:form}}
@@ -19,4 +19,4 @@ <h2>{{table}}: create</h2>
1919
</div>
2020
{{endfor}}
2121
<button type="submit" class="btn btn-primary">Save</button>
22-
</form>
22+
</form>

templates/table/created.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<h2>{{table}}: create</h2>
1+
<h2>create table</h2>
22

33
<p>Created {{name}}: {{success}}</p>
44

5-
<p><a href="{{base}}/admin/columns/{{table}}/read/{{name}}" class="btn btn-primary">Ok</a></p>
5+
<p><a href="{{base}}/admin/tables/list" class="btn btn-primary">Ok</a></p>

templates/table/delete.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<h2>{{table}}: delete {{name}}</h2>
1+
<h2>delete table {{table}}</h2>
22

33
<p>The action cannot be undone.</p>
44

55
<form method="post">
6-
<input type="hidden" name="name" value="{{name}}"/>
6+
<input type="hidden" name="name" value="{{table}}"/>
77
<button type="submit" class="btn btn-danger">Delete</button>
8-
<a href="{{base}}/admin/columns/{{table}}/read/{{name}}" class="btn btn-default">Cancel</a>
8+
<a href="{{base}}/admin/columns/{{table}}/list" class="btn btn-default">Cancel</a>
99
</form>

templates/table/deleted.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h2>{{table}}: delete</h2>
22

3-
<p>Deleted with name {{name}}</p>
3+
<p>Deleted with name {{table}}</p>
44

5-
<p><a href="{{base}}/admin/columns/{{table}}/list" class="btn btn-primary">Ok</a></p>
5+
<p><a href="{{base}}/admin/tables/list" class="btn btn-primary">Ok</a></p>

templates/table/home.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

templates/table/list.html

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
<h2>{{table}}: list</h2>
1+
<h2>list tables</h2>
22

33
<table class="table">
44
<thead><tr>
55
<th>name</th>
6-
<th></th>
7-
<th>type</th>
8-
<th>length</th>
9-
<th>precision</th>
10-
<th>scale</th>
11-
<th>nullable</th>
12-
<th>pk</th>
13-
<th>fk</th>
146
</tr></thead><tbody>
15-
{{for:column:columns}}
7+
{{for:table:tables}}
168
<tr>
17-
<td>{{column.name}}</td>
18-
<td style="border-right: 2px solid #ddd; width: 40px;">
19-
<a class="btn btn-default btn-xs" href="{{base}}/admin/columns/{{table}}/read/{{column.name}}">+</a>
20-
</td>
21-
<td>{{column.type}}</td>
22-
<td>{{column.length|or("-")}}</td>
23-
<td>{{column.precision|or("-")}}</td>
24-
<td>{{column.scale|or("-")}}</td>
25-
<td>{{column.nullable|bool("yes","-")}}</td>
26-
<td>{{column.pk|bool("yes","-")}}</td>
27-
<td>{{column.fk|or("-")}}</td>
9+
<td><a href="{{base}}/admin/columns/{{table}}/list">{{table}}</a></td>
2810
</tr>
2911
{{endfor}}
3012
</tbody></table>
3113

32-
<p><a href="{{base}}/admin/columns/{{table}}/create" class="btn btn-primary">Add</a></p>
14+
<p><a href="{{base}}/admin/tables/create" class="btn btn-primary">Add</a></p>

0 commit comments

Comments
 (0)