Skip to content

Commit 499ff2b

Browse files
committed
Cleanup
1 parent 94df570 commit 499ff2b

5 files changed

Lines changed: 78 additions & 374 deletions

File tree

app/Http/Controllers/Controller.php

Lines changed: 12 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -8,211 +8,35 @@
88

99
class Controller extends BaseController
1010
{
11-
/**
12-
* Retrieve all tables.
13-
*
14-
* @return Response
15-
*/
16-
public function t_showAll()
17-
{
18-
return TableController::table_showAll();
19-
}
2011

21-
/**
22-
* Retrieve all rows in table with the given name.
23-
*
24-
* @param string $tableName
25-
* @return Response
26-
*/
27-
public function t_show($tableName)
12+
public function tableGetAll_()
2813
{
29-
return TableController::table_show($tableName);
14+
return TableController::tableGet(false);
3015
}
3116

32-
/**
33-
* Retrieve info about table with the given name.
34-
*
35-
* @param string $tableName
36-
* @return Response
37-
*/
38-
public function t_showInfo($tableName)
17+
public function tableGet_($tableName)
3918
{
40-
return TableController::table_showInfo($tableName);
19+
return TableController::tableGet($tableName);
4120
}
4221

43-
/**
44-
* Retrieve row from table with the given id.
45-
*
46-
* @param string $tableName
47-
* @param int $id
48-
* @return Response
49-
*/
50-
public function r_show($tableName,$id)
22+
public function rowGet_($tableName,$id)
5123
{
52-
if (!TableController::table_exists($tableName))
53-
return abort(404, "Table '$tableName' don´t exist.");
54-
55-
return RowController::row_show($tableName,$id);
24+
return RowController::rowGet($tableName,$id);
5625
}
5726

58-
/**
59-
* Create/Replace table.
60-
*
61-
* @param Request $request
62-
* @return Response
63-
*/
64-
public function t_store(Request $request)
65-
{
66-
if (!$request->has('tablename'))
67-
return abort(400, "No tablename given.");
68-
69-
$tableName = $request->tablename;
70-
71-
$table = array();
72-
$table['id'] = "increments";
73-
74-
foreach ($request->input() as $column => $type) {
75-
if ($column != "id" && $column != "tablename") // Don´t use these
76-
if ($type=="text" || $type=="integer") // Enabled types
77-
$table[$column] = $type;
78-
}
79-
80-
if (TableController::table_exists($tableName))
81-
return abort(500, "Table '$tableName' allready exists.");
82-
83-
if (!TableController::table_store($tableName,$table))
84-
return abort(500, "Failed to create Table '$tableName'");
8527

86-
// Summary of the created table
87-
echo "<pre>";
88-
echo "CREATE TABLE '$tableName'\n";
89-
echo "(\n";
90-
foreach ($table as $column => $type) {
91-
echo " '$column' $type\n";
92-
}
93-
echo ")</pre>";
94-
95-
return "";
96-
}
97-
98-
/**
99-
* Create/Replace row.
100-
*
101-
* @param string $tableName
102-
* @param Request $request
103-
* @return Response
104-
*/
105-
public function r_store($tableName, Request $request)
28+
public function tableRemove_($tableName)
10629
{
107-
if (!TableController::table_exists($tableName))
108-
return abort(404, "Table '$tableName' don´t exist.");
109-
110-
$row = array();
111-
foreach ($request->input() as $column => $value) {
112-
if ($column != "id") // Don´t use these
113-
$row[$column] = $value;
114-
}
115-
116-
$id = RowController::row_store($tableName,$row);
30+
TableController::tableRemove($tableName);
11731

118-
if (!$id)
119-
return abort(500, "Failed to create row in '$tableName'");
120-
121-
return RowController::row_show($tableName,$id);
32+
return TableController::tableGet(false);
12233
}
12334

124-
/**
125-
* Update table.
126-
*
127-
* @param Request $request
128-
* @return Response
129-
*/
130-
public function t_update(Request $request)
35+
public function rowRemove_($tableName,$id)
13136
{
132-
if (!$request->has('tablename'))
133-
return abort(400, "No tablename given.");
134-
135-
$tableName = $request->tablename;
136-
137-
$table = array();
138-
$table['id'] = "increments";
139-
140-
foreach ($request->input() as $column => $type) {
141-
if ($column != "id" && $column != "tablename") // Don´t use these
142-
if ($type=="text" || $type=="integer") // Enabled types
143-
$table[$column] = $type;
144-
}
145-
146-
if (!TableController::table_update($tableName,$table))
147-
return abort(500, "Failed to update Table '$tableName'");
148-
149-
// Summary of the updated table
150-
echo "<pre>";
151-
echo "CREATE TABLE '$tableName'\n";
152-
echo "(\n";
153-
foreach ($table as $column => $type) {
154-
echo " '$column' $type\n";
155-
}
156-
echo ")</pre>";
157-
158-
return "";
159-
}
160-
161-
/**
162-
* Update row.
163-
*
164-
* @param string $tableName
165-
* @param int $id
166-
* @param Request $request
167-
* @return Response
168-
*/
169-
public function r_update($tableName, $id, Request $request)
170-
{
171-
if (!TableController::table_exists($tableName))
172-
return abort(404, "Table '$tableName' don´t exist.");
173-
174-
$row = array();
175-
foreach ($request->input() as $column => $value) {
176-
if ($column != "id") // Don´t use these
177-
$row[$column] = $value;
178-
}
179-
180-
if (!RowController::row_update($tableName,$id,$row))
181-
return abort(500, "Failed to update row $id in '$tableName'");
182-
183-
return RowController::row_show($tableName,$id);
184-
}
185-
186-
/**
187-
* Remove table.
188-
*
189-
* @param string $tableName
190-
* @return Response
191-
*/
192-
public function t_remove($tableName)
193-
{
194-
if (!TableController::table_remove($tableName))
195-
return abort(500, "Failed to remove Table '$tableName'");
196-
197-
return TableController::table_showAll();
198-
}
199-
200-
/**
201-
* Remove row.
202-
*
203-
* @param string $tableName
204-
* @param int $id
205-
* @return Response
206-
*/
207-
public function r_remove($tableName,$id)
208-
{
209-
if (!TableController::table_exists($tableName))
210-
return abort(404, "Table '$tableName' don´t exist.");
211-
212-
if (!RowController::row_remove($tableName,$id))
213-
return abort(500, "Failed to remove Row $id from '$tableName'");
37+
RowController::rowRemove($tableName,$id);
21438

215-
return TableController::table_show($tableName);
39+
return TableController::tableGet($tableName);
21640
}
21741

21842
}

