Skip to content

Commit 33d4b38

Browse files
committed
Cleanup
1 parent b1a95c1 commit 33d4b38

3 files changed

Lines changed: 52 additions & 51 deletions

File tree

app/Http/Controllers/Controller.php

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,73 @@ public function tableGet_($tableName)
3232
return TableController::tableGet($tableName);
3333
}
3434

35-
public function rowGet_($tableName,$id)
36-
{
37-
return RowController::rowGet($tableName,$id);
38-
}
39-
4035
public function tableRemove_($tableName)
4136
{
4237
TableController::tableRemove($tableName);
4338

4439
return TableController::tableGet(false);
4540
}
4641

47-
public function rowRemove_($tableName,$id)
48-
{
49-
RowController::rowRemove($tableName,$id);
50-
51-
return TableController::tableGet($tableName);
52-
}
53-
5442
public function tableStore_()
5543
{
5644
return TableController::tableStore();
5745
}
5846

59-
public function rowStore_($tableName)
60-
{
61-
if (!TableController::tableExists($tableName))
62-
return abort(404, "Table '$tableName' don´t exist.");
63-
64-
return RowController::rowStore($tableName);
65-
}
66-
6747
public function tableUpdate_($tableName, Request $request)
6848
{
6949
if (!$request->has('columns'))
7050
return abort(400, "No columns given.");
7151

72-
$tableinfoNew = [ 'tablename' => $request->input('tablename'), 'originalTablename' => $tableName ];
73-
74-
$tableinfoNew['columns'][] = array('originalName' => 'id', 'originalType' => "increments");
52+
$tableinfoNew = [
53+
'tablename' => $request->input('tablename'),
54+
'originalTablename' => $tableName,
55+
'columns' => [
56+
0 => [ 'originalName' => 'id', 'originalType' => 'increments' ]
57+
]
58+
];
7559

7660
foreach ($request->input('columns') as $columnNew){
7761

78-
if ($columnNew['originalName'] != "id") // Don´t use these
79-
if (strtoupper($columnNew['originalType'])=="TEXT" || strtoupper($columnNew['originalType'])=="INTEGER") // Enabled types
62+
if ($columnNew['originalName'] != "id")
63+
if (strtoupper($columnNew['originalType'])=="TEXT" || strtoupper($columnNew['originalType'])=="INTEGER")
8064
$tableinfoNew['columns'][] = $columnNew;
8165

8266
}
8367

8468
return TableController::tableUpdate($tableName,$tableinfoNew);
8569
}
8670

71+
72+
public function rowGet_($tableName,$id)
73+
{
74+
return RowController::rowGet($tableName,$id);
75+
}
76+
77+
public function rowRemove_($tableName,$id)
78+
{
79+
RowController::rowRemove($tableName,$id);
80+
81+
return TableController::tableGet($tableName);
82+
}
83+
84+
public function rowStore_($tableName)
85+
{
86+
if (!TableController::tableExists($tableName))
87+
return abort(404, "Table '$tableName' don´t exist.");
88+
89+
return RowController::rowStore($tableName);
90+
}
91+
8792
public function rowUpdate_($tableName, $id, Request $request)
8893
{
8994
if (!TableController::tableExists($tableName))
9095
return abort(404, "Table '$tableName' don´t exist.");
9196

92-
$row = array();
93-
$row['id'] = $id;
97+
$row = [ 'id' => $id ];
9498

9599
foreach ($request->input() as $column => $value) {
96100

97-
if ($column != "id") // Don´t use these
101+
if ($column != "id")
98102
$row[$column] = $value;
99103

100104
}

app/Http/Controllers/RowController.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ public static function rowRemove($tableName,$id)
4848
}
4949

5050
/**
51-
* Create row.
51+
* Create row in table with the given name.
5252
*
5353
* @param string $tableName
54-
* @param object $row
5554
* @return array
5655
*/
5756
public static function rowStore($tableName)
@@ -63,7 +62,7 @@ public static function rowStore($tableName)
6362

