Initial commit: Kalendartool (Next.js, Prisma, Docker)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
79
app/api/calendars/[id]/route.ts
Normal file
79
app/api/calendars/[id]/route.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* GET /api/calendars/[id] - Kalenderdetails (Owner oder Member).
|
||||
* PATCH /api/calendars/[id] - bearbeiten (nur besitzender Admin).
|
||||
*/
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db/client';
|
||||
import { requireCalendarContext } from '@/lib/api/context';
|
||||
import { updateCalendarSchema } from '@/lib/validation';
|
||||
import { canManageCalendar } from '@/lib/permissions/permissions';
|
||||
import { errorResponse, forbiddenResponse } from '@/lib/api/responses';
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
_request: NextRequest,
|
||||
{ params }: RouteParams,
|
||||
): Promise<NextResponse> {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const context = await requireCalendarContext(id);
|
||||
|
||||
return NextResponse.json({ calendar: context.calendar });
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: RouteParams,
|
||||
): Promise<NextResponse> {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const context = await requireCalendarContext(id);
|
||||
|
||||
if (!canManageCalendar(context.user, context.calendar)) {
|
||||
return forbiddenResponse();
|
||||
}
|
||||
|
||||
const body = updateCalendarSchema.parse(await request.json());
|
||||
|
||||
const calendar = await prisma.calendar.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(body.title !== undefined ? { title: body.title } : {}),
|
||||
...(body.description !== undefined
|
||||
? { description: body.description }
|
||||
: {}),
|
||||
...(body.timezone !== undefined ? { timezone: body.timezone } : {}),
|
||||
...(body.isActive !== undefined ? { isActive: body.isActive } : {}),
|
||||
...(body.maxReservationMinutes !== undefined
|
||||
? { maxReservationMinutes: body.maxReservationMinutes }
|
||||
: {}),
|
||||
...(body.showTitleToMembers !== undefined
|
||||
? { showTitleToMembers: body.showTitleToMembers }
|
||||
: {}),
|
||||
...(body.showNotesToMembers !== undefined
|
||||
? { showNotesToMembers: body.showNotesToMembers }
|
||||
: {}),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
description: true,
|
||||
timezone: true,
|
||||
isActive: true,
|
||||
maxReservationMinutes: true,
|
||||
showTitleToMembers: true,
|
||||
showNotesToMembers: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ calendar });
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user