94 lines
3.1 KiB
PHP
94 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Exceptions\MachineNotAvailableException;
|
|
use App\Exceptions\WalletInsufficientFundsException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Concerns\RespondsWithJson;
|
|
use App\Http\Requests\Api\V1\Wash\StartWashRequest;
|
|
use App\Http\Resources\WashResource;
|
|
use App\Models\Booking;
|
|
use App\Models\Machine;
|
|
use App\Models\Wash;
|
|
use App\Services\WashService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class WashController extends Controller
|
|
{
|
|
use RespondsWithJson;
|
|
|
|
public function __construct(
|
|
private readonly WashService $washService,
|
|
) {}
|
|
|
|
public function start(StartWashRequest $request): JsonResponse
|
|
{
|
|
$machine = $request->filled('machine_uuid')
|
|
? Machine::query()->where('uuid', $request->string('machine_uuid')->toString())->firstOrFail()
|
|
: Machine::query()->where('qr_code', $request->string('qr_code')->toString())->firstOrFail();
|
|
|
|
$bookingId = null;
|
|
if ($request->filled('booking_uuid')) {
|
|
$bookingId = Booking::query()
|
|
->where('uuid', $request->string('booking_uuid')->toString())
|
|
->where('user_id', $request->user()->id)
|
|
->value('id');
|
|
}
|
|
|
|
try {
|
|
$wash = $this->washService->startWash(
|
|
$request->user(),
|
|
$machine,
|
|
$request->string('trigger_method')->toString(),
|
|
$bookingId,
|
|
$request->input('program'),
|
|
);
|
|
} catch (MachineNotAvailableException $e) {
|
|
return $this->error($e->getMessage(), 409);
|
|
} catch (WalletInsufficientFundsException $e) {
|
|
return $this->error($e->getMessage(), 402);
|
|
} catch (\RuntimeException $e) {
|
|
return $this->error($e->getMessage(), 422);
|
|
}
|
|
|
|
return $this->created([
|
|
'wash' => new WashResource($wash),
|
|
], 'Lavage démarré.');
|
|
}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$washes = Wash::query()
|
|
->where('user_id', $request->user()->id)
|
|
->with(['machine.establishment', 'booking'])
|
|
->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')->toString()))
|
|
->orderByDesc('created_at')
|
|
->paginate($request->integer('per_page', 20));
|
|
|
|
return $this->success([
|
|
'washes' => WashResource::collection($washes),
|
|
'meta' => [
|
|
'current_page' => $washes->currentPage(),
|
|
'last_page' => $washes->lastPage(),
|
|
'per_page' => $washes->perPage(),
|
|
'total' => $washes->total(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function show(Request $request, string $uuid): JsonResponse
|
|
{
|
|
$wash = Wash::query()
|
|
->where('uuid', $uuid)
|
|
->where('user_id', $request->user()->id)
|
|
->with(['machine.establishment', 'booking'])
|
|
->firstOrFail();
|
|
|
|
return $this->success([
|
|
'wash' => new WashResource($wash),
|
|
]);
|
|
}
|
|
}
|