- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。 - 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。 - 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Admin\Currency;
|
|
|
|
use App\Models\Currency;
|
|
use App\Lottery\ErrorCode;
|
|
use App\Support\ApiMessage;
|
|
use App\Support\ApiResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\LotterySettings;
|
|
use App\Http\Controllers\Controller;
|
|
|
|
final class AdminCurrencyDestroyController extends Controller
|
|
{
|
|
public function __invoke(Request $request, Currency $currency): JsonResponse
|
|
{
|
|
$code = strtoupper((string) $currency->code);
|
|
|
|
if ($code === LotterySettings::defaultCurrency()) {
|
|
return ApiMessage::errorResponse($request, 'admin.currency_default_cannot_delete', ErrorCode::ValidationFailed->value, null, 422);
|
|
}
|
|
|
|
$references = $this->referenceSummary($code);
|
|
if ($references !== []) {
|
|
return ApiMessage::errorResponse(
|
|
$request,
|
|
'admin.currency_referenced_cannot_delete',
|
|
ErrorCode::ValidationFailed->value,
|
|
['references' => $references],
|
|
422,
|
|
['refs' => implode('、', $references)],
|
|
);
|
|
}
|
|
|
|
$id = (int) $currency->id;
|
|
$currency->delete();
|
|
|
|
return ApiResponse::success([
|
|
'deleted' => true,
|
|
'id' => $id,
|
|
'code' => $code,
|
|
]);
|
|
}
|
|
|
|
/** @return list<string> */
|
|
private function referenceSummary(string $code): array
|
|
{
|
|
$checks = [
|
|
'玩家默认币种' => DB::table('players')->where('default_currency', $code),
|
|
'玩家钱包' => DB::table('player_wallets')->where('currency_code', $code),
|
|
'转账单' => DB::table('transfer_orders')->where('currency_code', $code),
|
|
'注单' => DB::table('ticket_orders')->where('currency_code', $code),
|
|
'赔率配置' => DB::table('odds_items')->where('currency_code', $code),
|
|
'奖池' => DB::table('jackpot_pools')->where('currency_code', $code),
|
|
'Jackpot 贡献记录' => DB::table('jackpot_contributions')->where('currency_code', $code),
|
|
];
|
|
|
|
$references = [];
|
|
foreach ($checks as $label => $query) {
|
|
if ($query->exists()) {
|
|
$references[] = $label;
|
|
}
|
|
}
|
|
|
|
return $references;
|
|
}
|
|
}
|