ldc-boutique-cnc-sable/app/Http/Controllers/Bo/DiscountsController.php

140 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Bo;
use App\Actions\Discounts\CreateDiscount;
use App\Actions\Discounts\CreateDiscountInput;
use App\Actions\Discounts\DeleteDiscount;
use App\Actions\Discounts\DeleteDiscountInput;
use App\Actions\Discounts\UpdateDiscount;
use App\Actions\Discounts\UpdateDiscountInput;
use App\Contracts\Executor;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Discounts\StoreDiscountRequest;
use App\Http\Requests\Bo\Discounts\UpdateDiscountRequest;
use App\Models\Discount;
use App\Queries\Discounts\GetDiscountFormDataQuery;
use App\Queries\Discounts\GetDiscountFormDataQueryInput;
use App\Queries\Discounts\GetPaginatedDiscountsQuery;
use App\Queries\Discounts\GetPaginatedDiscountsQueryInput;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class DiscountsController extends Controller
{
public function __construct(
private readonly Executor $executor,
) {
}
public function index(Request $request, GetPaginatedDiscountsQuery $query): View
{
$this->authorize('viewAny', Discount::class);
$pagination = $request->pagination();
$discounts = $this->executor->execute(
$query,
new GetPaginatedDiscountsQueryInput(
pagination: $pagination,
)
);
return view('bo.discounts.index', compact('discounts'));
}
public function create(GetDiscountFormDataQuery $query): View
{
$this->authorize('create', Discount::class);
$formData = $this->executor->execute(
$query,
new GetDiscountFormDataQueryInput()
);
return view('bo.discounts.create', [
'discountTypes' => $formData->discountTypes,
'existingProds' => $formData->existingProds,
'selectedProds' => $formData->selectedProds,
]);
}
public function store(StoreDiscountRequest $request, CreateDiscount $action): RedirectResponse
{
$this->authorize('create', Discount::class);
$discount = $this->executor->execute(
$action,
new CreateDiscountInput(
type: $request->toDiscountType(),
value: $request->toDiscountValue(),
is_old_price_displayed: $request->boolean('is_old_price_displayed'),
period: $request->toDatePeriod(),
associatedProductsIds: $request->toAssociatedProductsIds(),
),
);
return to_route('bo.discounts.edit', $discount)
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.discounts', 1)),
]));
}
public function edit(Discount $discount, GetDiscountFormDataQuery $query): View
{
$this->authorize('update', $discount);
$formData = $this->executor->execute(
$query,
new GetDiscountFormDataQueryInput($discount)
);
return view('bo.discounts.edit', [
'discount' => $formData->discount,
'discountTypes' => $formData->discountTypes,
'existingProds' => $formData->existingProds,
'selectedProds' => $formData->selectedProds,
]);
}
public function update(UpdateDiscountRequest $request, Discount $discount, UpdateDiscount $action): RedirectResponse
{
$this->authorize('update', $discount);
$this->executor->execute(
$action,
new UpdateDiscountInput(
discount: $discount,
type: $request->toDiscountType(),
value: $request->toDiscountValue(),
is_old_price_displayed: $request->boolean('is_old_price_displayed'),
period: $request->toDatePeriod(),
associatedProductsIds: $request->toAssociatedProductsIds(),
),
);
return to_route('bo.discounts.edit', $discount)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.discounts', 1)),
]));
}
public function destroy(Discount $discount, DeleteDiscount $action): RedirectResponse
{
$this->authorize('delete', $discount);
$this->executor->execute(
$action,
new DeleteDiscountInput(discount: $discount),
);
return to_route('bo.discounts.index')
->with('warning', trans('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.discounts', 1)),
]));
}
}