Files
wzdj/app/api/admin/guilds/route.ts

104 lines
3.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server"
import { config } from "dotenv"
import { resolve } from "path"
import { ObjectId } from "mongodb"
import { getCollection } from "@/lib/db/mongo"
if (typeof process !== "undefined" && !process.env.MONGODB_URI) {
config({ path: resolve(process.cwd(), ".env.local") })
}
const now = () => new Date().toISOString()
async function ensureDefaultGuilds() {
const coll = await getCollection("guilds")
const defaults = [
{ name: "玩值电竞", description: "玩值电竞官方公会", tags: ["官方", "电竞"], status: "active" as const },
{ name: "丸子公会", description: "丸子公会,星探与主播培养", tags: ["公会", "主播"], status: "active" as const },
]
for (const d of defaults) {
const exists = await coll.findOne({ name: d.name })
if (!exists) {
await coll.insertOne({
name: d.name,
logo: "",
coverImage: "",
description: d.description,
ownerId: "",
memberCount: 0,
maxMembers: 5000,
level: 10,
totalIncome: 0,
commissionRate: 0.1,
tags: d.tags,
games: ["魔兽世界", "英雄联盟", "王者荣耀"],
requirements: "",
benefits: ["流量扶持", "课程分成"],
status: d.status,
createdAt: now(),
updatedAt: now(),
})
}
}
}
export async function GET() {
try {
await ensureDefaultGuilds()
const coll = await getCollection("guilds")
const streamersColl = await getCollection("streamers")
const list = await coll.find({}).sort({ updatedAt: -1, createdAt: -1 }).limit(200).toArray()
const data = await Promise.all(
list.map(async (doc: Record<string, unknown> & { _id: ObjectId }) => {
const id = doc._id.toString()
const streamerCount = await streamersColl.countDocuments({ guildId: id })
return {
...doc,
id,
streamerCount: streamerCount ?? 0,
memberCount: doc.memberCount ?? 0,
maxMembers: doc.maxMembers ?? 5000,
level: doc.level ?? 0,
totalIncome: doc.totalIncome ?? 0,
commissionRate: doc.commissionRate ?? 0.1,
}
}),
)
return NextResponse.json({ success: true, data })
} catch (e) {
console.error("GET /api/admin/guilds error:", e)
return NextResponse.json({ success: false, error: String(e) }, { status: 500 })
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const coll = await getCollection("guilds")
const doc = {
name: body.name ?? "未命名公会",
logo: body.logo ?? "",
coverImage: body.coverImage ?? "",
description: body.description ?? "",
ownerId: body.ownerId ?? "",
memberCount: 0,
maxMembers: body.maxMembers ?? 5000,
level: body.level ?? 1,
totalIncome: 0,
commissionRate: typeof body.commissionRate === "number" ? body.commissionRate : 0.1,
tags: Array.isArray(body.tags) ? body.tags : [],
games: Array.isArray(body.games) ? body.games : [],
requirements: body.requirements ?? "",
benefits: Array.isArray(body.benefits) ? body.benefits : [],
status: body.status ?? "active",
createdAt: now(),
updatedAt: now(),
}
const r = await coll.insertOne(doc)
return NextResponse.json({ success: true, data: { id: r.insertedId.toString() } })
} catch (e) {
console.error("POST /api/admin/guilds error:", e)
return NextResponse.json({ success: false, error: String(e) }, { status: 500 })
}
}