tournaments ordered by date

This commit is contained in:
Owen Rees
2023-05-23 10:00:43 +02:00
parent 8b8bf9dcb6
commit e0d363e612
6 changed files with 3904 additions and 24 deletions
+32 -6
View File
@@ -1,13 +1,39 @@
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from "next";
import clientPromise from "@/lib/mongodb"; import clientPromise from "@/lib/mongodb";
export async function GET(req: NextApiRequest, res: NextApiResponse) { export async function GET(req: NextApiRequest, res: NextApiResponse) {
// TODO delete req / res ?
try { try {
const client = await clientPromise const client = await clientPromise;
const db = client.db("tournamentsFranceDB") const db = client.db("tournamentsFranceDB");
const data = await db.collection("tournaments").find({}).toArray() // const data = await db.collection("tournaments").find({}).toArray();
return new Response(JSON.stringify(data), { status: 200 }) // converts date from string into a date and then orders by date
const data = await db
.collection("tournaments")
.aggregate([
{
$addFields: {
dateParts: {
$dateFromString: {
dateString: "$date",
format: "%d/%m/%Y",
},
},
},
},
{
$sort: {
dateParts: 1,
},
},
{
$unset: "dateParts",
},
])
.toArray();
return new Response(JSON.stringify(data), { status: 200 });
} catch (error) { } catch (error) {
return new Response(JSON.stringify(error), { status: 500 }) return new Response(JSON.stringify(error), { status: 500 });
} }
} }
+6 -2
View File
@@ -1,3 +1,5 @@
import Layout from "@/components/Layout";
export interface TournamentType { export interface TournamentType {
_id: string; _id: string;
location: string; location: string;
@@ -21,7 +23,7 @@ export default async function Tournaments() {
console.log(data); console.log(data);
const tournaments = data.map((t: TournamentType) => ( const tournaments = data.map((t: TournamentType) => (
<tr> <tr key={t._id}>
<td>{t.date}</td> <td>{t.date}</td>
<td>{t.location}</td> <td>{t.location}</td>
<td>{t.tournament}</td> <td>{t.tournament}</td>
@@ -30,7 +32,8 @@ export default async function Tournaments() {
)); ));
return ( return (
<main className=""> <Layout>
<main className="w-full">
<table className="table-auto"> <table className="table-auto">
<thead> <thead>
<tr> <tr>
@@ -43,5 +46,6 @@ export default async function Tournaments() {
<tbody>{tournaments}</tbody> <tbody>{tournaments}</tbody>
</table> </table>
</main> </main>
</Layout>
); );
} }
+14
View File
@@ -0,0 +1,14 @@
import Navbar from "@/components/Navbar";
export default function Layout({
children, // will be a page or nested layout
}: {
children: React.ReactNode;
}) {
return (
<>
<Navbar />
{children}
</>
);
}
+3
View File
@@ -0,0 +1,3 @@
export default function Navbar() {
return <nav className="bg-gray-800 p-2 m-0 w-full">NAVBAR</nav>;
}
+3826
View File
File diff suppressed because it is too large Load Diff
+8 -1
View File
@@ -6,7 +6,8 @@
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"lint": "next lint" "lint": "next lint",
"test": "jest --watch"
}, },
"dependencies": { "dependencies": {
"@types/node": "20.2.3", "@types/node": "20.2.3",
@@ -22,5 +23,11 @@
"react-dom": "18.2.0", "react-dom": "18.2.0",
"tailwindcss": "3.3.2", "tailwindcss": "3.3.2",
"typescript": "5.0.4" "typescript": "5.0.4"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0"
} }
} }