Refactor Study Desk application structure

This commit is contained in:
Elijah 2026-08-07 19:31:23 -07:00
parent faaccf8a7e
commit 089439ed90
145 changed files with 8087 additions and 3412 deletions

View file

@ -1,20 +1,21 @@
import { NextRequest, NextResponse } from "next/server";
import { toggleShareLink, getShareLinkForContent, isContentSharedViaGroup } from "@/services/shareService";
import { shareTargetSchema } from "@/lib/validation/shareSchemas";
import {
ShareTargetNotFoundError,
getShareLinkForContent,
isContentSharedViaGroup,
toggleShareLink,
} from "@/services/shareService";
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const targetType = searchParams.get("targetType") as "DECK" | "QUIZ" | "GROUP";
const contentId = searchParams.get("contentId");
if (!targetType || !contentId) {
return NextResponse.json(
{ error: "targetType and contentId are required" },
{ status: 400 }
);
const parsed = shareTargetSchema.safeParse(
Object.fromEntries(request.nextUrl.searchParams)
);
if (!parsed.success) {
return NextResponse.json({ error: "Invalid share target" }, { status: 400 });
}
const { targetType, contentId } = parsed.data;
const link = await getShareLinkForContent(targetType, contentId);
if (targetType === "DECK" || targetType === "QUIZ") {
const groupShare = await isContentSharedViaGroup(targetType, contentId);
if (groupShare) {
@ -25,20 +26,27 @@ export async function GET(request: NextRequest) {
});
}
}
return NextResponse.json({ token: link?.id || null, isGroupShared: false });
return NextResponse.json({ token: link?.id ?? null, isGroupShared: false });
}
export async function POST(request: NextRequest) {
const body = await request.json().catch(() => null);
if (!body?.targetType || !body?.contentId) {
return NextResponse.json(
{ error: "targetType and contentId are required" },
{ status: 400 }
);
const parsed = shareTargetSchema.safeParse(
await request.json().catch(() => null)
);
if (!parsed.success) {
return NextResponse.json({ error: "Invalid share target" }, { status: 400 });
}
try {
const link = await toggleShareLink(
parsed.data.targetType,
parsed.data.contentId
);
return NextResponse.json({ token: link?.id ?? null });
} catch (error) {
if (error instanceof ShareTargetNotFoundError) {
return NextResponse.json({ error: error.message }, { status: 404 });
}
console.error("Failed to update share link", error);
return NextResponse.json({ error: "Failed to update sharing" }, { status: 500 });
}
const link = await toggleShareLink(body.targetType as "DECK" | "QUIZ" | "GROUP", body.contentId);
return NextResponse.json({ token: link?.id || null });
}