- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。 - 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。 - 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
|
|
|
use App\Models\JackpotPool;
|
|
use App\Models\AdminUser;
|
|
use App\Lottery\ErrorCode;
|
|
use App\Support\ApiResponse;
|
|
use App\Support\ApiMessage;
|
|
use Illuminate\Http\JsonResponse;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Jackpot\JackpotPoolAdjustmentService;
|
|
use App\Http\Requests\Admin\Jackpot\AdminJackpotPoolAdjustRequest;
|
|
use App\Http\Controllers\Api\V1\Admin\Jackpot\Concerns\PresentsJackpotPoolAdjustment;
|
|
|
|
/**
|
|
* POST /api/v1/admin/jackpot/pools/{pool}/adjustments — 奖池余额调整(须备注,写流水)。
|
|
*/
|
|
final class AdminJackpotPoolAdjustController extends Controller
|
|
{
|
|
use PresentsJackpotPoolAdjustment;
|
|
|
|
public function __construct(
|
|
private readonly JackpotPoolAdjustmentService $adjustments,
|
|
) {}
|
|
|
|
public function __invoke(AdminJackpotPoolAdjustRequest $request, JackpotPool $pool): JsonResponse
|
|
{
|
|
$admin = $request->user();
|
|
if (! $admin instanceof AdminUser) {
|
|
return ApiResponse::error(
|
|
trans('admin.unauthenticated', [], $request->lotteryLocale()),
|
|
ErrorCode::AdminUnauthenticated->value,
|
|
null,
|
|
401,
|
|
);
|
|
}
|
|
|
|
try {
|
|
$row = $this->adjustments->apply(
|
|
$pool,
|
|
$admin,
|
|
(int) $request->validated('amount_delta'),
|
|
(string) $request->validated('reason'),
|
|
$request,
|
|
);
|
|
} catch (\RuntimeException $e) {
|
|
return ApiMessage::runtimeErrorResponse($request, $e, ErrorCode::ClientHttpError->value, 422);
|
|
}
|
|
|
|
$pool->refresh();
|
|
|
|
return ApiResponse::success([
|
|
'adjustment' => $this->adjustmentRow($row),
|
|
'pool' => [
|
|
'id' => (int) $pool->id,
|
|
'currency_code' => $pool->currency_code,
|
|
'current_amount' => (int) $pool->current_amount,
|
|
'updated_at' => $pool->updated_at?->toIso8601String(),
|
|
],
|
|
]);
|
|
}
|
|
}
|