From f971aa4e699b3bbe8f3f58932776f05fc5f4df82 Mon Sep 17 00:00:00 2001 From: AeCw Date: Wed, 4 Feb 2026 17:33:28 +0800 Subject: [PATCH] update --- README.md | 3 + app/api/admin/init-database/route.ts | 32 + app/api/admin/set-config/route.ts | 49 ++ app/api/config/route.ts | 19 + app/api/config/routes.ts | 92 --- app/debug/api/tournament-config/route.ts | 62 +- app/debug/components/TournamentConfigCard.tsx | 24 +- app/lib/PrismaClient.ts | 30 +- app/lib/UserOperation.ts | 28 + package-lock.json | 637 +++--------------- package.json | 3 +- prisma.config.ts | 12 +- prisma/models/User.prisma | 2 +- prisma/schema.prisma | 3 +- 14 files changed, 272 insertions(+), 724 deletions(-) create mode 100644 app/api/admin/init-database/route.ts create mode 100644 app/api/admin/set-config/route.ts create mode 100644 app/api/config/route.ts delete mode 100644 app/api/config/routes.ts diff --git a/README.md b/README.md index 49baf41..9af0fd7 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ `npx prisma migrate dev --name init` `npx prisma migrate dev` +Generate Prisma Client +`npx prisma generate` + ### arch笔记本mysql测试服务器 `` diff --git a/app/api/admin/init-database/route.ts b/app/api/admin/init-database/route.ts new file mode 100644 index 0000000..3c25802 --- /dev/null +++ b/app/api/admin/init-database/route.ts @@ -0,0 +1,32 @@ +import { NextResponse } from 'next/server'; +import { prisma } from '@/app/lib/PrismaClient'; + +// TODO: admin 验证 + +// INIT: 初始化配置 仅主办使用 +export async function GET() { + try { + let config = await prisma.tournamentConfig.findUnique({ + where: { id: 1 }, + }); + if (!config) { + config = await prisma.tournamentConfig.create({ + data: { + tournament_name: 'Astar Cup', + max_pp_for_registration: 1000, + min_pp_for_registration: 0, + current_seasonal: 'S1', + current_category: 'QUA', + canRegister: false, + }, + }); + } + return NextResponse.json(config); + } catch (error) { + console.error('初始化比赛配置失败:', error); + return NextResponse.json( + { error: '初始化配置失败' }, + { status: 500 } + ); + } +} diff --git a/app/api/admin/set-config/route.ts b/app/api/admin/set-config/route.ts new file mode 100644 index 0000000..9046ade --- /dev/null +++ b/app/api/admin/set-config/route.ts @@ -0,0 +1,49 @@ +import { NextRequest, NextResponse } from 'next/server'; +import { prisma } from '@/app/lib/PrismaClient'; +// PUT: 更新配置 +export async function PUT(request: NextRequest) { + + try { + // TODO: 鉴权 + + const data = await request.json(); + + // 验证必要字段 + const requiredFields = [ + 'tournament_name', + 'max_pp_for_registration', + 'min_pp_for_registration', + 'current_seasonal', + 'current_category', + 'canRegister', + ]; + + for (const field of requiredFields) { + if (data[field] === undefined) { + return NextResponse.json( + { error: `缺少必要字段: ${field}` }, + { status: 400 } + ); + } + } + + // 更新或创建配置 + const config = await prisma.tournamentConfig.upsert({ + where: { id: 1 }, + update: data, + create: { + id: 1, + ...data, + }, + }); + + return NextResponse.json(config); + } catch (error) { + console.error('更新比赛配置失败:', error); + return NextResponse.json( + { error: '更新配置失败' }, + { status: 500 } + ); + } +} + diff --git a/app/api/config/route.ts b/app/api/config/route.ts new file mode 100644 index 0000000..e3d56f7 --- /dev/null +++ b/app/api/config/route.ts @@ -0,0 +1,19 @@ +import { NextResponse } from 'next/server'; +import { prisma } from '@/app/lib/PrismaClient'; + + +export async function GET() { + try { + const config = await prisma.tournamentConfig.findUnique({ + where: { id: 1 }, + }); + return NextResponse.json(config); + } catch (error) { + console.error('获取比赛配置失败:', error); + return NextResponse.json( + { error: '获取配置失败' }, + { status: 500 } + ); + } +} + diff --git a/app/api/config/routes.ts b/app/api/config/routes.ts deleted file mode 100644 index c29dc66..0000000 --- a/app/api/config/routes.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { NextRequest, NextResponse } from 'next/server'; -import { prisma } from '@/app/lib/PrismaClient'; - -// TODO: admin 验证 - -// GET: 获取配置 -export async function GET() { - try { - let config = await prisma.tournamentConfig.findUnique({ - where: { id: 1 }, - }); - return NextResponse.json(config); - } catch (error) { - console.error('获取比赛配置失败:', error); - return NextResponse.json( - { error: '获取配置失败' }, - { status: 500 } - ); - } -} - -// PUT: 更新配置 需要管理验证 -export async function PUT(request: NextRequest) { - try { - const data = await request.json(); - - // 验证必要字段 - const requiredFields = [ - 'tournament_name', - 'max_pp_for_registration', - 'min_pp_for_registration', - 'current_seasonal', - 'current_category', - 'canRegister', - ]; - - for (const field of requiredFields) { - if (data[field] === undefined) { - return NextResponse.json( - { error: `缺少必要字段: ${field}` }, - { status: 400 } - ); - } - } - - // 更新或创建配置 - const config = await prisma.tournamentConfig.upsert({ - where: { id: 1 }, - update: data, - create: { - id: 1, - ...data, - }, - }); - - return NextResponse.json(config); - } catch (error) { - console.error('更新比赛配置失败:', error); - return NextResponse.json( - { error: '更新配置失败' }, - { status: 500 } - ); - } -} - -// INIT: 初始化配置 仅主办使用 -export async function INIT() { - try { - let config = await prisma.tournamentConfig.findUnique({ - where: { id: 1 }, - }); - if (!config) { - config = await prisma.tournamentConfig.create({ - data: { - tournament_name: 'Astar Cup', - max_pp_for_registration: 1000, - min_pp_for_registration: 0, - current_seasonal: 'S1', - current_category: 'QUA', - canRegister: false, - }, - }); - } - return NextResponse.json(config); - } catch (error) { - console.error('初始化比赛配置失败:', error); - return NextResponse.json( - { error: '初始化配置失败' }, - { status: 500 } - ); - } -} diff --git a/app/debug/api/tournament-config/route.ts b/app/debug/api/tournament-config/route.ts index 19edbf6..898d3e0 100644 --- a/app/debug/api/tournament-config/route.ts +++ b/app/debug/api/tournament-config/route.ts @@ -1,27 +1,13 @@ -import { NextRequest, NextResponse } from 'next/server'; +import { NextResponse } from 'next/server'; import { prisma } from '@/app/lib/PrismaClient'; // GET: 获取配置 export async function GET() { try { - let config = await prisma.tournamentConfig.findUnique({ + const config = await prisma.tournamentConfig.findUnique({ where: { id: 1 }, }); - if (!config) { - config = await prisma.tournamentConfig.create({ - data: { - id: 1, - tournament_name: 'AstarCup', - max_pp_for_registration: 0, - min_pp_for_registration: 0, - current_seasonal: 'S1', - current_category: 'QUA', - canRegister: false, - }, - }); - } - return NextResponse.json(config); } catch (error) { console.error('获取比赛配置失败:', error); @@ -31,47 +17,3 @@ export async function GET() { ); } } - -// PUT: 更新配置 -export async function PUT(request: NextRequest) { - try { - const data = await request.json(); - - // 验证必要字段 - const requiredFields = [ - 'tournament_name', - 'max_pp_for_registration', - 'min_pp_for_registration', - 'current_seasonal', - 'current_category', - 'canRegister', - ]; - - for (const field of requiredFields) { - if (data[field] === undefined) { - return NextResponse.json( - { error: `缺少必要字段: ${field}` }, - { status: 400 } - ); - } - } - - // 更新或创建配置 - const config = await prisma.tournamentConfig.upsert({ - where: { id: 1 }, - update: data, - create: { - id: 1, - ...data, - }, - }); - - return NextResponse.json(config); - } catch (error) { - console.error('更新比赛配置失败:', error); - return NextResponse.json( - { error: '更新配置失败' }, - { status: 500 } - ); - } -} diff --git a/app/debug/components/TournamentConfigCard.tsx b/app/debug/components/TournamentConfigCard.tsx index 8b1de08..fdbdc35 100644 --- a/app/debug/components/TournamentConfigCard.tsx +++ b/app/debug/components/TournamentConfigCard.tsx @@ -58,6 +58,19 @@ export default function TournamentConfigCard({ }); }; + const initConfig = async () => { + try { + const response = await fetch('/api/admin/init-database', { + method: 'GET', + }); + const data = await response.json(); + setConfig(data); + initializeForm(data); + } catch (error) { + console.error('初始化比赛配置失败:', error); + } + } + // 加载配置 const loadConfig = async () => { setLoading(true); @@ -65,7 +78,7 @@ export default function TournamentConfigCard({ try { // 这里需要调用 API 路由 - const response = await fetch('/debug/api/tournament-config'); + const response = await fetch('/api/config',{ method:'GET',}); if (!response.ok) { throw new Error(`加载失败: ${response.status}`); } @@ -97,7 +110,7 @@ export default function TournamentConfigCard({ setSuccess(null); try { - const response = await fetch('/debug/api/tournament-config', { + const response = await fetch('/api/admin/set-config', { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -177,6 +190,13 @@ export default function TournamentConfigCard({ > 刷新 +