feat: add controller logic
This commit is contained in:
parent
8d21cfb983
commit
f7a19b72ad
@ -2,9 +2,18 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use App\Services\SlotService;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
class AvailabilityController extends Controller
|
class AvailabilityController extends Controller
|
||||||
{
|
{
|
||||||
//
|
public function __construct(
|
||||||
|
private readonly SlotService $slotService,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
return response()->json($this->slotService->getAvailability());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,63 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
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
|
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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user