Skip to content

Commit 0c264f4

Browse files
committed
Initial Commit
1 parent f6e6cf9 commit 0c264f4

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ yarn-error.log*
3333

3434
# typescript
3535
*.tsbuildinfo
36-
next-env.d.ts
36+
next-env.d.ts

pages/movies.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import clientPromise from "../lib/mongodb";
2+
3+
export default function Movies({ movies }) {
4+
return (
5+
<div>
6+
<h1>Top 20 Movies of All Time</h1>
7+
<p>
8+
<small>(According to Metacritic)</small>
9+
</p>
10+
<ul>
11+
{movies.map((movie) => (
12+
<li>
13+
<h2>{movie.title}</h2>
14+
<h3>{movie._id}</h3>
15+
<p>{movie.plot}</p>
16+
</li>
17+
))}
18+
</ul>
19+
</div>
20+
);
21+
}
22+
23+
export async function getServerSideProps(context) {
24+
try {
25+
const client = await clientPromise;
26+
27+
const db = client.db("sample_mflix");
28+
29+
const movies = await db
30+
.collection("movies")
31+
.find({})
32+
.sort({ metacritic: -1 })
33+
.limit(10)
34+
.toArray();
35+
36+
return {
37+
props: { movies: JSON.parse(JSON.stringify(movies)) },
38+
};
39+
} catch (error) {
40+
console.log(error);
41+
}
42+
}

pages/top.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import clientPromise from "../lib/mongodb";
2+
3+
export default function Top({ movies }) {
4+
return (
5+
<div>
6+
<h1>Top 1000 Movies of All Time</h1>
7+
<p>
8+
<small>(According to Metacritic)</small>
9+
</p>
10+
<ul>
11+
{movies.map((movie) => (
12+
<li>
13+
<h2>{movie.title}</h2>
14+
<h3>{movie.metacritic}</h3>
15+
<p>{movie.plot}</p>
16+
</li>
17+
))}
18+
</ul>
19+
</div>
20+
);
21+
}
22+
23+
export async function getStaticProps() {
24+
try {
25+
const client = await clientPromise;
26+
27+
const db = client.db("sample_mflix");
28+
29+
const movies = await db
30+
.collection("movies")
31+
.find({})
32+
.sort({ metacritic: -1 })
33+
.limit(1000)
34+
.toArray();
35+
36+
return {
37+
props: { movies: JSON.parse(JSON.stringify(movies)) },
38+
};
39+
} catch (error) {
40+
console.log(error);
41+
}
42+
}

0 commit comments

Comments
 (0)