diff --git a/app/api/calendars/route.ts b/app/api/calendars/route.ts index 70557ba9..ebf25132 100644 --- a/app/api/calendars/route.ts +++ b/app/api/calendars/route.ts @@ -29,6 +29,13 @@ export async function GET(req: NextRequest) { where: { userId: session.user.username, }, + include: { + events: { + orderBy: { + start: 'asc' + } + } + }, orderBy: { createdAt: "desc", }, diff --git a/app/api/events/[id]/route.ts b/app/api/events/[id]/route.ts new file mode 100644 index 00000000..a31a7ecc --- /dev/null +++ b/app/api/events/[id]/route.ts @@ -0,0 +1,43 @@ +import { NextRequest, NextResponse } from "next/server"; +import { getServerSession } from "next-auth/next"; +import { authOptions } from "@/app/api/auth/[...nextauth]/route"; +import { prisma } from "@/lib/prisma"; + +export async function DELETE( + req: NextRequest, + { params }: { params: { id: string } } +) { + const session = await getServerSession(authOptions); + + if (!session?.user?.username) { + return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); + } + + try { + // Verify event ownership + const event = await prisma.event.findFirst({ + where: { + id: params.id, + userId: session.user.username, + }, + }); + + if (!event) { + return NextResponse.json( + { error: "Événement non trouvé ou non autorisé" }, + { status: 404 } + ); + } + + await prisma.event.delete({ + where: { + id: params.id, + }, + }); + + return NextResponse.json({ success: true }); + } catch (error) { + console.error("Erreur lors de la suppression de l'événement:", error); + return NextResponse.json({ error: "Erreur serveur" }, { status: 500 }); + } +} \ No newline at end of file diff --git a/app/api/events/route.ts b/app/api/events/route.ts new file mode 100644 index 00000000..9e40c785 --- /dev/null +++ b/app/api/events/route.ts @@ -0,0 +1,110 @@ +import { NextRequest, NextResponse } from "next/server"; +import { getServerSession } from "next-auth/next"; +import { authOptions } from "@/app/api/auth/[...nextauth]/route"; +import { prisma } from "@/lib/prisma"; + +export async function POST(req: NextRequest) { + const session = await getServerSession(authOptions); + + if (!session?.user?.username) { + return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); + } + + try { + const { title, description, start, end, allDay, location, calendarId } = await req.json(); + + // Validation + if (!title || !start || !end || !calendarId) { + return NextResponse.json( + { error: "Titre, début, fin et calendrier sont requis" }, + { status: 400 } + ); + } + + // Verify calendar ownership + const calendar = await prisma.calendar.findFirst({ + where: { + id: calendarId, + userId: session.user.username, + }, + }); + + if (!calendar) { + return NextResponse.json( + { error: "Calendrier non trouvé ou non autorisé" }, + { status: 404 } + ); + } + + const event = await prisma.event.create({ + data: { + title, + description, + start: new Date(start), + end: new Date(end), + isAllDay: allDay || false, + location, + calendarId, + userId: session.user.username, + }, + }); + + return NextResponse.json(event, { status: 201 }); + } catch (error) { + console.error("Erreur lors de la création de l'événement:", error); + return NextResponse.json({ error: "Erreur serveur" }, { status: 500 }); + } +} + +export async function PUT(req: NextRequest) { + const session = await getServerSession(authOptions); + + if (!session?.user?.username) { + return NextResponse.json({ error: "Non authentifié" }, { status: 401 }); + } + + try { + const { id, title, description, start, end, allDay, location, calendarId } = await req.json(); + + // Validation + if (!id || !title || !start || !end || !calendarId) { + return NextResponse.json( + { error: "ID, titre, début, fin et calendrier sont requis" }, + { status: 400 } + ); + } + + // Verify event ownership + const existingEvent = await prisma.event.findFirst({ + where: { + id, + userId: session.user.username, + }, + }); + + if (!existingEvent) { + return NextResponse.json( + { error: "Événement non trouvé ou non autorisé" }, + { status: 404 } + ); + } + + const event = await prisma.event.update({ + where: { id }, + data: { + title, + description, + start: new Date(start), + end: new Date(end), + isAllDay: allDay || false, + location, + calendarId, + }, + }); + + return NextResponse.json(event); + } catch (error) { + console.error("Erreur lors de la mise à jour de l'événement:", error); + return NextResponse.json({ error: "Erreur serveur" }, { status: 500 }); + } +} \ No newline at end of file diff --git a/components/calendar/calendar-client.tsx b/components/calendar/calendar-client.tsx index 854545e2..5fc18a09 100644 --- a/components/calendar/calendar-client.tsx +++ b/components/calendar/calendar-client.tsx @@ -446,6 +446,7 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend const response = await fetch("/api/calendars"); if (!response.ok) throw new Error("Failed to fetch calendars"); const data = await response.json(); + console.log("Fetched calendars:", data); // Debug log setCalendars(data.map((cal: Calendar & { events: Event[] }) => ({ ...cal, events: cal.events || [] @@ -597,6 +598,26 @@ export function CalendarClient({ initialCalendars, userId, userProfile }: Calend } }; + // Add a calendar selector component + const CalendarSelector = () => ( +