From f7a19b72ad143b5fa47c0a9db65335c8aa6f255a Mon Sep 17 00:00:00 2001 From: rinsvent Date: Sun, 17 May 2026 21:10:10 +0700 Subject: [PATCH] feat: add controller logic --- .../Controllers/AvailabilityController.php | 13 ++++- app/Http/Controllers/HoldController.php | 58 ++++++++++++++++++- 2 files changed, 67 insertions(+), 4 deletions(-) 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(), + ]; + } }