Skip to content

Commit

Permalink
transaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Feb 4, 2024
1 parent bb23c5f commit 58f62c3
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 19 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/alert.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="alert alert--{{ $type }}" role="alert">{{ $slot }}</div>
<div class="alert {{ $class }}" role="alert">{{ $slot }}</div>
10 changes: 5 additions & 5 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public function isConfirmable(): bool
public function handleFormRequest(Request $request, Model $model): void
{
$this->validateFormRequest($request, $model);

$this->handle(
$request,
$request->boolean('all') ? $this->getQuery()->get() : $this->getQuery()->findMany($request->input('models', []))
);
}

/**
Expand All @@ -179,11 +184,6 @@ public function perform(Request $request): Response
{
$this->handleFormRequest($request, $this->getQuery()->getModel());

$this->handle(
$request,
$request->boolean('all') ? $this->getQuery()->get() : $this->getQuery()->findMany($request->input('models', []))
);

return Redirect::back()->with(
sprintf('alerts.action-%s', $this->getKey()),
Alert::info(__(':action was successful!', ['action' => $this->getName()]))
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/SaveFormDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cone\Root\Exceptions;

use Exception;

class SaveFormDataException extends Exception
{
//
}
27 changes: 21 additions & 6 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Filters\Search;
Expand All @@ -24,9 +25,11 @@
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;
use Throwable;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\Relation
Expand Down Expand Up @@ -557,14 +560,26 @@ public function handleFormRequest(Request $request, Model $model): void
{
$this->validateFormRequest($request, $model);

$this->resolveFields($request)
->authorized($request, $model)
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);
try {
DB::beginTransaction();

$model->save();
$this->resolveFields($request)
->authorized($request, $model)
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);

$model->save();

$this->saved($request, $model);

$this->saved($request, $model);
DB::commit();
} catch (Throwable $exception) {
report($exception);

DB::rollback();

throw new SaveFormDataException($exception->getMessage());
}
}

/**
Expand Down
27 changes: 21 additions & 6 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Root\Resources;

use Cone\Root\Actions\Action;
use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Fields\Field;
use Cone\Root\Fields\Relation;
use Cone\Root\Filters\Filter;
Expand Down Expand Up @@ -31,8 +32,10 @@
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use Throwable;

abstract class Resource implements Arrayable, Form
{
Expand Down Expand Up @@ -416,14 +419,26 @@ public function handleFormRequest(Request $request, Model $model): void
{
$this->validateFormRequest($request, $model);

$this->resolveFields($request)
->authorized($request, $model)
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);
try {
DB::beginTransaction();

$model->save();
$this->resolveFields($request)
->authorized($request, $model)
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);

$model->save();

$this->saved($request, $model);

$this->saved($request, $model);
DB::commit();
} catch (Throwable $exception) {
report($exception);

DB::rollBack();

throw new SaveFormDataException($exception->getMessage());
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/RootServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace Cone\Root;

use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Resources\Resource;
use Cone\Root\Support\Alert;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\View;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\ServiceProvider;

class RootServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -75,6 +80,14 @@ public function boot(): void

$this->registerViews();
$this->registerRoutes();

$this->app->make(ExceptionHandler::class)->renderable(
static function (SaveFormDataException $exception): RedirectResponse {
return Redirect::back()
->withInput()
->with('alerts.form-save', Alert::error($exception->getMessage()));
}
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ public function toJson($options = 0): string
*/
public function __toString(): string
{
return $this->toJson();
return $this->message;
}
}
4 changes: 4 additions & 0 deletions src/View/Components/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function render(): View
return $this->view('root::components.alert', [
'type' => $this->type,
'closable' => $this->closable,
'class' => match ($this->type) {
'error' => 'alert--danger',
default => 'alert--'.$this->type,
},
]);
}
}

0 comments on commit 58f62c3

Please sign in to comment.