initial commit
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\BookingConflictException;
|
||||
use App\Exceptions\WalletInsufficientFundsException;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Concerns\RespondsWithJson;
|
||||
use App\Http\Requests\Api\V1\Booking\MoveBookingRequest;
|
||||
use App\Http\Requests\Api\V1\Booking\StoreBookingRequest;
|
||||
use App\Http\Resources\BookingResource;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Machine;
|
||||
use App\Services\BookingService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class BookingController extends Controller
|
||||
{
|
||||
use RespondsWithJson;
|
||||
|
||||
public function __construct(
|
||||
private readonly BookingService $bookingService,
|
||||
) {}
|
||||
|
||||
public function store(StoreBookingRequest $request): JsonResponse
|
||||
{
|
||||
$machine = Machine::query()
|
||||
->where('uuid', $request->string('machine_uuid')->toString())
|
||||
->firstOrFail();
|
||||
|
||||
try {
|
||||
$booking = $this->bookingService->createBooking(
|
||||
$request->user(),
|
||||
$machine,
|
||||
Carbon::parse($request->input('slot_start')),
|
||||
Carbon::parse($request->input('slot_end')),
|
||||
$request->input('idempotency_key'),
|
||||
);
|
||||
} catch (BookingConflictException $e) {
|
||||
return $this->error($e->getMessage(), 409);
|
||||
} catch (WalletInsufficientFundsException $e) {
|
||||
return $this->error($e->getMessage(), 402);
|
||||
} catch (\RuntimeException|\InvalidArgumentException $e) {
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
return $this->created([
|
||||
'booking' => new BookingResource($booking->load('machine.establishment')),
|
||||
], 'Réservation créée.');
|
||||
}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$bookings = Booking::query()
|
||||
->where('user_id', $request->user()->id)
|
||||
->with(['machine.establishment'])
|
||||
->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')->toString()))
|
||||
->orderByDesc('slot_start')
|
||||
->paginate($request->integer('per_page', 20));
|
||||
|
||||
return $this->success([
|
||||
'bookings' => BookingResource::collection($bookings),
|
||||
'meta' => [
|
||||
'current_page' => $bookings->currentPage(),
|
||||
'last_page' => $bookings->lastPage(),
|
||||
'per_page' => $bookings->perPage(),
|
||||
'total' => $bookings->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request, string $uuid): JsonResponse
|
||||
{
|
||||
$booking = Booking::query()
|
||||
->where('uuid', $uuid)
|
||||
->where('user_id', $request->user()->id)
|
||||
->with(['machine.establishment'])
|
||||
->firstOrFail();
|
||||
|
||||
return $this->success([
|
||||
'booking' => new BookingResource($booking),
|
||||
]);
|
||||
}
|
||||
|
||||
public function cancel(Request $request, string $uuid): JsonResponse
|
||||
{
|
||||
$booking = Booking::query()
|
||||
->where('uuid', $uuid)
|
||||
->where('user_id', $request->user()->id)
|
||||
->firstOrFail();
|
||||
|
||||
try {
|
||||
$booking = $this->bookingService->cancelBooking($booking, $request->user());
|
||||
} catch (\RuntimeException $e) {
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'booking' => new BookingResource($booking->load('machine.establishment')),
|
||||
], 'Réservation annulée.');
|
||||
}
|
||||
|
||||
public function move(MoveBookingRequest $request, string $uuid): JsonResponse
|
||||
{
|
||||
$booking = Booking::query()
|
||||
->where('uuid', $uuid)
|
||||
->where('user_id', $request->user()->id)
|
||||
->firstOrFail();
|
||||
|
||||
try {
|
||||
$booking = $this->bookingService->moveBooking(
|
||||
$booking,
|
||||
$request->user(),
|
||||
Carbon::parse($request->input('slot_start')),
|
||||
Carbon::parse($request->input('slot_end')),
|
||||
);
|
||||
} catch (BookingConflictException $e) {
|
||||
return $this->error($e->getMessage(), 409);
|
||||
} catch (WalletInsufficientFundsException $e) {
|
||||
return $this->error($e->getMessage(), 402);
|
||||
} catch (\RuntimeException|\InvalidArgumentException $e) {
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'booking' => new BookingResource($booking->load('machine.establishment')),
|
||||
], 'Réservation déplacée.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user