Skip to content

Commit

Permalink
Added shorts functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
teachmetw committed Sep 12, 2023
1 parent d92c51f commit 39505e2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
9 changes: 5 additions & 4 deletions hooks/useMovieList.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import useSwr from 'swr'
import useSwr from 'swr';
import fetcher from '@/libs/fetcher';

const useMovies = () => {
const { data, error, isLoading } = useSwr('/api/movies', fetcher, {
const useMovies = (classifier = 'movie') => {
const endpoint = classifier === 'movie' ? '/api/movies' : `/api/movies?classifier=${classifier}`;
const { data, error, isLoading } = useSwr(endpoint, fetcher, {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
Expand All @@ -11,7 +12,7 @@ const useMovies = () => {
data,
error,
isLoading
}
};
};

export default useMovies;
19 changes: 17 additions & 2 deletions pages/api/movies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

await serverAuth(req, res);

const movies = await prismadb.movie.findMany();
const classifier = req.query.classifier;

let queryOptions = {
where: {
classifier: 'movie'
}
};
if (classifier) {
queryOptions = {
where: {
classifier: classifier as string
}
};
}

const movies = await prismadb.movie.findMany(queryOptions);

return res.status(200).json(movies);
} catch (error) {
console.log({ error })
console.log({ error });
return res.status(500).end();
}
}
9 changes: 8 additions & 1 deletion pages/api/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

await serverAuth(req, res);

const moviesCount = await prismadb.movie.count();
const moviesCount = await prismadb.movie.count({
where: {
classifier: "movie"
}
});
const randomIndex = Math.floor(Math.random() * moviesCount);

const randomMovies = await prismadb.movie.findMany({
where: {
classifier: "movie"
},
take: 1,
skip: randomIndex
});
Expand Down
5 changes: 4 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export async function getServerSideProps(context: NextPageContext) {
}

const Home = () => {
const { data: movies = [] } = useMovieList();
const { data: movies = [] } = useMovieList("movie");
const { data: shorts = [] } = useMovieList("shorts");
const { data: favorites = [] } = useFavorites();
const {isOpen, closeModal} = useInfoModalStore();

Expand All @@ -39,7 +40,9 @@ const Home = () => {
<Billboard />
<div className="pb-40">
<MovieList title="Trending Now" data={movies} />
<MovieList title="Shorts" data={shorts} />
<MovieList title="My List" data={favorites} />

</div>
</>
)
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ model Movie {
thumbnailUrl String
genre String
duration String
classifier String @default("movie")
}

0 comments on commit 39505e2

Please sign in to comment.