NextJSNetflix/pages/api/random.ts

22 lines
732 B
TypeScript
Raw Normal View History

2023-08-18 12:00:58 +00:00
import { NextApiRequest, NextApiResponse } from "next";
import prismadb from "@/lib/prismadb";
import serverAuth from "@/lib/serverAuth";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
return res.status(405).end
}
try {
await serverAuth(req);
const movieCount = await prismadb.movie.count();
const randomIndex = Math.floor(Math.random() * movieCount);
const randomMovies = await prismadb.movie.findMany({
take: 1,
skip: randomIndex
});
return res.status(200).json(randomMovies[0])
} catch (error) {
console.log(error);
return res.status(400).end()
}
}