ldc-cocotte/app/Http/Controllers/Bo/ShopsController.php
Jérémy Munsch 42bdae01cc
feat: implement pagination using Pagination value object
- Updated DataTableService to accept a Pagination object for pagination parameters.
- Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination.

refactor: replace ActionExecutor with Executor in controller traits

- Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor.

feat: add PictureMetadata and Slug value objects

- Introduced PictureMetadata for managing image metadata with validation.
- Added Slug value object for slug generation and validation.

feat: update breadcrumbs to support RangeDto

- Modified breadcrumbs-bo.php to accept RangeDto in the edit route.

test: add tests for RangesController

- Added tests for accessing, creating, updating, and deleting ranges.
- Included tests for toggling range status.

test: add integration tests for range actions

- Created tests for CreateRange, DeleteRange, and UpdateRange actions.
- Validated input handling and database interactions.

test: add unit tests for range input classes

- Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints.

test: add unit tests for value objects

- Added tests for PictureMetadata and Slug to ensure proper validation and instantiation.

test: add unit tests for RangeDto

- Created tests to verify RangeDto mapping from Range model.

test: add unit tests for RangePolicy

- Implemented tests for RangePolicy to validate permissions based on user roles.
2026-10-27 09:58:34 +01:00

189 lines
6.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Shops\StoreShopRequest;
use App\Http\Requests\Bo\Shops\UpdateShopPickupExceptionsRequest;
use App\Http\Requests\Bo\Shops\UpdateShopRequest;
use App\Models\PickupException;
use App\Models\Shop;
use App\Services\DataTableService;
use Domain\Ports\Repositories\ShopRepository;
use Domain\Services\PickupSchedulingService;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class ShopsController extends Controller
{
public function __construct(
private readonly PickupSchedulingService $pickupService,
private readonly ShopRepository $shopRepository,
) {
}
public function index(Request $request, DataTableService $dataTable): View
{
$this->authorize('viewAny', Shop::class);
$query = Shop::query();
if ($request->has('pickup-exception')) {
request()->session()->forget('shops.filter.pickup-exception');
request()->session()->put(
'shops.filter.pickup-exception',
$request->input('pickup-exception')
);
}
$pickupException = request()->session()->get('shops.filter.pickup-exception');
if (! is_null($pickupException)) {
$query->whereHas(
'pickupExceptions',
fn (Builder $q) => $q->where('id', $pickupException)
);
}
$dataTable->sortQuery(Shop::class, $query);
$pagination = $request->pagination();
$shops = $dataTable->paginateQuery($query, $pagination);
$pickupExceptions = PickupException::query()->get();
return view('bo.shops.index', compact('shops', 'pickupExceptions'));
}
public function create(): View
{
$this->authorize('create', Shop::class);
return view('bo.shops.create');
}
public function store(StoreShopRequest $request): RedirectResponse
{
$this->authorize('create', Shop::class);
return DB::transaction(function () use ($request) {
$shop = new Shop();
$shop->fill($request->only(
$shop->getFillable()
));
if ($shop->saveOrFail()) {
return to_route('bo.shops.edit', $shop)
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
]));
}
return to_route('bo.shops.create')
->with('error', trans('crud.messages.cannot_be_created', [
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
]));
});
}
public function edit(Shop $shop): View
{
$this->authorize('update', $shop);
return view('bo.shops.edit', compact('shop'));
}
public function update(UpdateShopRequest $request, Shop $shop): RedirectResponse
{
$this->authorize('update', $shop);
return DB::transaction(function () use ($request, $shop) {
$shop->fill($request->only(
$shop->getFillable()
));
if ($shop->saveOrFail()) {
return to_route('bo.shops.edit', $shop)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
]));
}
return to_route('bo.shops.edit', $shop)
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
]));
});
}
public function destroy(Shop $shop): RedirectResponse
{
$this->authorize('delete', $shop);
return DB::transaction(function () use ($shop) {
if ($shop->deleteOrFail()) {
return to_route('bo.shops.index')
->with('warning', trans('crud.messages.has_been_deleted', ['model' => Str::ucfirst(trans_choice('models.classes.shops', 1))]));
}
return to_route('bo.shops.edit', $shop)
->with('error', trans('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
]));
});
}
public function pickupExceptionsEdit(Shop $shop): View
{
$this->authorize('pickupExceptionUpdate', $shop);
$shop->load('pickupExceptions');
$domainShop = $this->shopRepository->find($shop->getId());
if ($domainShop === null) {
abort(404, 'Shop not found in domain');
}
$pickupExceptions = $this->pickupService->getEditablePickupExceptionsFor($domainShop);
$pickupDay = $this->pickupService->getPickupDay();
$pickupPeriod = $this->pickupService->getPeriodFromPickUpDay();
return view('bo.shops.pickup-exceptions', compact(
'shop',
'pickupExceptions',
'pickupDay',
'pickupPeriod',
));
}
public function pickupExceptionsUpdate(UpdateShopPickupExceptionsRequest $request, Shop $shop): RedirectResponse
{
$this->authorize('pickupExceptionUpdate', $shop);
return DB::transaction(function () use ($request, $shop) {
$pickupExceptions = $request->array('pickup-exceptions');
$replacementDates = $request->array('replacement_date');
$orderDeadlines = $request->array('order_deadline_at');
$syncData = [];
foreach ($pickupExceptions as $exceptionId) {
$replacementDate = ! empty($replacementDates[$exceptionId]) ? $replacementDates[$exceptionId] : null;
$orderDeadline = ! empty($orderDeadlines[$exceptionId]) ? $orderDeadlines[$exceptionId] : null;
$syncData[$exceptionId] = [
'replacement_date' => $replacementDate,
'order_deadline_at' => $orderDeadline,
];
}
$shop->pickupExceptions()->sync($syncData);
return back()->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)),
]));
});
}
}