add table viewing page
This commit is contained in:
@@ -12,6 +12,20 @@ use MightyPork\Exceptions\NotApplicableException;
|
||||
|
||||
class TableController extends Controller
|
||||
{
|
||||
public function view(User $user, string $table)
|
||||
{
|
||||
/** @var Table $tableModel */
|
||||
$tableModel = $user->tables()->where('name', $table)->first();
|
||||
$revision = $tableModel->activeRevision;
|
||||
|
||||
return view('table.view', [
|
||||
'table' => $tableModel,
|
||||
'revision' => $revision,
|
||||
'columns' => Column::columnsFromJson(json_decode($revision->columns)),
|
||||
'rows' => $revision->rows,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* SHow a form for creating a new table
|
||||
*
|
||||
@@ -20,8 +34,8 @@ class TableController extends Controller
|
||||
public function create()
|
||||
{
|
||||
$exampleColumns =
|
||||
"latin,string,Latin Name\n".
|
||||
"common,string,Common Name\n".
|
||||
"latin,string,Latin Name\n" .
|
||||
"common,string,Common Name\n" .
|
||||
"lifespan,int,Lifespan (years)";
|
||||
|
||||
$exampleData =
|
||||
@@ -82,11 +96,12 @@ class TableController extends Controller
|
||||
|
||||
$rowTable = array_map('str_getcsv', explode("\n", $request->get('data')));
|
||||
|
||||
// Preparing data to insert into the Rows table
|
||||
$rowsData = null;
|
||||
try {
|
||||
$rowsData = array_map(function ($row) use ($columns) {
|
||||
if (count($row) != count($columns)) {
|
||||
throw new NotApplicableException("All rows must have ".count($columns)." fields.");
|
||||
throw new NotApplicableException("All rows must have " . count($columns) . " fields.");
|
||||
}
|
||||
$parsed = [];
|
||||
foreach ($row as $i => $val) {
|
||||
@@ -98,7 +113,7 @@ class TableController extends Controller
|
||||
'refs' => 1,
|
||||
];
|
||||
}, $rowTable);
|
||||
}catch (\Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
return $this->backWithErrors(['columns' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
@@ -118,6 +133,11 @@ class TableController extends Controller
|
||||
'origin' => $request->get('origin'),
|
||||
]);
|
||||
|
||||
// Attach the revision to the table, set as current
|
||||
$table->revisions()->attach($revision);
|
||||
$table->activeRevision()->associate($revision);
|
||||
|
||||
// Spawn the rows, linked to the revision
|
||||
$revision->rows()->createMany($rowsData);
|
||||
|
||||
// Now we create rows, a revision pointing to them, and the table using it.
|
||||
|
||||
Reference in New Issue
Block a user