Add database and Prisma configurations, enhance event and leaderboard components with API integration, and update navigation for session management
This commit is contained in:
65
app/api/preferences/route.ts
Normal file
65
app/api/preferences/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { Role } from "@/prisma/generated/prisma/client";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
|
||||
}
|
||||
|
||||
const preferences = await prisma.userPreferences.findUnique({
|
||||
where: { userId: session.user.id },
|
||||
});
|
||||
|
||||
return NextResponse.json(preferences || {});
|
||||
} catch (error) {
|
||||
console.error("Error fetching preferences:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la récupération des préférences" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: Request) {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { homeBackground, eventsBackground, leaderboardBackground, theme } =
|
||||
body;
|
||||
|
||||
const preferences = await prisma.userPreferences.upsert({
|
||||
where: { userId: session.user.id },
|
||||
update: {
|
||||
homeBackground: homeBackground ?? undefined,
|
||||
eventsBackground: eventsBackground ?? undefined,
|
||||
leaderboardBackground: leaderboardBackground ?? undefined,
|
||||
theme: theme ?? undefined,
|
||||
},
|
||||
create: {
|
||||
userId: session.user.id,
|
||||
homeBackground: homeBackground ?? null,
|
||||
eventsBackground: eventsBackground ?? null,
|
||||
leaderboardBackground: leaderboardBackground ?? null,
|
||||
theme: theme ?? "default",
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(preferences);
|
||||
} catch (error) {
|
||||
console.error("Error updating preferences:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur lors de la mise à jour des préférences" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user