static data generation with cache

This commit is contained in:
Owen Rees
2023-06-13 20:36:48 +02:00
parent db266e6ef3
commit 112c257321
9 changed files with 117 additions and 118 deletions
+3 -29
View File
@@ -1,44 +1,18 @@
import clientPromise from "@/lib/mongodb";
import { dateOrderingFrance } from "@/utils/dbDateOrdering";
// TODO collate only the country of France - redundant for now but will be needed when new countries are added
// probably do this by passing the country name as parameter
/**
* Tournament data API endpoint
* @route /api/tournaments/france
* @internal
*/
// TODO cache this result for 24 hours - using cache()
export async function GET() {
try {
const client = await clientPromise;
const db = client.db("tournamentsFranceDB");
/**
* Converts date from string into a date to allow ordering
*/
// TODO add into a middleware?
const data = await db
.collection("tournaments")
.aggregate([
{
$addFields: {
dateParts: {
$dateFromString: {
dateString: "$date",
format: "%d/%m/%Y",
},
},
},
},
{
$sort: {
dateParts: 1,
},
},
{
$unset: "dateParts",
},
])
.toArray();
const data = await dateOrderingFrance(db);
return new Response(JSON.stringify(data), { status: 200 });
} catch (error) {