app/Http/Controllers/DatabaseController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
class DatabaseController extends Controller
66
{
7+
78
/**
89
* Check if database exists.
910
*
1011
* @return ture|false
1112
*/
12-
public static function database_exists()
13+
public static function databaseExists()
1314
{
1415
if (!file_exists( database_path('database.sqlite') ))
1516
return false;
17+
1618
return true;
1719
}
1820

@@ -21,8 +23,9 @@ public static function database_exists()
2123
*
2224
* @return file pointer resource|false
2325
*/
24-
public static function database_create()
26+
public static function databaseCreate()
2527
{
2628
return fopen( database_path('database.sqlite') , "w");
2729
}
28-
}
30+
31+
}

app/Http/Controllers/RowController.php

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,44 @@
77

88
class RowController extends Controller
99
{
10+
1011
/**
11-
* Retrieve row with the given id.
12+
* Retrieve row with the given id from table with the given name.
1213
*
1314
* @param string $tableName
1415
* @param int $id
15-
* @return array|false
16+
* @return array
1617
*/
17-
public static function row_show($tableName,$id)
18+
public static function rowGet($tableName,$id)
1819
{
19-
return DB::table($tableName)->where('id', $id)->get();
20-
}
20+
$result = [];
2121

22-
/**
23-
* Create/Replace row.
24-
*
25-
* @param string $tableName
26-
* @param object $row
27-
* @return id|false
28-
*/
29-
public static function row_store($tableName,$row)
30-
{
31-
// Only use existing columns
32-
$insert = array();
33-
$hasColumns = Schema::getColumnListing($tableName);
34-
foreach ($hasColumns as $hasColumn){
35-
foreach ($row as $column => $value){
36-
if ($column == $hasColumn){
37-
$insert[$column] = $value;
38-
}
39-
}
40-
}
22+
if (TableController::tableExists($tableName)){
4123

42-
return DB::table($tableName)->insertGetId($insert);
43-
}
24+
if ($id)
25+
$result = DB::table($tableName)->where('id', $id)->get();
4426

45-
/**
46-
* Update row.
47-
*
48-
* @param string $tableName
49-
* @param int $id
50-
* @param object $row
51-
* @return true|false
52-
*/
53-
public static function row_update($tableName,$id,$row)
54-
{
55-
// Only use existing columns
56-
$update = array();
57-
$hasColumns = Schema::getColumnListing($tableName);
58-
foreach ($hasColumns as $hasColumn){
59-
foreach ($row as $column => $value){
60-
if ($column == $hasColumn){
61-
$update[$column] = $value;
62-
}
63-
}
27+
else
28+
$result = DB::table($tableName)->get();
6429
}
6530

66-
return DB::table($tableName)->where('id', $id)->update($update);
31+
return $result;
6732
}
6833

69-
7034
/**
71-
* Remove row.
35+
* Remove row with the given id from table with the given name.
7236
*
7337
* @param string $tableName
7438
* @param int $id
7539
* @return true|false
7640
*/
77-
public static function row_remove($tableName,$id)
41+
public static function rowRemove($tableName,$id)
7842
{
79-
return DB::table($tableName)->where('id', $id)->delete();
43+
if (TableController::tableExists($tableName))
44+
if (DB::table($tableName)->where('id', $id)->delete())
45+
return true;
46+
47+
return false;
8048
}
8149

8250
}

0 commit comments

Comments
 (0)