mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-07-23 12:36:57 +00:00
static data generation with cache
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user