컴포넌트
로그인이 안되어있으면 Reserve 버튼 눌렀을때 로그인 모달 뜸.
실제 POST요청을 날리는 코드
import { NextResponse } from 'next/server';
import prisma from '@/app/libs/prismadb';
import getCurrentUser from '@/app/actions/getCurrentUser';
export async function POST(request: Request) {
const currentUser = await getCurrentUser();
if (!currentUser) {
return NextResponse.error();
}
const body = await request.json();
const { listingId, startDate, endDate, totalPrice } = body;
if (!listingId || !startDate || !endDate || !totalPrice) {
return NextResponse.error();
}
const listingAndReservation = await prisma.listing.update({
where: {
id: listingId,
},
data: {
reservations: {
create: {
userId: currentUser.id,
startDate,
endDate,
totalPrice,
},
},
},
});
return NextResponse.json(listingAndReservation);
}
import prisma from '@/app/libs/prismadb';
interface IParams {
listingId?: string;
userId?: string;
authorId?: string;
}
export default async function getReservations(params: IParams) {
const { listingId, userId, authorId } = params;
try {
const query: any = {};
if (listingId) {
query.listingId = listingId;
}
if (userId) {
query.userId = userId;
}
if (authorId) {
query.listing = { userId: authorId };
}
const reservations = await prisma.reservation.findMany({
where: query,
include: {
listing: true,
},
orderBy: {
createdAt: 'desc',
},
});
const safeReservations = reservations.map((reservation) => ({
...reservation,
createdAt: reservation.createdAt.toISOString(),
startDate: reservation.startDate.toISOString(),
endDate: reservation.endDate.toISOString(),
listing: {
...reservation.listing,
createdAt: reservation.listing.createdAt.toISOString(),
},
}));
return safeReservations;
} catch (error: any) {
throw new Error(error);
}
}