GET /slots/availability, POST /slots/{id}/hold, POST /holds/{id}/confirm
and DELETE /holds/{id} on Laravel 12 and MySQL 8.
- availability cached for 10s behind a single-flight lock with a stale
copy, invalidated after commit on confirm and cancel
- oversell prevented by a conditional decrement, verified by the number
of affected rows rather than a preceding select
- idempotency enforced by a unique index on holds.idempotency_key
- active holds counted with an expires_at filter, so no background
cleanup is required for correctness
- 27 feature tests against MySQL, service documentation, Postman collection
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Http\Requests\StoreHoldRequest;
|
||
use App\Http\Resources\HoldResource;
|
||
use App\Services\SlotService;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
final class HoldController extends Controller
|
||
{
|
||
public function __construct(private readonly SlotService $slots) {}
|
||
|
||
/**
|
||
* POST /slots/{slot}/hold
|
||
*
|
||
* Без привязки модели: строку слота читает сервис, внутри транзакции и с локом.
|
||
*/
|
||
public function store(StoreHoldRequest $request, int $slot): JsonResponse
|
||
{
|
||
$hold = $this->slots->hold($slot, $request->idempotencyKey());
|
||
|
||
// Повтор по ключу ничего не создал — отвечаем 200 вместо 201.
|
||
$status = $hold->wasRecentlyCreated ? Response::HTTP_CREATED : Response::HTTP_OK;
|
||
|
||
return HoldResource::make($hold)->response()->setStatusCode($status);
|
||
}
|
||
|
||
/**
|
||
* POST /holds/{hold}/confirm
|
||
*/
|
||
public function confirm(int $hold): JsonResponse
|
||
{
|
||
return HoldResource::make($this->slots->confirm($hold))->response();
|
||
}
|
||
|
||
/**
|
||
* DELETE /holds/{hold}
|
||
*/
|
||
public function destroy(int $hold): JsonResponse
|
||
{
|
||
return HoldResource::make($this->slots->cancel($hold))->response();
|
||
}
|
||
}
|