add some validation to row edit, better handle setting null cols with empty

This commit is contained in:
2018-08-05 18:45:11 +02:00
parent 06fb01d146
commit 6cfd5aa7e9
13 changed files with 164 additions and 90 deletions
+2 -1
View File
@@ -6,6 +6,7 @@ use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use MightyPork\Exceptions\Exceptions\SimpleValidationException;
class Handler extends ExceptionHandler
{
@@ -15,7 +16,7 @@ class Handler extends ExceptionHandler
* @var array
*/
protected $dontReport = [
//
SimpleValidationException::class,
];
/**
-14
View File
@@ -1,14 +0,0 @@
<?php
namespace FlowBox\Exceptions;
class ViewException extends FBRuntimeException
{
public $captured;
public function __construct(\Exception $cause, $captured)
{
$this->captured = $captured;
parent::__construct($cause);
}
}
+40 -29
View File
@@ -3,14 +3,12 @@
namespace App\Http\Controllers;
use App\Models\Row;
use App\Models\Table;
use App\Models\User;
use App\Tables\Changeset;
use App\Tables\Column;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use MightyPork\Exceptions\SimpleValidationException;
class TableEditController extends Controller
{
@@ -22,14 +20,12 @@ class TableEditController extends Controller
*/
private function getChangeset(Table $table)
{
$session_key = "proposal_{$table->id}";
if (Input::has('reset')) {
session()->forget($session_key);
session()->forget($table->draftSessionKey);
}
/** @var Changeset $changeset */
return session()->remember($session_key, function () use ($table) {
return session()->remember($table->draftSessionKey, function () use ($table) {
$changeset = new Changeset();
$changeset->table = $table;
$changeset->revision = $table->revision;
@@ -39,10 +35,21 @@ class TableEditController extends Controller
private function storeChangeset(Changeset $chs)
{
$session_key = "proposal_{$chs->table->id}";
session()->put($chs->table->draftSessionKey, $chs);
}
session()->put($session_key, $chs);
session()->save();
/**
* Discard draft and redirect to table view
*/
public function discard(User $user, string $table)
{
/** @var Table $tableModel */
$tableModel = $user->tables()->where('name', $table)->first();
if ($tableModel === null) abort(404, "No such table.");
session()->forget($tableModel->draftSessionKey);
return redirect($tableModel->viewRoute);
}
public function draft(User $user, string $table, $tab = null)
@@ -103,29 +110,33 @@ class TableEditController extends Controller
$input = objBag($request->all(), false);
$code = 200;
switch ($input->action) {
case 'row.update':
$data = (object)$input->data;
$changeset->rowUpdate($data);
try {
$code = 200;
switch ($input->action) {
case 'row.update':
$data = (object)$input->data;
$changeset->rowUpdate($data);
$resp = $changeset->fetchAndTransformRow($data->_id);
break;
$resp = $changeset->fetchAndTransformRow($data->_id);
break;
case 'row.remove':
$changeset->rowRemove($input->id);
$resp = $changeset->fetchAndTransformRow($input->id);
break;
case 'row.remove':
$changeset->rowRemove($input->id);
$resp = $changeset->fetchAndTransformRow($input->id);
break;
case 'row.restore':
$changeset->rowRestore($input->id);
$resp = $changeset->fetchAndTransformRow($input->id);
break;
case 'row.restore':
$changeset->rowRestore($input->id);
$resp = $changeset->fetchAndTransformRow($input->id);
break;
default:
$resp = "Bad Action";
$code = 400;
break;
default:
$resp = "Bad Action";
$code = 400;
break;
}
} catch (SimpleValidationException $e) {
return $this->jsonResponse(['errors' => $e->getMessageBag()->getMessages()], 400);
}
$this->storeChangeset($changeset);
+7
View File
@@ -25,6 +25,9 @@ use Illuminate\Database\Eloquent\Collection;
* @property-read string $draftRoute
* @property-read string $settingsRoute
* @property-read string $deleteRoute
* @property-read string $draftSessionKey
* @property-read string $draftDiscardRoute
* @property-read string $draftUpdateRoute
* @property-read User $owner
* @property-read Table $parentTable
* @property-read Table[]|Collection $forks
@@ -136,9 +139,13 @@ class Table extends BaseModel
case 'settingsRoute': return route('table.conf', $arg);
case 'draftRoute': return route('table.draft', $arg);
case 'deleteRoute': return route('table.delete', $arg);
case 'draftDiscardRoute': return route('table.draft-discard', $arg);
case 'draftUpdateRoute': return route('table.draft-update', $arg);
}
}
if ($name == 'draftSessionKey') return "proposal.{$this->id}";
return parent::__get($name);
}
+6 -2
View File
@@ -325,8 +325,12 @@ class Changeset
foreach ($newVals as $colId => $value) {
if (starts_with($colId, '_')) continue; // internals
if (!isset($origRow->$colId) || $value != $origRow->$colId) {
$updateObj[$colId] = $cols[$colId]->cast($value);
$col = $cols[$colId];
$value = $col->cast($value);
$origValue = $col->cast(isset($origRow->$colId) ? $origRow->$colId : null);
if ($value !== $origValue) {
$updateObj[$colId] = $value;
}
}
+3 -2
View File
@@ -4,6 +4,7 @@ namespace App\Tables;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;
use MightyPork\Exceptions\SimpleValidationException;
use MightyPork\Exceptions\NotApplicableException;
use MightyPork\Utils\Utils;
@@ -172,13 +173,13 @@ class Column implements JsonSerializable, Arrayable
if (is_int($value)) return $value;
if (is_float($value)) return round($value);
if (is_numeric($value)) return intval($value);
throw new NotApplicableException("Could not convert value \"$value\" to int!");
throw new SimpleValidationException($this->id, "Could not convert value \"$value\" to int!");
case 'float':
if (is_null($value)) return 0;
if (is_int($value) || is_float($value)) return (float)$value;
if (is_numeric($value)) return floatval($value);
throw new NotApplicableException("Could not convert value \"$value\" to float!");
throw new SimpleValidationException($this->id, "Could not convert value \"$value\" to float!");
case 'bool':
if (is_null($value)) return false;