refactor(common): implement generic actions for controller traits (order, status, star)
This commit is contained in:
parent
c2dd9e79c5
commit
3ac95cc6f8
25 changed files with 716 additions and 133 deletions
66
app/Actions/Common/ChangeModelOrder.php
Normal file
66
app/Actions/Common/ChangeModelOrder.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Common;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use SortDirection;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Common\ChangeModelOrderInput, bool>
|
||||
*/
|
||||
final class ChangeModelOrder implements Action
|
||||
{
|
||||
private const int MAX_ORDER_VALUE = 4294967295;
|
||||
|
||||
/**
|
||||
* @param \App\Actions\Common\ChangeModelOrderInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): bool
|
||||
{
|
||||
Assert::isInstanceOf($input, ChangeModelOrderInput::class);
|
||||
|
||||
$model = $input->model;
|
||||
$goUp = $input->direction === SortDirection::Ascending;
|
||||
$currentOrder = $model->getOrder();
|
||||
|
||||
/** @var (Model&ModelWithOrder)|null $adjacentModel */
|
||||
$adjacentModel = $model::query()
|
||||
->where('order', $goUp ? '<' : '>', $currentOrder)
|
||||
->orderBy('order', $goUp ? SortDirection::Descending : SortDirection::Ascending)
|
||||
->first();
|
||||
|
||||
if ($adjacentModel === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->permuteModelOrders($model, $adjacentModel);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function permuteModelOrders(Model & ModelWithOrder $model1, Model & ModelWithOrder $model2): void
|
||||
{
|
||||
$order1 = $model1->getOrder();
|
||||
$order2 = $model2->getOrder();
|
||||
|
||||
// Deactivate timestamps to avoid updating updated_at
|
||||
$model1->timestamps = false;
|
||||
$model2->timestamps = false;
|
||||
|
||||
$model1->setOrder(self::MAX_ORDER_VALUE);
|
||||
$model1->saveQuietly();
|
||||
|
||||
$model2->setOrder($order1);
|
||||
$model2->saveQuietly();
|
||||
|
||||
$model1->setOrder($order2);
|
||||
$model1->saveQuietly();
|
||||
}
|
||||
}
|
||||
22
app/Actions/Common/ChangeModelOrderInput.php
Normal file
22
app/Actions/Common/ChangeModelOrderInput.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Common;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use SortDirection;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final readonly class ChangeModelOrderInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public Model & ModelWithOrder $model,
|
||||
public SortDirection $direction,
|
||||
) {
|
||||
Assert::isInstanceOf($this->model, Model::class);
|
||||
Assert::isInstanceOf($this->model, ModelWithOrder::class);
|
||||
}
|
||||
}
|
||||
32
app/Actions/Common/ToggleModelStar.php
Normal file
32
app/Actions/Common/ToggleModelStar.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Common;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Interfaces\ModelWithStar;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Common\ToggleModelStarInput, \Illuminate\Database\Eloquent\Model&\App\Interfaces\ModelWithStar>
|
||||
*/
|
||||
final class ToggleModelStar implements Action
|
||||
{
|
||||
/**
|
||||
* @param \App\Actions\Common\ToggleModelStarInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): Model & ModelWithStar
|
||||
{
|
||||
Assert::isInstanceOf($input, ToggleModelStarInput::class);
|
||||
|
||||
$model = $input->model;
|
||||
$model->toggleStar();
|
||||
$model->saveOrFail();
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
20
app/Actions/Common/ToggleModelStarInput.php
Normal file
20
app/Actions/Common/ToggleModelStarInput.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Common;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Interfaces\ModelWithStar;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final readonly class ToggleModelStarInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public Model & ModelWithStar $model,
|
||||
) {
|
||||
Assert::isInstanceOf($this->model, Model::class);
|
||||
Assert::isInstanceOf($this->model, ModelWithStar::class);
|
||||
}
|
||||
}
|
||||
32
app/Actions/Common/ToggleModelStatus.php
Normal file
32
app/Actions/Common/ToggleModelStatus.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Common;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Common\ToggleModelStatusInput, \Illuminate\Database\Eloquent\Model&\App\Interfaces\ModelWithStatus>
|
||||
*/
|
||||
final class ToggleModelStatus implements Action
|
||||
{
|
||||
/**
|
||||
* @param \App\Actions\Common\ToggleModelStatusInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): Model & ModelWithStatus
|
||||
{
|
||||
Assert::isInstanceOf($input, ToggleModelStatusInput::class);
|
||||
|
||||
$model = $input->model;
|
||||
$model->toggleStatus();
|
||||
$model->saveOrFail();
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
20
app/Actions/Common/ToggleModelStatusInput.php
Normal file
20
app/Actions/Common/ToggleModelStatusInput.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Common;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final readonly class ToggleModelStatusInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public Model & ModelWithStatus $model,
|
||||
) {
|
||||
Assert::isInstanceOf($this->model, Model::class);
|
||||
Assert::isInstanceOf($this->model, ModelWithStatus::class);
|
||||
}
|
||||
}
|
||||
|
|
@ -42,4 +42,11 @@ class ChangeOrderRequest extends FormRequest
|
|||
'action' => ['required', 'string', 'in:up,down'],
|
||||
];
|
||||
}
|
||||
|
||||
public function getSortDirection(): \SortDirection
|
||||
{
|
||||
return strval($this->route('action')) === 'up'
|
||||
? \SortDirection::Ascending
|
||||
: \SortDirection::Descending;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
app/Interfaces/ModelWithOrder.php
Normal file
12
app/Interfaces/ModelWithOrder.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Interfaces;
|
||||
|
||||
interface ModelWithOrder
|
||||
{
|
||||
public function getOrder(): int;
|
||||
|
||||
public function setOrder(int $order): void;
|
||||
}
|
||||
|
|
@ -5,10 +5,12 @@ declare(strict_types=1);
|
|||
namespace App\Models;
|
||||
|
||||
use App\Casts\MoneyCast;
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use App\Interfaces\ModelWithStar;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use App\Lib\Helpers\FileStorageHelper;
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use App\Traits\Models\HasOrder;
|
||||
use App\Traits\Models\HasStar;
|
||||
use App\Traits\Models\HasStatus;
|
||||
use App\Traits\Models\HasUuidV7AsPrimaryKey;
|
||||
|
|
@ -107,8 +109,9 @@ use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
|||
'picture_large_title',
|
||||
'picture_large',
|
||||
])]
|
||||
class Product extends Model implements ModelWithStar, ModelWithStatus, RedirectableInterface
|
||||
class Product extends Model implements ModelWithOrder, ModelWithStar, ModelWithStatus, RedirectableInterface
|
||||
{
|
||||
use HasOrder;
|
||||
use HasStar;
|
||||
use HasStatus;
|
||||
use HasUuidV7AsPrimaryKey;
|
||||
|
|
|
|||
|
|
@ -4,8 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use App\Lib\Helpers\FileStorageHelper;
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use App\Traits\Models\HasOrder;
|
||||
use App\Traits\Models\HasStatus;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
|
@ -37,8 +41,10 @@ use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
|||
'picture_title',
|
||||
'status',
|
||||
])]
|
||||
class Range extends Model implements RedirectableInterface
|
||||
class Range extends Model implements ModelWithOrder, ModelWithStatus, RedirectableInterface
|
||||
{
|
||||
use HasOrder;
|
||||
use HasStatus;
|
||||
use IsRedirectable;
|
||||
|
||||
#[\Override]
|
||||
|
|
|
|||
|
|
@ -4,7 +4,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use App\Lib\Helpers\FileStorageHelper;
|
||||
use App\Traits\Models\HasOrder;
|
||||
use App\Traits\Models\HasStatus;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
||||
|
|
@ -54,11 +58,14 @@ use Shared\Helpers\DatesHelper;
|
|||
'status',
|
||||
'order',
|
||||
])]
|
||||
class Slide extends Model
|
||||
class Slide extends Model implements ModelWithOrder, ModelWithStatus
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\SlideFactory> */
|
||||
use HasFactory;
|
||||
|
||||
use HasOrder;
|
||||
use HasStatus;
|
||||
|
||||
#[\Override]
|
||||
protected static function booted()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use App\Traits\Models\HasOrder;
|
||||
use App\Traits\Models\HasStatus;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;
|
||||
|
|
@ -33,11 +35,12 @@ use Illuminate\Support\Str;
|
|||
'status',
|
||||
])]
|
||||
#[WithoutTimestamps]
|
||||
class Tag extends Model implements ModelWithStatus
|
||||
class Tag extends Model implements ModelWithOrder, ModelWithStatus
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\TagFactory> */
|
||||
use HasFactory;
|
||||
|
||||
use HasOrder;
|
||||
use HasStatus;
|
||||
|
||||
#[\Override]
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace App\Providers;
|
|||
|
||||
use App\Dto\ProductWithPricingDto;
|
||||
use App\Models\Discount;
|
||||
use App\Models\Faq;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductInco;
|
||||
use App\Models\Range;
|
||||
|
|
@ -90,6 +91,7 @@ class RouteServiceProvider extends ServiceProvider
|
|||
});
|
||||
|
||||
Route::model('inco', ProductInco::class);
|
||||
Route::model('faq', Faq::class);
|
||||
Route::model('tag', Tag::class);
|
||||
Route::model('slide', Slide::class);
|
||||
Route::model('range', Range::class);
|
||||
|
|
|
|||
|
|
@ -4,90 +4,36 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Traits\Controllers;
|
||||
|
||||
use App\Actions\Common\ChangeModelOrder;
|
||||
use App\Actions\Common\ChangeModelOrderInput;
|
||||
use App\Contracts\ActionExecutor;
|
||||
use App\Http\Requests\Bo\ChangeOrderRequest;
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use App\Services\FlashMessageService;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use SortDirection;
|
||||
|
||||
trait ChangesModelOrder
|
||||
{
|
||||
private const MODEL_ATTRIBUTE_NAME = 'order';
|
||||
|
||||
private const MAX_ORDER_VALUE = 4294967295;
|
||||
|
||||
public function changeOrder(
|
||||
Model & ModelWithOrder $model,
|
||||
ChangeOrderRequest $request,
|
||||
ChangeModelOrder $action,
|
||||
ActionExecutor $executor,
|
||||
FlashMessageService $flashMessageService
|
||||
): RedirectResponse {
|
||||
/** @var Model|null $model */
|
||||
$model = Collection::make(request()->route()?->parameters())
|
||||
->first(fn ($param) => $param instanceof Model);
|
||||
$direction = $request->getSortDirection();
|
||||
|
||||
if ($model === null && method_exists($this, 'getModelClass')) {
|
||||
$modelClass = $this->getModelClass();
|
||||
/** @var Model $modelInstance */
|
||||
$modelInstance = app($modelClass);
|
||||
$changed = $executor->execute($action, new ChangeModelOrderInput($model, $direction));
|
||||
|
||||
$routeParams = request()->route()?->parameters() ?? [];
|
||||
unset($routeParams['action']);
|
||||
|
||||
$identifier = reset($routeParams);
|
||||
|
||||
if (is_string($identifier) || is_int($identifier)) {
|
||||
$model = $modelInstance->resolveRouteBinding($identifier);
|
||||
}
|
||||
}
|
||||
|
||||
if ($model === null || ! $model instanceof Model || ! array_key_exists(self::MODEL_ATTRIBUTE_NAME, $model->getAttributes())) {
|
||||
throw new \InvalidArgumentException('Model must exists and extend Model and have an order attribute');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($model, $request, $flashMessageService) {
|
||||
$goUp = $request->boolean('direction');
|
||||
|
||||
$adjacentModel = $model->newQuery()
|
||||
->where(
|
||||
self::MODEL_ATTRIBUTE_NAME,
|
||||
$goUp ? '<' : '>',
|
||||
$model->getAttribute(self::MODEL_ATTRIBUTE_NAME)
|
||||
)
|
||||
->orderBy(self::MODEL_ATTRIBUTE_NAME, $goUp ?
|
||||
SortDirection::Descending : SortDirection::Ascending)
|
||||
->first();
|
||||
|
||||
if (! $adjacentModel) {
|
||||
$flashMessageService->error(trans('crud.messages.order_not_changed'));
|
||||
|
||||
return back();
|
||||
}
|
||||
|
||||
$this->permuteModelOrders($model, $adjacentModel);
|
||||
|
||||
$flashMessageService->success(trans('crud.messages.order_changed'));
|
||||
if (! $changed) {
|
||||
$flashMessageService->error(trans('crud.messages.order_not_changed'));
|
||||
|
||||
return back();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private function permuteModelOrders(Model $model1, Model $model2): void
|
||||
{
|
||||
$order1 = $model1->getAttribute(self::MODEL_ATTRIBUTE_NAME);
|
||||
$order2 = $model2->getAttribute(self::MODEL_ATTRIBUTE_NAME);
|
||||
$flashMessageService->success(trans('crud.messages.order_changed'));
|
||||
|
||||
// * Deactivate timestamps to avoid updating `updated_at`
|
||||
$model1->timestamps = false;
|
||||
$model2->timestamps = false;
|
||||
|
||||
$model1->setAttribute(self::MODEL_ATTRIBUTE_NAME, self::MAX_ORDER_VALUE);
|
||||
$model1->saveQuietly();
|
||||
|
||||
$model2->setAttribute(self::MODEL_ATTRIBUTE_NAME, $order1);
|
||||
$model2->saveQuietly();
|
||||
|
||||
$model1->setAttribute(self::MODEL_ATTRIBUTE_NAME, $order2);
|
||||
$model1->saveQuietly();
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Traits\Controllers;
|
||||
|
||||
use App\Actions\Common\ToggleModelStar;
|
||||
use App\Actions\Common\ToggleModelStarInput;
|
||||
use App\Contracts\ActionExecutor;
|
||||
use App\Interfaces\ModelWithStar;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
|
|
@ -18,23 +20,17 @@ trait TogglesModelStar
|
|||
/**
|
||||
* Toggles model star
|
||||
*/
|
||||
public function toggleStar(Model & ModelWithStar $model): RedirectResponse
|
||||
{
|
||||
public function toggleStar(
|
||||
Model & ModelWithStar $model,
|
||||
ToggleModelStar $action,
|
||||
ActionExecutor $executor
|
||||
): RedirectResponse {
|
||||
$this->authorize('toggleStar', $model);
|
||||
|
||||
try {
|
||||
return DB::transaction(function () use ($model) {
|
||||
$model->toggleStar();
|
||||
$model->saveOrFail();
|
||||
$executor->execute($action, new ToggleModelStarInput($model));
|
||||
|
||||
return back()->with('success', trans('crud.messages.has_been_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)),
|
||||
]));
|
||||
});
|
||||
} catch (\Throwable) {
|
||||
return back()->with('error', trans('crud.messages.cannot_be_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)),
|
||||
]));
|
||||
}
|
||||
return back()->with('success', trans('crud.messages.has_been_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,47 +4,33 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Traits\Controllers;
|
||||
|
||||
use App\Actions\Common\ToggleModelStatus;
|
||||
use App\Actions\Common\ToggleModelStatusInput;
|
||||
use App\Contracts\ActionExecutor;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @mixin \Illuminate\Routing\Controller */
|
||||
/**
|
||||
* @mixin \Illuminate\Routing\Controller
|
||||
*/
|
||||
trait TogglesModelStatus
|
||||
{
|
||||
/**
|
||||
* Toggles model status
|
||||
*/
|
||||
public function toggleStatus(): RedirectResponse
|
||||
{
|
||||
/** @var Model|null $model */
|
||||
$model = Collection::make(request()->route()?->parameters())
|
||||
->first(fn ($param) => $param instanceof Model);
|
||||
|
||||
if ($model === null) {
|
||||
throw new \InvalidArgumentException('No model found in route parameters');
|
||||
}
|
||||
|
||||
if (! $model instanceof ModelWithStatus || ! $model instanceof Model) {
|
||||
throw new \InvalidArgumentException('Model must implement ModelWithStatus and extend Model');
|
||||
}
|
||||
|
||||
public function toggleStatus(
|
||||
Model & ModelWithStatus $model,
|
||||
ToggleModelStatus $action,
|
||||
ActionExecutor $executor
|
||||
): RedirectResponse {
|
||||
$this->authorize('toggleStatus', $model);
|
||||
|
||||
return DB::transaction(function () use ($model) {
|
||||
$model->toggleStatus();
|
||||
$executor->execute($action, new ToggleModelStatusInput($model));
|
||||
|
||||
if ($model->saveOrFail()) {
|
||||
return back()->with('success', trans('crud.messages.has_been_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)),
|
||||
]));
|
||||
}
|
||||
|
||||
return back()->with('error', trans('crud.messages.cannot_be_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)),
|
||||
]));
|
||||
});
|
||||
return back()->with('success', trans('crud.messages.has_been_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
app/Traits/Models/HasOrder.php
Normal file
21
app/Traits/Models/HasOrder.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Traits\Models;
|
||||
|
||||
/** @mixin \Illuminate\Database\Eloquent\Model */
|
||||
trait HasOrder
|
||||
{
|
||||
public function getOrder(): int
|
||||
{
|
||||
$order = $this->getAttribute('order');
|
||||
|
||||
return is_numeric($order) ? (int) $order : 0;
|
||||
}
|
||||
|
||||
public function setOrder(int $order): void
|
||||
{
|
||||
$this->setAttribute('order', $order);
|
||||
}
|
||||
}
|
||||
|
|
@ -235,6 +235,122 @@ describe('ProductsController', function () {
|
|||
expect(Product::find($product->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('can toggle star', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$range = new Range();
|
||||
$range->name = 'Test Range ' . uniqid();
|
||||
$range->status = true;
|
||||
$range->order = random_int(1, 9999);
|
||||
$range->picture_alt = 'alt';
|
||||
$range->picture_title = 'title';
|
||||
$range->save();
|
||||
|
||||
$product = new Product([
|
||||
'range_id' => $range->id,
|
||||
'name' => 'Test Product ' . uniqid(),
|
||||
'brand' => ProductBrand::loue,
|
||||
'invoicing_unit' => InvoicingUnit::PCE,
|
||||
'sell_unit' => SellUnit::PCE,
|
||||
'order_unit' => OrderUnit::PCE,
|
||||
'tax_rate' => TaxRate::TVA_20,
|
||||
'description' => 'Test description',
|
||||
'status' => true,
|
||||
]);
|
||||
$product->price = Money::EUR(1000);
|
||||
$product->short_description = 'Short test description';
|
||||
$product->picture_alt = 'test alt';
|
||||
$product->picture_title = 'test title';
|
||||
$product->picture_large_alt = 'test alt';
|
||||
$product->picture_large_title = 'test title';
|
||||
$product->star = false;
|
||||
$product->save();
|
||||
|
||||
// WHEN
|
||||
$response = $this->patch("/bo/products/{$product->slug}/toggle-star");
|
||||
|
||||
// THEN
|
||||
$product->refresh();
|
||||
expect($product->star)->toBeTrue();
|
||||
$response->assertRedirect();
|
||||
});
|
||||
|
||||
it('can toggle status', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$range = new Range();
|
||||
$range->name = 'Test Range ' . uniqid();
|
||||
$range->status = true;
|
||||
$range->order = random_int(1, 9999);
|
||||
$range->picture_alt = 'alt';
|
||||
$range->picture_title = 'title';
|
||||
$range->save();
|
||||
|
||||
$product = new Product([
|
||||
'range_id' => $range->id,
|
||||
'name' => 'Test Product ' . uniqid(),
|
||||
'brand' => ProductBrand::loue,
|
||||
'invoicing_unit' => InvoicingUnit::PCE,
|
||||
'sell_unit' => SellUnit::PCE,
|
||||
'order_unit' => OrderUnit::PCE,
|
||||
'tax_rate' => TaxRate::TVA_20,
|
||||
'description' => 'Test description',
|
||||
'status' => true,
|
||||
]);
|
||||
$product->price = Money::EUR(1000);
|
||||
$product->short_description = 'Short test description';
|
||||
$product->picture_alt = 'test alt';
|
||||
$product->picture_title = 'test title';
|
||||
$product->picture_large_alt = 'test alt';
|
||||
$product->picture_large_title = 'test title';
|
||||
$product->save();
|
||||
|
||||
// WHEN
|
||||
$response = $this->patch("/bo/products/{$product->slug}/toggle-status");
|
||||
|
||||
// THEN
|
||||
$product->refresh();
|
||||
expect($product->status)->toBeFalse();
|
||||
$response->assertRedirect();
|
||||
});
|
||||
|
||||
it('can change order', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$range = new Range();
|
||||
$range->name = 'Test Range ' . uniqid();
|
||||
$range->status = true;
|
||||
$range->order = random_int(1, 9999);
|
||||
$range->picture_alt = 'alt';
|
||||
$range->picture_title = 'title';
|
||||
$range->save();
|
||||
|
||||
$product = new Product([
|
||||
'range_id' => $range->id,
|
||||
'name' => 'Test Product ' . uniqid(),
|
||||
'brand' => ProductBrand::loue,
|
||||
'invoicing_unit' => InvoicingUnit::PCE,
|
||||
'sell_unit' => SellUnit::PCE,
|
||||
'order_unit' => OrderUnit::PCE,
|
||||
'tax_rate' => TaxRate::TVA_20,
|
||||
'description' => 'Test description',
|
||||
'status' => true,
|
||||
]);
|
||||
$product->price = Money::EUR(1000);
|
||||
$product->short_description = 'Short test description';
|
||||
$product->picture_alt = 'test alt';
|
||||
$product->picture_title = 'test title';
|
||||
$product->picture_large_alt = 'test alt';
|
||||
$product->picture_large_title = 'test title';
|
||||
$product->save();
|
||||
|
||||
// WHEN
|
||||
$response = $this->patch("/bo/products/{$product->slug}/up");
|
||||
|
||||
// THEN
|
||||
expect($response->status())->toBe(302);
|
||||
});
|
||||
|
||||
describe('authorization rejections', function () {
|
||||
it('redirects unauthenticated guest to login', function () {
|
||||
/** @var \Tests\TestCase $this */
|
||||
|
|
|
|||
|
|
@ -6,15 +6,16 @@ use App\Models\Tag;
|
|||
use App\Models\User;
|
||||
use Domain\Enums\UserRole;
|
||||
|
||||
describe('TagsController', function () {
|
||||
|
||||
beforeEach(function () {
|
||||
describe('TagsController', function (): void {
|
||||
beforeEach(function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
|
||||
$this->actingAs($this->user, 'backend');
|
||||
$user = User::factory()->create(['role' => UserRole::SuperAdmin]);
|
||||
$this->actingAs($user, 'backend');
|
||||
});
|
||||
|
||||
it('can access index', function () {
|
||||
it('can access index', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// WHEN
|
||||
$response = $this->get('/bo/tags');
|
||||
|
||||
|
|
@ -23,7 +24,8 @@ describe('TagsController', function () {
|
|||
$response->assertViewIs('bo.tags.index');
|
||||
});
|
||||
|
||||
it('can access create', function () {
|
||||
it('can access create', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// WHEN
|
||||
$response = $this->get('/bo/tags/create');
|
||||
|
||||
|
|
@ -32,7 +34,8 @@ describe('TagsController', function () {
|
|||
$response->assertViewIs('bo.tags.create');
|
||||
});
|
||||
|
||||
it('can store a tag', function () {
|
||||
it('can store a tag', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$data = [
|
||||
'label' => 'New Tag',
|
||||
|
|
@ -43,12 +46,12 @@ describe('TagsController', function () {
|
|||
$response = $this->post('/bo/tags', $data);
|
||||
|
||||
// THEN
|
||||
$tag = Tag::where('label', 'New Tag')->first();
|
||||
expect($tag)->not->toBeNull();
|
||||
$tag = Tag::query()->where('label', 'New Tag')->firstOrFail();
|
||||
$response->assertRedirect("/bo/tags/{$tag->id}/edit");
|
||||
});
|
||||
|
||||
it('can access edit', function () {
|
||||
it('can access edit', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
|
||||
|
|
@ -60,7 +63,8 @@ describe('TagsController', function () {
|
|||
$response->assertViewIs('bo.tags.edit');
|
||||
});
|
||||
|
||||
it('can update a tag', function () {
|
||||
it('can update a tag', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
|
||||
|
|
@ -79,7 +83,8 @@ describe('TagsController', function () {
|
|||
$response->assertRedirect("/bo/tags/{$tag->id}/edit");
|
||||
});
|
||||
|
||||
it('can delete a tag', function () {
|
||||
it('can delete a tag', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
|
||||
|
|
@ -87,11 +92,12 @@ describe('TagsController', function () {
|
|||
$response = $this->delete("/bo/tags/{$tag->id}");
|
||||
|
||||
// THEN
|
||||
expect(Tag::find($tag->id))->toBeNull();
|
||||
expect(Tag::query()->find($tag->id))->toBeNull();
|
||||
$response->assertRedirect('/bo/tags');
|
||||
});
|
||||
|
||||
it('can toggle status', function () {
|
||||
it('can toggle status', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
$tag->status = true;
|
||||
|
|
@ -106,7 +112,8 @@ describe('TagsController', function () {
|
|||
$response->assertRedirect();
|
||||
});
|
||||
|
||||
it('can change order', function () {
|
||||
it('can change order', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Common\ChangeModelOrder;
|
||||
use App\Actions\Common\ChangeModelOrderInput;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('ChangeModelOrder action', function (): void {
|
||||
it('permutes order between adjacent models', function (): void {
|
||||
// GIVEN
|
||||
$tag1 = Tag::factory()->create();
|
||||
$tag1->setOrder(1);
|
||||
$tag1->save();
|
||||
|
||||
$tag2 = Tag::factory()->create();
|
||||
$tag2->setOrder(2);
|
||||
$tag2->save();
|
||||
|
||||
$action = new ChangeModelOrder();
|
||||
$input = new ChangeModelOrderInput($tag2, \SortDirection::Ascending);
|
||||
|
||||
// WHEN
|
||||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeTrue();
|
||||
|
||||
$fresh1 = Tag::query()->find($tag1->id);
|
||||
$fresh2 = Tag::query()->find($tag2->id);
|
||||
|
||||
expect($fresh1?->getOrder())->toBe(2)
|
||||
->and($fresh2?->getOrder())->toBe(1);
|
||||
});
|
||||
|
||||
it('returns false when no adjacent model exists', function (): void {
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
$tag->setOrder(1);
|
||||
$tag->save();
|
||||
|
||||
$action = new ChangeModelOrder();
|
||||
$input = new ChangeModelOrderInput($tag, \SortDirection::Ascending);
|
||||
|
||||
// WHEN
|
||||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeFalse();
|
||||
});
|
||||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$action = new ChangeModelOrder();
|
||||
$invalidInput = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
expect(
|
||||
/** @phpstan-ignore argument.type */
|
||||
fn () => $action($invalidInput)
|
||||
)
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Common\ToggleModelStar;
|
||||
use App\Actions\Common\ToggleModelStarInput;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Product;
|
||||
use App\Models\Range;
|
||||
use Domain\Enums\ProductBrand;
|
||||
use Domain\Enums\SellUnit;
|
||||
use Domain\Enums\TaxRate;
|
||||
use Domain\Enums\Units\InvoicingUnit;
|
||||
use Domain\Enums\Units\OrderUnit;
|
||||
use Money\Money;
|
||||
|
||||
describe('ToggleModelStar action', function (): void {
|
||||
it('toggles model star in database', function (): void {
|
||||
// GIVEN
|
||||
$range = new Range();
|
||||
$range->name = 'Test Range ' . uniqid();
|
||||
$range->status = true;
|
||||
$range->order = random_int(1, 9999);
|
||||
$range->picture_alt = 'alt';
|
||||
$range->picture_title = 'title';
|
||||
$range->save();
|
||||
|
||||
$product = new Product([
|
||||
'range_id' => $range->id,
|
||||
'name' => 'Test Product ' . uniqid(),
|
||||
'brand' => ProductBrand::loue,
|
||||
'invoicing_unit' => InvoicingUnit::PCE,
|
||||
'sell_unit' => SellUnit::PCE,
|
||||
'order_unit' => OrderUnit::PCE,
|
||||
'tax_rate' => TaxRate::TVA_20,
|
||||
'description' => 'Test description',
|
||||
'status' => true,
|
||||
]);
|
||||
$product->price = Money::EUR(1000);
|
||||
$product->short_description = 'Short test description';
|
||||
$product->picture_alt = 'test alt';
|
||||
$product->picture_title = 'test title';
|
||||
$product->picture_large_alt = 'test alt';
|
||||
$product->picture_large_title = 'test title';
|
||||
$product->star = false;
|
||||
$product->save();
|
||||
|
||||
$action = new ToggleModelStar();
|
||||
$input = new ToggleModelStarInput($product);
|
||||
|
||||
// WHEN
|
||||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result->getAttribute('star'))->toBeTrue();
|
||||
|
||||
$fresh = Product::query()->find($product->id);
|
||||
assert($fresh instanceof Product);
|
||||
expect($fresh->getAttribute('star'))->toBeTrue();
|
||||
});
|
||||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$action = new ToggleModelStar();
|
||||
$invalidInput = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
expect(
|
||||
/** @phpstan-ignore argument.type */
|
||||
fn () => $action($invalidInput)
|
||||
)
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Common\ToggleModelStatus;
|
||||
use App\Actions\Common\ToggleModelStatusInput;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('ToggleModelStatus action', function (): void {
|
||||
it('toggles model status in database', function (): void {
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create([
|
||||
'status' => true,
|
||||
]);
|
||||
|
||||
$action = new ToggleModelStatus();
|
||||
$input = new ToggleModelStatusInput($tag);
|
||||
|
||||
// WHEN
|
||||
$result = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($result->getAttribute('status'))->toBeFalse();
|
||||
|
||||
$fresh = Tag::query()->find($tag->id);
|
||||
expect($fresh?->getAttribute('status'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$action = new ToggleModelStatus();
|
||||
$invalidInput = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
expect(
|
||||
/** @phpstan-ignore argument.type */
|
||||
fn () => $action($invalidInput)
|
||||
)
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
31
tests/Unit/App/Actions/Common/ChangeModelOrderInputTest.php
Normal file
31
tests/Unit/App/Actions/Common/ChangeModelOrderInputTest.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Common\ChangeModelOrderInput;
|
||||
use App\Interfaces\ModelWithOrder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
describe('ChangeModelOrderInput', function (): void {
|
||||
it('can be instantiated with a model implementing ModelWithOrder and a SortDirection', function (): void {
|
||||
// GIVEN
|
||||
$mock = new class () extends Model implements ModelWithOrder
|
||||
{
|
||||
public function getOrder(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function setOrder(int $order): void
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// WHEN
|
||||
$input = new ChangeModelOrderInput(model: $mock, direction: \SortDirection::Ascending);
|
||||
|
||||
// THEN
|
||||
expect($input->model)->toBe($mock)
|
||||
->and($input->direction)->toBe(\SortDirection::Ascending);
|
||||
});
|
||||
});
|
||||
29
tests/Unit/App/Actions/Common/ToggleModelStarInputTest.php
Normal file
29
tests/Unit/App/Actions/Common/ToggleModelStarInputTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Common\ToggleModelStarInput;
|
||||
use App\Interfaces\ModelWithStar;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
describe('ToggleModelStarInput', function (): void {
|
||||
it('can be instantiated with a model implementing ModelWithStar', function (): void {
|
||||
// GIVEN
|
||||
$mock = new class () extends Model implements ModelWithStar
|
||||
{
|
||||
public function setStar(bool $value): void
|
||||
{
|
||||
}
|
||||
|
||||
public function toggleStar(): void
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// WHEN
|
||||
$input = new ToggleModelStarInput(model: $mock);
|
||||
|
||||
// THEN
|
||||
expect($input->model)->toBe($mock);
|
||||
});
|
||||
});
|
||||
29
tests/Unit/App/Actions/Common/ToggleModelStatusInputTest.php
Normal file
29
tests/Unit/App/Actions/Common/ToggleModelStatusInputTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Common\ToggleModelStatusInput;
|
||||
use App\Interfaces\ModelWithStatus;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
describe('ToggleModelStatusInput', function (): void {
|
||||
it('can be instantiated with a model implementing ModelWithStatus', function (): void {
|
||||
// GIVEN
|
||||
$mock = new class () extends Model implements ModelWithStatus
|
||||
{
|
||||
public function setStatus(bool $value): void
|
||||
{
|
||||
}
|
||||
|
||||
public function toggleStatus(): void
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// WHEN
|
||||
$input = new ToggleModelStatusInput(model: $mock);
|
||||
|
||||
// THEN
|
||||
expect($input->model)->toBe($mock);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue