From 8b8bf9dcb68db3571920bb2128fbd709fe5b92ba Mon Sep 17 00:00:00 2001 From: Owen Rees Date: Tue, 23 May 2023 08:51:05 +0200 Subject: [PATCH] initial commit --- app/api/tournaments/route.ts | 13 +++ app/globals.css | 2 +- app/layout.tsx | 16 ++-- app/page.tsx | 106 +------------------------ app/tournois/page.tsx | 47 +++++++++++ lib/mongodb.ts | 31 ++++++++ package-lock.json | 149 +++++++++++++++++++++++++++++++++++ package.json | 1 + tests/getTournaments.http | 2 + 9 files changed, 253 insertions(+), 114 deletions(-) create mode 100644 app/api/tournaments/route.ts create mode 100644 app/tournois/page.tsx create mode 100644 lib/mongodb.ts create mode 100644 tests/getTournaments.http diff --git a/app/api/tournaments/route.ts b/app/api/tournaments/route.ts new file mode 100644 index 0000000..b19ae39 --- /dev/null +++ b/app/api/tournaments/route.ts @@ -0,0 +1,13 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import clientPromise from "@/lib/mongodb"; + +export async function GET(req: NextApiRequest, res: NextApiResponse) { + try { + const client = await clientPromise + const db = client.db("tournamentsFranceDB") + const data = await db.collection("tournaments").find({}).toArray() + return new Response(JSON.stringify(data), { status: 200 }) + } catch (error) { + return new Response(JSON.stringify(error), { status: 500 }) + } +} diff --git a/app/globals.css b/app/globals.css index fd81e88..ce7a2fc 100644 --- a/app/globals.css +++ b/app/globals.css @@ -8,7 +8,7 @@ --background-end-rgb: 255, 255, 255; } -@media (prefers-color-scheme: dark) { +@media (prefers-color-scheme: light) { :root { --foreground-rgb: 255, 255, 255; --background-start-rgb: 0, 0, 0; diff --git a/app/layout.tsx b/app/layout.tsx index 71b3fbf..ace44e4 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,21 +1,21 @@ -import './globals.css' -import { Inter } from 'next/font/google' +import "./globals.css"; +import { Inter } from "next/font/google"; -const inter = Inter({ subsets: ['latin'] }) +const inter = Inter({ subsets: ["latin"] }); export const metadata = { - title: 'Create Next App', - description: 'Generated by create next app', -} + title: "Echecs France", + description: "Generated by create next app", // TODO give the website a description + maybe keywords if they go here +}; export default function RootLayout({ children, }: { - children: React.ReactNode + children: React.ReactNode; }) { return ( {children} - ) + ); } diff --git a/app/page.tsx b/app/page.tsx index 745df65..cca751f 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -2,112 +2,8 @@ import Image from 'next/image' export default function Home() { return ( -
-
-

- Get started by editing  - app/page.tsx -

-
- - By{' '} - Vercel Logo - -
-
+
-
- Next.js Logo -
- -
- -

- Docs{' '} - - -> - -

-

- Find in-depth information about Next.js features and API. -

-
- - -

- Learn{' '} - - -> - -

-

- Learn about Next.js in an interactive course with quizzes! -

-
- - -

- Templates{' '} - - -> - -

-

- Explore the Next.js 13 playground. -

-
- - -

- Deploy{' '} - - -> - -

-

- Instantly deploy your Next.js site to a shareable URL with Vercel. -

-
-
) } diff --git a/app/tournois/page.tsx b/app/tournois/page.tsx new file mode 100644 index 0000000..f481961 --- /dev/null +++ b/app/tournois/page.tsx @@ -0,0 +1,47 @@ +export interface TournamentType { + _id: string; + location: string; + department: string; + tournament: string; + url: string; + time_control: string; + date: string; + coordinates: [number, number]; +} + +async function getTournaments() { + const res = await fetch("http://localhost:3000/api/tournaments", { + next: { revalidate: 300 }, + }); + return await res.json(); +} + +export default async function Tournaments() { + const data = await getTournaments(); + console.log(data); + + const tournaments = data.map((t: TournamentType) => ( + + {t.date} + {t.location} + {t.tournament} + {t.time_control} + + )); + + return ( +
+ + + + + + + + + + {tournaments} +
DateVilleTournoisCadence
+
+ ); +} diff --git a/lib/mongodb.ts b/lib/mongodb.ts new file mode 100644 index 0000000..7aa064f --- /dev/null +++ b/lib/mongodb.ts @@ -0,0 +1,31 @@ +import { MongoClient } from "mongodb"; + +if (!process.env.MONGODB_URI) { + throw new Error('Invalid environment variable: "MONGODB_URI"'); +} + +const uri = process.env.MONGODB_URI +const options = {} + +let client +let clientPromise: Promise + +if (!process.env.MONGODB_URI) { + throw new Error('Please add your Mongo URI to .env.local') +} + +if (process.env.NODE_ENV === 'development') { + // In development mode, use a global variable so that the value + // is preserved across module reloads caused by HMR (Hot Module Replacement). + if (!global._mongoClientPromise) { + client = new MongoClient(uri, options) + global._mongoClientPromise = client.connect() + } + clientPromise = global._mongoClientPromise +} else { + // In production mode, it's best to not use a global variable. + client = new MongoClient(uri, options) + clientPromise = client.connect() +} + +export default clientPromise diff --git a/package-lock.json b/package-lock.json index e5341c8..e79db7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "autoprefixer": "10.4.14", "eslint": "8.41.0", "eslint-config-next": "13.4.3", + "mongodb": "^5.5.0", "next": "13.4.3", "postcss": "8.4.23", "react": "18.2.0", @@ -424,6 +425,20 @@ "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.3.tgz", "integrity": "sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==" }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==" + }, + "node_modules/@types/whatwg-url": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-8.2.2.tgz", + "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==", + "dependencies": { + "@types/node": "*", + "@types/webidl-conversions": "*" + } + }, "node_modules/@typescript-eslint/parser": { "version": "5.59.6", "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.59.6.tgz", @@ -838,6 +853,14 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/bson": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-5.3.0.tgz", + "integrity": "sha512-ukmCZMneMlaC5ebPHXIkP8YJzNl5DC41N5MAIvKDqLggdao342t4McltoJBQfQya/nHBWAcSsYRqlXPoQkTJag==", + "engines": { + "node": ">=14.20.1" + } + }, "node_modules/bundle-name": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bundle-name/-/bundle-name-3.0.0.tgz", @@ -2214,6 +2237,11 @@ "node": ">= 0.4" } }, + "node_modules/ip": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", + "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==" + }, "node_modules/is-arguments": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.1.tgz", @@ -2707,6 +2735,12 @@ "node": ">=10" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -2762,6 +2796,47 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/mongodb": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.5.0.tgz", + "integrity": "sha512-XgrkUgAAdfnZKQfk5AsYL8j7O99WHd4YXPxYxnh8dZxD+ekYWFRA3JktUsBnfg+455Smf75/+asoU/YLwNGoQQ==", + "dependencies": { + "bson": "^5.3.0", + "mongodb-connection-string-url": "^2.6.0", + "socks": "^2.7.1" + }, + "engines": { + "node": ">=14.20.1" + }, + "optionalDependencies": { + "saslprep": "^1.0.3" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.201.0", + "mongodb-client-encryption": ">=2.3.0 <3", + "snappy": "^7.2.2" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-2.6.0.tgz", + "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==", + "dependencies": { + "@types/whatwg-url": "^8.2.1", + "whatwg-url": "^11.0.0" + } + }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -3620,6 +3695,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "dependencies": { + "sparse-bitfield": "^3.0.3" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/scheduler": { "version": "0.23.0", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.0.tgz", @@ -3687,6 +3774,28 @@ "node": ">=8" } }, + "node_modules/smart-buffer": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz", + "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==", + "engines": { + "node": ">= 6.0.0", + "npm": ">= 3.0.0" + } + }, + "node_modules/socks": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/socks/-/socks-2.7.1.tgz", + "integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==", + "dependencies": { + "ip": "^2.0.0", + "smart-buffer": "^4.2.0" + }, + "engines": { + "node": ">= 10.13.0", + "npm": ">= 3.0.0" + } + }, "node_modules/source-map-js": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", @@ -3695,6 +3804,15 @@ "node": ">=0.10.0" } }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "optional": true, + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/stop-iteration-iterator": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/stop-iteration-iterator/-/stop-iteration-iterator-1.0.0.tgz", @@ -4005,6 +4123,17 @@ "node": ">=8.0" } }, + "node_modules/tr46": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-3.0.0.tgz", + "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", + "dependencies": { + "punycode": "^2.1.1" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/ts-interface-checker": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz", @@ -4156,6 +4285,26 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-11.0.0.tgz", + "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", + "dependencies": { + "tr46": "^3.0.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 72dcfdb..3bb9c9d 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "autoprefixer": "10.4.14", "eslint": "8.41.0", "eslint-config-next": "13.4.3", + "mongodb": "^5.5.0", "next": "13.4.3", "postcss": "8.4.23", "react": "18.2.0", diff --git a/tests/getTournaments.http b/tests/getTournaments.http new file mode 100644 index 0000000..0fcd795 --- /dev/null +++ b/tests/getTournaments.http @@ -0,0 +1,2 @@ +### GET request with a header +GET http://localhost:3000/api/tournaments