@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { loginUser, deleteSession, getServerSession, SESSION_COOKIE } from "@/lib/auth";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { nickname, password } = await req.json();
|
||||
if (!nickname || !password) {
|
||||
return NextResponse.json({ error: "Nickname and password required" }, { status: 400 });
|
||||
}
|
||||
const result = await loginUser(nickname, password);
|
||||
if ("error" in result) {
|
||||
return NextResponse.json(result, { status: 401 });
|
||||
}
|
||||
const res = NextResponse.json(result.user);
|
||||
res.cookies.set(SESSION_COOKIE, result.sessionId, {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
maxAge: 7 * 86400,
|
||||
});
|
||||
return res;
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Login failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE() {
|
||||
const session = await getServerSession();
|
||||
if (session) {
|
||||
const cookieStore = await cookies();
|
||||
const token = cookieStore.get(SESSION_COOKIE)?.value;
|
||||
if (token) await deleteSession(token);
|
||||
}
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set(SESSION_COOKIE, "", { httpOnly: true, path: "/", maxAge: 0 });
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ user: null });
|
||||
}
|
||||
return NextResponse.json({ user: { id: session.id, nickname: session.nickname, isAdmin: session.isAdmin } });
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { registerUser } from "@/lib/auth";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { nickname, password } = await req.json();
|
||||
if (!nickname || !password) {
|
||||
return NextResponse.json({ error: "Nickname and password required" }, { status: 400 });
|
||||
}
|
||||
if (nickname.length < 2 || nickname.length > 20) {
|
||||
return NextResponse.json({ error: "Nickname 2-20 characters" }, { status: 400 });
|
||||
}
|
||||
if (password.length < 4) {
|
||||
return NextResponse.json({ error: "Password min 4 characters" }, { status: 400 });
|
||||
}
|
||||
const result = await registerUser(nickname, password);
|
||||
if ("error" in result) {
|
||||
return NextResponse.json(result, { status: 409 });
|
||||
}
|
||||
return NextResponse.json(result);
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Registration failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { bingoItems } from "@/lib/db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
const { campaignId } = await params;
|
||||
const items = db.select().from(bingoItems).where(eq(bingoItems.campaignId, campaignId)).orderBy(bingoItems.gridIndex).all();
|
||||
return NextResponse.json(items);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
try {
|
||||
const { campaignId } = await params;
|
||||
const { text, emoji, soundCategory, soundUrl, gridIndex } = await req.json();
|
||||
if (!text) return NextResponse.json({ error: "Text required" }, { status: 400 });
|
||||
|
||||
const existing = db.select().from(bingoItems)
|
||||
.where(and(eq(bingoItems.campaignId, campaignId), eq(bingoItems.gridIndex, gridIndex)))
|
||||
.get();
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: "Grid position taken" }, { status: 409 });
|
||||
}
|
||||
|
||||
const id = uuidv4();
|
||||
const now = new Date().toISOString();
|
||||
db.insert(bingoItems).values({ id, campaignId, text, emoji: emoji || "💀", soundCategory: soundCategory || "horn", soundUrl: soundUrl || null, gridIndex, createdAt: now }).run();
|
||||
return NextResponse.json({ id });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Failed to add item" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
try {
|
||||
const { campaignId } = await params;
|
||||
const body = await req.json();
|
||||
if (!body.id) return NextResponse.json({ error: "Item ID required" }, { status: 400 });
|
||||
const updates: Record<string, unknown> = {};
|
||||
if (body.text) updates.text = body.text;
|
||||
if (body.emoji) updates.emoji = body.emoji;
|
||||
if (body.soundCategory) updates.soundCategory = body.soundCategory;
|
||||
if (body.soundUrl !== undefined) updates.soundUrl = body.soundUrl;
|
||||
if (body.gridIndex !== undefined) updates.gridIndex = body.gridIndex;
|
||||
db.update(bingoItems).set(updates)
|
||||
.where(and(eq(bingoItems.id, body.id), eq(bingoItems.campaignId, campaignId)))
|
||||
.run();
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Update failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
try {
|
||||
const { campaignId } = await params;
|
||||
const url = new URL(req.url);
|
||||
const itemId = url.searchParams.get("itemId");
|
||||
if (!itemId) return NextResponse.json({ error: "itemId required" }, { status: 400 });
|
||||
db.delete(bingoItems)
|
||||
.where(and(eq(bingoItems.id, itemId), eq(bingoItems.campaignId, campaignId)))
|
||||
.run();
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Delete failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { campaigns } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export async function DELETE(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
const { campaignId } = await params;
|
||||
db.delete(campaigns).where(eq(campaigns.id, campaignId)).run();
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
try {
|
||||
const { campaignId } = await params;
|
||||
const body = await req.json();
|
||||
const updates: Record<string, unknown> = {};
|
||||
if (body.name) updates.name = body.name;
|
||||
if (body.status) updates.status = body.status;
|
||||
if (body.gridSize) updates.gridSize = body.gridSize;
|
||||
db.update(campaigns).set(updates).where(eq(campaigns.id, campaignId)).run();
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Update failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { campaigns } from "@/lib/db/schema";
|
||||
import { desc } from "drizzle-orm";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const all = db.select().from(campaigns).orderBy(desc(campaigns.createdAt)).all();
|
||||
return NextResponse.json(all);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { name, gridSize = 5 } = await req.json();
|
||||
if (!name) return NextResponse.json({ error: "Name required" }, { status: 400 });
|
||||
if (gridSize < 3 || gridSize > 10) {
|
||||
return NextResponse.json({ error: "Grid size 3-10" }, { status: 400 });
|
||||
}
|
||||
|
||||
const id = uuidv4();
|
||||
const now = new Date().toISOString();
|
||||
db.insert(campaigns).values({ id, name, gridSize, createdBy: session.id, createdAt: now }).run();
|
||||
|
||||
return NextResponse.json({ id, name, gridSize });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Failed to create campaign" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { campaigns, bingoItems, marks, users } from "@/lib/db/schema";
|
||||
import { eq, inArray, desc } from "drizzle-orm";
|
||||
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
||||
const session = await getServerSession();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const { campaignId } = await params;
|
||||
const campaign = db.select().from(campaigns).where(eq(campaigns.id, campaignId)).get();
|
||||
if (!campaign) return NextResponse.json({ error: "Campaign not found" }, { status: 404 });
|
||||
|
||||
const items = db.select().from(bingoItems).where(eq(bingoItems.campaignId, campaignId)).orderBy(bingoItems.gridIndex).all();
|
||||
const allMarks = db.select().from(marks).where(eq(marks.campaignId, campaignId)).orderBy(desc(marks.createdAt)).all();
|
||||
|
||||
const userIds = [...new Set(allMarks.map(m => m.userId))];
|
||||
const userMap: Record<string, string> = {};
|
||||
if (userIds.length > 0) {
|
||||
const markedUsers = db.select({ id: users.id, nickname: users.nickname })
|
||||
.from(users).where(inArray(users.id, userIds)).all();
|
||||
markedUsers.forEach(u => { userMap[u.id] = u.nickname; });
|
||||
}
|
||||
|
||||
const itemMap: Record<string, typeof items[0]> = {};
|
||||
items.forEach(it => { itemMap[it.id] = it; });
|
||||
|
||||
const markedItemIds = allMarks.map(m => m.itemId);
|
||||
const markCountMap: Record<string, number> = {};
|
||||
const markUsersMap: Record<string, string[]> = {};
|
||||
for (const m of allMarks) {
|
||||
markCountMap[m.itemId] = (markCountMap[m.itemId] || 0) + 1;
|
||||
if (!markUsersMap[m.itemId]) markUsersMap[m.itemId] = [];
|
||||
if (!markUsersMap[m.itemId].includes(userMap[m.userId] || "???")) {
|
||||
markUsersMap[m.itemId].push(userMap[m.userId] || "???");
|
||||
}
|
||||
}
|
||||
|
||||
const activityLog = allMarks.slice(0, 20).map(m => {
|
||||
const nickname = userMap[m.userId] || "???";
|
||||
const item = itemMap[m.itemId];
|
||||
const emoji = item?.emoji || "💀";
|
||||
const text = item?.text || "unknown";
|
||||
const time = new Date(m.createdAt).toLocaleTimeString();
|
||||
return `${time} ${nickname} marked ${emoji} "${text}"`;
|
||||
});
|
||||
|
||||
const totalCells = campaign.gridSize * campaign.gridSize;
|
||||
const grid: Array<{
|
||||
index: number;
|
||||
item: typeof items[0] | null;
|
||||
marked: boolean;
|
||||
markedBy: string[];
|
||||
markCount: number;
|
||||
}> = [];
|
||||
|
||||
for (let i = 0; i < totalCells; i++) {
|
||||
const item = items.find(it => it.gridIndex === i) || null;
|
||||
grid.push({
|
||||
index: i,
|
||||
item,
|
||||
marked: markedItemIds.includes(item?.id || ""),
|
||||
markedBy: markUsersMap[item?.id || ""] || [],
|
||||
markCount: markCountMap[item?.id || ""] || 0,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ campaign, grid, activityLog });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Failed to load game state" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { marks, bingoItems } from "@/lib/db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const { campaignId, itemId } = await req.json();
|
||||
if (!campaignId || !itemId) {
|
||||
return NextResponse.json({ error: "campaignId and itemId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const item = db.select().from(bingoItems)
|
||||
.where(and(eq(bingoItems.id, itemId), eq(bingoItems.campaignId, campaignId)))
|
||||
.get();
|
||||
if (!item) return NextResponse.json({ error: "Item not found" }, { status: 404 });
|
||||
|
||||
const existing = db.select().from(marks)
|
||||
.where(and(eq(marks.campaignId, campaignId), eq(marks.itemId, itemId)))
|
||||
.get();
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: "Already marked", mark: existing }, { status: 409 });
|
||||
}
|
||||
|
||||
const id = uuidv4();
|
||||
const now = new Date().toISOString();
|
||||
db.insert(marks).values({ id, campaignId, itemId, userId: session.id, createdAt: now }).run();
|
||||
|
||||
return NextResponse.json({ id, markedBy: session.nickname });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Failed to mark" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
const session = await getServerSession();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const { campaignId, itemId } = await req.json();
|
||||
if (!campaignId || !itemId) {
|
||||
return NextResponse.json({ error: "campaignId and itemId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
db.delete(marks)
|
||||
.where(and(eq(marks.campaignId, campaignId), eq(marks.itemId, itemId)))
|
||||
.run();
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Failed to unmark" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getServerSession } from "@/lib/auth";
|
||||
import { writeFile, mkdir } from "fs/promises";
|
||||
import path from "path";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession();
|
||||
if (!session || !session.isAdmin) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const formData = await req.formData();
|
||||
const file = formData.get("file") as File | null;
|
||||
if (!file) return NextResponse.json({ error: "No file" }, { status: 400 });
|
||||
|
||||
if (!file.name.toLowerCase().endsWith(".ogg")) {
|
||||
return NextResponse.json({ error: "Only OGG files allowed" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (file.size > 2 * 1024 * 1024) {
|
||||
return NextResponse.json({ error: "File too large (max 2MB)" }, { status: 400 });
|
||||
}
|
||||
|
||||
await mkdir(UPLOAD_DIR, { recursive: true });
|
||||
const ext = path.extname(file.name);
|
||||
const filename = `${uuidv4()}${ext}`;
|
||||
const filepath = path.join(UPLOAD_DIR, filename);
|
||||
const bytes = await file.arrayBuffer();
|
||||
await writeFile(filepath, Buffer.from(bytes));
|
||||
|
||||
return NextResponse.json({ url: `/uploads/sounds/${filename}` });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user