Skip to content
Snippets Groups Projects
Commit b80c6aa3 authored by ExtraDev's avatar ExtraDev
Browse files

Add api cors, create endpoints to get teams and editions, create main frontend...

Add api cors, create endpoints to get teams and editions, create main frontend component to display teams
parent 4280d622
No related branches found
No related tags found
No related merge requests found
Showing
with 1469 additions and 28 deletions
PORT=8000
API_PORT=8000
DB_PORT=1028
DB_HOST="localhost"
DB_USER="admin"
DB_PWD="admin"
......
......@@ -7,22 +7,51 @@ exports.db = void 0;
const express_1 = __importDefault(require("express"));
const dotenv_1 = __importDefault(require("dotenv"));
const mysql2_1 = __importDefault(require("mysql2"));
dotenv_1.default.config();
// -------------------------
const cors_1 = __importDefault(require("cors"));
dotenv_1.default.config({ path: __dirname + '/../.env' });
exports.db = mysql2_1.default.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PWD,
database: process.env.DB_NAME,
});
const options = {
origin: "*"
};
const app = (0, express_1.default)();
const port = process.env.PORT;
app.get('/', (req, res) => {
res.json({ user: 'geek' });
});
app.get('/all', (req, res) => {
const query = "SELECT * FROM entry";
const port = process.env.API_PORT;
app.use((0, cors_1.default)(options));
app.get('/editions', (req, res) => {
const query = 'SELECT * FROM edition';
exports.db.query(query, [], (err, result) => {
if (err) {
res.send(err);
}
res.json(result);
});
});
app.get('/teams', (req, res) => {
const query = 'SELECT team.id_team, team.name ' +
'FROM `tag_team_edition` ' +
'LEFT JOIN team ON tag_team_edition.id_team = team.id_team ' +
'WHERE year = ?';
const year = req.query.year;
exports.db.query(query, [year], (err, result) => {
if (err) {
res.send(err);
}
res.json(result);
});
});
app.get('/entry', (req, res) => {
const query = 'SELECT entry.time_stamp, tag_team_edition.year, entry.id_tag, tag_team_edition.year, team.name ' +
'FROM `entry` ' +
'LEFT JOIN tag ON entry.id_tag = tag.id_tag ' +
'LEFT JOIN tag_team_edition ON tag.id_tag = tag_team_edition.id_tag ' +
'LEFT JOIN team ON tag_team_edition.id_team = team.id_team ' +
'WHERE tag_team_edition.year = ?';
const year = req.query.year;
exports.db.query(query, [year], (err, result) => {
if (err) {
res.send("ERROR");
}
......
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import mysql from "mysql2";
import cors from 'cors';
dotenv.config();
dotenv.config({path:__dirname+'/../.env'});
// -------------------------
export const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
......@@ -12,19 +12,57 @@ export const db = mysql.createConnection({
database: process.env.DB_NAME,
})
const options: cors.CorsOptions = {
origin: "*"
};
const app = express();
const port = process.env.PORT;
const port = process.env.API_PORT;
app.use(cors(options));
app.get('/editions', (req: Request, res: Response) => {
const query = 'SELECT * FROM edition';
db.query(query, [], (err, result) => {
if (err) {
res.send(err);
}
app.get('/', (req: Request, res: Response) => {
res.json({ user: 'geek' });
res.json(result);
}
});
app.get('/all', (req: Request, res: Response) => {
const query = "SELECT * FROM entry";
db.query(
query,
[],
(err, result) => {
app.get('/teams', (req: Request, res: Response) => {
const query = 'SELECT team.id_team, team.name ' +
'FROM `tag_team_edition` ' +
'LEFT JOIN team ON tag_team_edition.id_team = team.id_team ' +
'WHERE year = ?';
const year = req.query.year
db.query(query, [year], (err, result) => {
if (err) {
res.send(err);
}
res.json(result);
}
)
});
app.get('/entry', (req: Request, res: Response) => {
const query = 'SELECT entry.time_stamp, tag_team_edition.year, entry.id_tag, tag_team_edition.year, team.name '+
'FROM `entry` ' +
'LEFT JOIN tag ON entry.id_tag = tag.id_tag ' +
'LEFT JOIN tag_team_edition ON tag.id_tag = tag_team_edition.id_tag ' +
'LEFT JOIN team ON tag_team_edition.id_team = team.id_team ' +
'WHERE tag_team_edition.year = ?';
const year = req.query.year
db.query(query, [year], (err, result) => {
if (err) {
res.send("ERROR");
}
......
......@@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mysql": "^2.18.1",
......@@ -17,6 +18,7 @@
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.12",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.14",
"@types/mysql": "^2.15.21",
......@@ -45,6 +47,12 @@
"@types/node": "*"
}
},
"node_modules/@types/cors": {
"version": "2.8.12",
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz",
"integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==",
"dev": true
},
"node_modules/@types/dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz",
......@@ -426,6 +434,18 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"node_modules/cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"dependencies": {
"object-assign": "^4",
"vary": "^1"
},
"engines": {
"node": ">= 0.10"
}
},
"node_modules/date-fns": {
"version": "2.29.3",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
......@@ -1072,6 +1092,14 @@
"node": ">=0.10.0"
}
},
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/object-inspect": {
"version": "1.12.2",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz",
......@@ -1619,6 +1647,12 @@
"@types/node": "*"
}
},
"@types/cors": {
"version": "2.8.12",
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz",
"integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==",
"dev": true
},
"@types/dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz",
......@@ -1922,6 +1956,15 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
"requires": {
"object-assign": "^4",
"vary": "^1"
}
},
"date-fns": {
"version": "2.29.3",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
......@@ -2413,6 +2456,11 @@
"integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==",
"dev": true
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg=="
},
"object-inspect": {
"version": "1.12.2",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz",
......
......@@ -13,6 +13,7 @@
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mysql": "^2.18.1",
......@@ -20,6 +21,7 @@
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.12",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.14",
"@types/mysql": "^2.15.21",
......
......@@ -3,22 +3,22 @@ services:
database:
image: mysql:latest
volumes:
- ./private/data/db:/var/lib/mysql:rw
- ./database/private/data/db:/var/lib/mysql:rw
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=boldor
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PWD}
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 1028:80
- ${DB_PORT}:80
links:
- database
environment:
- PMA_HOST=database
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PWD}
- MYSQL_ROOT_PASSWORD=root
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
{
"recommendations": ["svelte.svelte-vscode"]
}
# Svelte + Vite
This template should help get you started developing with Svelte in Vite.
## Recommended IDE Setup
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
## Need an official Svelte framework?
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
## Technical considerations
**Why use this over SvelteKit?**
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
**Why include `.vscode/extensions.json`?**
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
**Why enable `checkJs` in the JS template?**
It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.
**Why is HMR not preserving my local component state?**
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```
<h1>Pad d'bol d'or</h1>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
{
"compilerOptions": {
"moduleResolution": "Node",
"target": "ESNext",
"module": "ESNext",
/**
* svelte-preprocess cannot figure out whether you have
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
* To have warnings / errors of the Svelte compiler at the
* correct position, enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable this if you'd like to use dynamic types.
*/
"checkJs": true
},
/**
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
This diff is collapsed.
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.1.0",
"svelte": "^3.52.0",
"vite": "^3.2.3"
}
}
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
\ No newline at end of file
<script>
import Teams from "./lib/Teams.svelte";
</script>
<main>
<Teams />
</main>
<style>
</style>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="26.6" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 308"><path fill="#FF3E00" d="M239.682 40.707C211.113-.182 154.69-12.301 113.895 13.69L42.247 59.356a82.198 82.198 0 0 0-37.135 55.056a86.566 86.566 0 0 0 8.536 55.576a82.425 82.425 0 0 0-12.296 30.719a87.596 87.596 0 0 0 14.964 66.244c28.574 40.893 84.997 53.007 125.787 27.016l71.648-45.664a82.182 82.182 0 0 0 37.135-55.057a86.601 86.601 0 0 0-8.53-55.577a82.409 82.409 0 0 0 12.29-30.718a87.573 87.573 0 0 0-14.963-66.244"></path><path fill="#FFF" d="M106.889 270.841c-23.102 6.007-47.497-3.036-61.103-22.648a52.685 52.685 0 0 1-9.003-39.85a49.978 49.978 0 0 1 1.713-6.693l1.35-4.115l3.671 2.697a92.447 92.447 0 0 0 28.036 14.007l2.663.808l-.245 2.659a16.067 16.067 0 0 0 2.89 10.656a17.143 17.143 0 0 0 18.397 6.828a15.786 15.786 0 0 0 4.403-1.935l71.67-45.672a14.922 14.922 0 0 0 6.734-9.977a15.923 15.923 0 0 0-2.713-12.011a17.156 17.156 0 0 0-18.404-6.832a15.78 15.78 0 0 0-4.396 1.933l-27.35 17.434a52.298 52.298 0 0 1-14.553 6.391c-23.101 6.007-47.497-3.036-61.101-22.649a52.681 52.681 0 0 1-9.004-39.849a49.428 49.428 0 0 1 22.34-33.114l71.664-45.677a52.218 52.218 0 0 1 14.563-6.398c23.101-6.007 47.497 3.036 61.101 22.648a52.685 52.685 0 0 1 9.004 39.85a50.559 50.559 0 0 1-1.713 6.692l-1.35 4.116l-3.67-2.693a92.373 92.373 0 0 0-28.037-14.013l-2.664-.809l.246-2.658a16.099 16.099 0 0 0-2.89-10.656a17.143 17.143 0 0 0-18.398-6.828a15.786 15.786 0 0 0-4.402 1.935l-71.67 45.674a14.898 14.898 0 0 0-6.73 9.975a15.9 15.9 0 0 0 2.709 12.012a17.156 17.156 0 0 0 18.404 6.832a15.841 15.841 0 0 0 4.402-1.935l27.345-17.427a52.147 52.147 0 0 1 14.552-6.397c23.101-6.006 47.497 3.037 61.102 22.65a52.681 52.681 0 0 1 9.003 39.848a49.453 49.453 0 0 1-22.34 33.12l-71.664 45.673a52.218 52.218 0 0 1-14.563 6.398"></path></svg>
\ No newline at end of file
<script>
let count = 0
const increment = () => {
count += 1
}
</script>
<button on:click={increment}>
count is {count}
</button>
<script>
const edition_url = "http://localhost:8000/editions";
let editions = [];
fetch(edition_url)
.then((response) => response.json())
.then((editions_data) => {
editions = editions_data;
})
let teams = [];
function getTeams(year) {
let teams_url = "http://localhost:8000/entry?year="+year;
fetch(teams_url)
.then((response) => response.json())
.then((teams_data) => {
teams = teams_data;
})
}
let year_selected = new Date().getFullYear();
function selectYear(){
getTeams(year_selected);
}
getTeams(year_selected)
</script>
<div class="container">
<h1>Pad d'bol d'or</h1>
<select name="" id="" bind:value={year_selected} on:change={selectYear}>
{#each editions as edition }
<option value="{edition.year}">{edition.year}</option>
{/each}
</select>
<table>
<tr>
<th>Tag RFID</th>
<th>Team name</th>
</tr>
{#each teams as team}
<tr>
<td>{team.id_tag}</td>
<td>{team.name}</td>
</tr>
{/each}
</table>
</div>
<style>
.container {
width: 80%;
margin: auto;
max-width: 1024px;
background-color: rgb(235, 235, 235);
padding: 15px;
}
table, tr, td, th{
border: 1px solid black;
}
table {
border-collapse: collapse;
width: 100%;
}
</style>
\ No newline at end of file
import './app.css'
import App from './App.svelte'
const app = new App({
target: document.getElementById('app')
})
export default app
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment