- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。 - 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。 - 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
|
|
|
use App\Lottery\ErrorCode;
|
|
use App\Support\ApiMessage;
|
|
use App\Support\ApiResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Draw\DrawDestroyService;
|
|
|
|
final class AdminDrawBatchDestroyController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly DrawDestroyService $service,
|
|
) {}
|
|
|
|
public function __invoke(Request $request): JsonResponse
|
|
{
|
|
$drawIds = $request->input('draw_ids', []);
|
|
|
|
if (!is_array($drawIds) || empty($drawIds)) {
|
|
return ApiMessage::errorResponse($request, 'invalid_params', ErrorCode::ClientHttpError->value, [], 400);
|
|
}
|
|
|
|
$results = [
|
|
'success' => [],
|
|
'failed' => [],
|
|
];
|
|
|
|
foreach ($drawIds as $drawId) {
|
|
try {
|
|
$draw = \App\Models\Draw::findOrFail($drawId);
|
|
$this->service->destroy($draw);
|
|
$results['success'][] = $drawId;
|
|
} catch (\RuntimeException $e) {
|
|
$results['failed'][] = [
|
|
'id' => $drawId,
|
|
'reason' => ApiMessage::reason($request, $e->getMessage()),
|
|
'reason_key' => $e->getMessage(),
|
|
];
|
|
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
|
$results['failed'][] = [
|
|
'id' => $drawId,
|
|
'reason' => ApiMessage::get($request, 'draw_not_found'),
|
|
'reason_key' => 'draw_not_found',
|
|
];
|
|
}
|
|
}
|
|
|
|
return ApiResponse::success($results);
|
|
}
|
|
}
|