6463
foreach ($structure['columns'] as $column)
6564
if ($column['originalName'] != 'id')
66-
$insert[ $column['originalName'] ]= ' ' ;
65+
$insert[ $column['originalName'] ] = '0' ;
6766

6867

6968
$id = DB::table($tableName)->insertGetId($insert);
@@ -72,17 +71,17 @@ public static function rowStore($tableName)
7271
}
7372

7473
/**
75-
* Update row.
74+
* Update row in table with the given name.
7675
*
7776
* @param string $tableName
7877
* @param object $row
7978
* @return array
8079
*/
8180
public static function rowUpdate($tableName,$row)
8281
{
83-
$update = array();
82+
$update = [];
8483

85-
// Only use existing columns
84+
// Get existing columns
8685
$structure = TableController::tableInfo($tableName);
8786

8887
foreach ($structure['columns'] as $column){

app/Http/Controllers/TableController.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,26 @@ public static function tableStore()
112112
* Update table.
113113
*
114114
* @param string $tableName
115-
* @param object $structure
115+
* @param object $structureNew
116116
* @return array
117117
*/
118-
public static function tableUpdate($tableName,$tableInfoNew)
118+
public static function tableUpdate($tableName,$structureNew)
119119
{
120120
if (!TableController::tableExists($tableName))
121121
return TableController::tableStore();
122122

123123
// Get Current table
124-
$tableInfoOld = TableController::tableInfo($tableName);
125-
126-
$structureOld = $tableInfoOld['columns'];
127-
128-
// Get New table
129-
$structureNew = $tableInfoNew['columns'];
130-
124+
$structureOld = TableController::tableInfo($tableName);
125+
131126

132127
// Check for new columns
133128
$addThese = array();
134129

135-
foreach ($structureNew as $columnNew){
130+
foreach ($structureNew['columns'] as $columnNew){
136131

137132
$add = true;
138133

139-
foreach ($structureOld as $columnOld) {
134+
foreach ($structureOld['columns'] as $columnOld) {
140135

141136
// Column exists
142137
if ($columnOld['originalName'] == $columnNew['originalName'])
@@ -157,14 +152,15 @@ public static function tableUpdate($tableName,$tableInfoNew)
157152

158153
});
159154

155+
160156
// Check existing columns
161157
$dropThese = array();
162158

163-
foreach ($structureOld as $columnOld) {
159+
foreach ($structureOld['columns'] as $columnOld) {
164160

165161
$drop = true;
166162

167-
foreach ($structureNew as $columnNew){
163+
foreach ($structureNew['columns'] as $columnNew){
168164

169165
// Column exists
170166
if ($columnOld['originalName'] == $columnNew['originalName'])
@@ -184,8 +180,9 @@ public static function tableUpdate($tableName,$tableInfoNew)
184180

185181
});
186182

183+
187184
// Check for renamed columns
188-
foreach ($structureNew as $columnNew){
185+
foreach ($structureNew['columns'] as $columnNew){
189186

190187
// Column renamed
191188
if (isset($columnNew['name']) && $columnNew['originalName'] != $columnNew['name']){
@@ -200,12 +197,13 @@ public static function tableUpdate($tableName,$tableInfoNew)
200197

201198
}
202199

200+
203201
// Check for rename table
204-
if ($tableInfoNew['originalTablename'] != $tableInfoNew['tablename'])
205-
Schema::rename($tableInfoNew['originalTablename'], $tableInfoNew['tablename']);
202+
if ($structureNew['originalTablename'] != $structureNew['tablename'])
203+
Schema::rename($structureNew['originalTablename'], $structureNew['tablename']);
206204

207205

208-
return TableController::tableGet($tableInfoNew['tablename']);
206+
return TableController::tableGet($structureNew['tablename']);
209207
}
210208

211209
}

0 commit comments

Comments
 (0)