diff --git a/app/Http/Controllers/AvailabilityController.php b/app/Http/Controllers/AvailabilityController.php index b767d83..8ec8409 100644 --- a/app/Http/Controllers/AvailabilityController.php +++ b/app/Http/Controllers/AvailabilityController.php @@ -2,9 +2,18 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use App\Services\SlotService; +use Illuminate\Http\JsonResponse; class AvailabilityController extends Controller { - // + public function __construct( + private readonly SlotService $slotService, + ) { + } + + public function index(): JsonResponse + { + return response()->json($this->slotService->getAvailability()); + } } diff --git a/app/Http/Controllers/HoldController.php b/app/Http/Controllers/HoldController.php index 0ff176c..3c16cb2 100644 --- a/app/Http/Controllers/HoldController.php +++ b/app/Http/Controllers/HoldController.php @@ -2,9 +2,63 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use App\Exceptions\HoldNotAvailableException; +use App\Exceptions\SlotCapacityExceededException; +use App\Models\Hold; +use App\Models\Slot; +use App\Services\SlotService; +use Illuminate\Http\JsonResponse; +use Symfony\Component\HttpFoundation\Response; class HoldController extends Controller { - // + public function __construct( + private readonly SlotService $slotService, + ) { + } + + public function store(Slot $slot): JsonResponse + { + $idempotencyKey = request()->header('X-Idempotency-Key'); + + try { + $hold = $this->slotService->createHold($slot, $idempotencyKey); + } catch (SlotCapacityExceededException $exception) { + return response()->json([ + 'message' => $exception->getMessage() + ], Response::HTTP_CONFLICT); + } + + return response()->json($this->holdPayload($hold), Response::HTTP_CREATED); + } + + public function confirm(Hold $hold): JsonResponse + { + try { + $hold = $this->slotService->confirmHold($hold); + } catch (HoldNotAvailableException $exception) { + return response()->json([ + 'message' => $exception->getMessage() + ], Response::HTTP_CONFLICT); + } + + return response()->json($this->holdPayload($hold)); + } + + public function destroy(Hold $hold): JsonResponse + { + $hold = $this->slotService->cancelHold($hold); + + return response()->json($this->holdPayload($hold)); + } + + private function holdPayload(Hold $hold): array + { + return [ + 'id' => $hold->id, + 'slot_id' => $hold->slot_id, + 'status' => $hold->status, + 'expires_at' => $hold->expires_at->toIso8601String(), + ]; + } }