diff --git a/api/index.js b/api/index.js index 778ab4452ded781dd8e2d3a83a2267d9bf81ff20..c3d3d0c38be0544f92b44e1073b4e128eb640e20 100644 --- a/api/index.js +++ b/api/index.js @@ -61,6 +61,21 @@ app.get('/tags/edition', (req, res) => { res.json(result); }); }); +app.post('/tags/edition/tag', (req, res) => { + let year = req.body.year; + if (!year) { + res.status(http_status_codes_1.default.BAD_REQUEST).send({ error: "Missing arguments" }); + return; + } + const query = "SELECT `id_tag` FROM `tag_team_edition` WHERE `year` = ?"; + exports.db.query(query, [year], (err, result) => { + if (err) { + res.send(err); + return; + } + res.json(result); + }); +}); // Insert a tag app.post('/tags', (req, res) => { let id_tag = req.body.id_tag; @@ -147,17 +162,59 @@ app.post('/teams', (req, res) => { exports.db.query(query, [team_name, team_participants], (err, result) => { if (err) { res.status(http_status_codes_1.default.CONFLICT).send({ error: "Team name already exists!" }); + return; } let id_team = result.insertId; const query_tte = "INSERT INTO `tag_team_edition`(`id_tag`, `id_team`, `year`) VALUES (?,?,?)"; exports.db.query(query_tte, [tag, id_team, year], (err, result) => { if (err) { res.status(http_status_codes_1.default.INTERNAL_SERVER_ERROR).send({ error: "Something wrong append" }); + return; + } + res.status(http_status_codes_1.default.OK).send(); + }); + }); +}); +app.put('/teams', (req, res) => { + let id_team = req.body.id_team; + let team_name = req.body.team_name; + let team_participants = req.body.team_participants; + let year = req.body.year_selected; + let tag = req.body.tag_selected; + if (!id_team || !team_name || !team_participants || !year || !tag) { + res.status(http_status_codes_1.default.BAD_REQUEST).send({ error: "Missing arguments" }); + return; + } + const query = 'UPDATE `team` SET `name`= ?,`participants`= ? WHERE id_team = ?'; + exports.db.query(query, [team_name, team_participants, id_team], (err, result) => { + if (err) { + res.status(http_status_codes_1.default.CONFLICT).send({ error: "Team name already exists!" }); + return; + } + const query_tte = "UPDATE `tag_team_edition` SET `id_tag` = ? WHERE `tag_team_edition`.`id_team` = ? AND `tag_team_edition`.`year` = ? "; + exports.db.query(query_tte, [tag, id_team, year], (err, result) => { + if (err) { + res.status(http_status_codes_1.default.INTERNAL_SERVER_ERROR).send({ error: "Something wrong append" }); + return; } res.status(http_status_codes_1.default.OK).send(); }); }); }); +app.delete('/teams', (req, res) => { + let id_team = req.body.id_team; + if (!id_team) { + res.status(http_status_codes_1.default.BAD_REQUEST).send({ error: "Missing arguments" }); + return; + } + const query = "DELETE FROM team WHERE id_team = ?"; + exports.db.query(query, [id_team], (err, result) => { + if (err) { + res.status(http_status_codes_1.default.INTERNAL_SERVER_ERROR).send({ error: "Something wrong append" }); + } + res.status(http_status_codes_1.default.OK).send(); + }); +}); /* * ------------------------------- Entry / results -------------------------------------- */ diff --git a/api/index.ts b/api/index.ts index acf8429ac0bac759950d300f736f0e04b77f3256..7e5de8132918d0309e207e270b9760ff1611488f 100644 --- a/api/index.ts +++ b/api/index.ts @@ -72,6 +72,26 @@ app.get('/tags/edition', (req: Request, res: Response) => { }) }); +app.post('/tags/edition/tag', (req: Request, res: Response) => { + let year = req.body.year; + + if (!year) { + res.status(StatusCode.BAD_REQUEST).send({error: "Missing arguments"}); + return + } + + const query = "SELECT `id_tag` FROM `tag_team_edition` WHERE `year` = ?" + + db.query(query, [year], (err, result) => { + if (err) { + res.send(err); + return + } + + res.json(result); + }) +}); + // Insert a tag app.post('/tags', (req: Request, res: Response) => { let id_tag = req.body.id_tag; @@ -184,6 +204,7 @@ app.post('/teams', (req: Request, res:Response) => { db.query(query, [team_name, team_participants], (err, result) => { if (err) { res.status(StatusCode.CONFLICT).send({error: "Team name already exists!"}); + return } let id_team = result.insertId; @@ -192,6 +213,7 @@ app.post('/teams', (req: Request, res:Response) => { db.query(query_tte, [tag, id_team, year], (err, result) => { if (err) { res.status(StatusCode.INTERNAL_SERVER_ERROR).send({error: "Something wrong append"}); + return } res.status(StatusCode.OK).send(); @@ -199,6 +221,59 @@ app.post('/teams', (req: Request, res:Response) => { }); }) +app.put('/teams', (req: Request, res:Response) => { + let id_team = req.body.id_team + let team_name = req.body.team_name; + let team_participants = req.body.team_participants; + let year = req.body.year_selected; + let tag = req.body.tag_selected; + + + if(!id_team || !team_name || !team_participants || !year || !tag) { + res.status(StatusCode.BAD_REQUEST).send({error: "Missing arguments"}); + return + } + + const query = 'UPDATE `team` SET `name`= ?,`participants`= ? WHERE id_team = ?'; + + db.query(query, [team_name, team_participants, id_team], (err, result) => { + if (err) { + res.status(StatusCode.CONFLICT).send({error: "Team name already exists!"}); + return + } + + const query_tte = "UPDATE `tag_team_edition` SET `id_tag` = ? WHERE `tag_team_edition`.`id_team` = ? AND `tag_team_edition`.`year` = ? "; + + db.query(query_tte, [tag, id_team, year], (err, result) => { + if (err) { + res.status(StatusCode.INTERNAL_SERVER_ERROR).send({error: "Something wrong append"}); + return + } + + res.status(StatusCode.OK).send(); + }); + }); +}) + +app.delete('/teams',(req: Request, res:Response) => { + let id_team = req.body.id_team; + + if(!id_team) { + res.status(StatusCode.BAD_REQUEST).send({error: "Missing arguments"}); + return + } + + const query = "DELETE FROM team WHERE id_team = ?"; + + db.query(query, [id_team], (err, result) => { + if (err) { + res.status(StatusCode.INTERNAL_SERVER_ERROR).send({error: "Something wrong append"}); + } + + res.status(StatusCode.OK).send(); + }); +}); + /* * ------------------------------- Entry / results -------------------------------------- */ diff --git a/frontend/index.html b/frontend/index.html index 26c29c45b2689ed46f41251e9b92944771bfe298..13048a8edf121966a853edc2e58773cf4195181c 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -2,9 +2,8 @@ <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> + <title>Boldor</title> </head> <body> <div id="app"></div> diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 7f868e77b47090cc14ad7c3a02ce5911ca0fc14f..3affe48e64a8f5ed684249b279f361e0ff8586f9 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -10,11 +10,11 @@ <Router> <header> <h1 id="title">Boldor</h1> - <Link class="link" to="/">Home</Link> - <Link class="link" to="teams">Équipes</Link> - <Link class="link" to="team">Gestion des équipes</Link> - <Link class="link" to="entry">Résultats</Link> - <Link class="link" to="tags">Tag RFID</Link> + <Link to="/"><span class="nav-link">Home</span></Link> + <Link to="teams"><span class="nav-link">Équipes</span></Link> + <Link to="team"><span class="nav-link">Gestion des équipes</span></Link> + <Link to="entry"><span class="nav-link">Résultats</span></Link> + <Link to="tags"><span class="nav-link">Tag RFID</span></Link> </header> <main> @@ -62,10 +62,6 @@ text-align: center; } - .link { - background-color: brown; - } - main { width: 80%; max-width: 1024px; diff --git a/frontend/src/app.css b/frontend/src/app.css index 69dd220cc487f862b1911b8d65b0be8c465850e7..3884f063c0cbe6acb4e1a60a4ce1c54bfb3351f2 100644 --- a/frontend/src/app.css +++ b/frontend/src/app.css @@ -30,7 +30,7 @@ select, button { color: white; border: 0; padding: 10px; - font-size: 1.1em; + font-size: 1em; font-weight: bold; border-radius: 5px; } @@ -79,4 +79,25 @@ form button { margin-top: 20px; font-size: 1.3em; padding: 5px; +} + +.nav-link { + font-size: 1.3em; + text-decoration: none; + color: white; + background-color: #1251c7; + padding: 10px; + border-radius: 5px; +} + +.nav-link:hover { + background-color: #0a2f74; +} + +.warning { + background-color: #E84855; +} + +.warning:hover { + background-color: #9F4A54; } \ No newline at end of file diff --git a/frontend/src/lib/Entry.svelte b/frontend/src/lib/Entry.svelte index d707ebf5665e9bd76f0fff6f4160e669a3522519..dadd8e26ab8a06dc65ff9fdeccd970ec65b68221 100644 --- a/frontend/src/lib/Entry.svelte +++ b/frontend/src/lib/Entry.svelte @@ -1,6 +1,9 @@ <script> const edition_url = "http://localhost:8000/editions"; + const tags_url = "http://localhost:8000/tags/edition/tag"; + let year_selected = new Date().getFullYear(); + let editions = []; fetch(edition_url) .then((response) => response.json()) @@ -8,6 +11,26 @@ editions = editions_data; }); + let tags_array= []; + let tag_selected; + + let body_data = { + year: year_selected + }; + fetch(tags_url,{ + method: "POST", + body: JSON.stringify(body_data), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, + }) + .then((response) => response.json()) + .then((tags_data) => { + console.log(tags_data) + tags_array = tags_data; + tag_selected = tags_array[0].id_tag; + }); + let entries = []; function getEntries(year) { let entries_url = "http://localhost:8000/entry?year=" + year; @@ -18,7 +41,6 @@ }); } - let year_selected = new Date().getFullYear(); function selectYear() { getEntries(year_selected); } @@ -30,9 +52,8 @@ getEntries(year_selected); function sendEntry() { - console.log("get entries"); let body_data = { - id_tag: "19040562" + id_tag: tag_selected }; fetch("http://localhost:8000/entry", { @@ -44,12 +65,7 @@ }).then(function (response) { if (!response.ok) return Promise.reject(response.status); getEntries(year_selected); - return response.json(); - }) - .then(function (data) { - console.log("okey" + data); - }) - .catch(function (error) { + }).catch(function (error) { console.log("Error: " + error); }); @@ -66,8 +82,14 @@ <option value={edition.year}>{edition.year}</option> {/each} </select> + + <select name="" id="" bind:value={tag_selected}> + {#each tags_array as tag} + <option value={tag.id_tag}>{tag.id_tag}</option> + {/each} + </select> - <button on:click={sendEntry}>Add entry</button> + <button on:click|preventDefault={sendEntry}>Add entry</button> </form> <table> diff --git a/frontend/src/lib/Tags.svelte b/frontend/src/lib/Tags.svelte index 55b033c940e7f44a983c0701f95365ab1759a05d..fe7aa8b494436acd8894003f4afea15eac4456f2 100644 --- a/frontend/src/lib/Tags.svelte +++ b/frontend/src/lib/Tags.svelte @@ -102,7 +102,7 @@ <button on:click|preventDefault={updateTag}>Mettre à jour</button> {/if} {:else} - <button on:click|preventDefault={deleteTag}>Supprimer le tag</button> + <button class="warning" on:click|preventDefault={deleteTag}>Supprimer le tag</button> {/if} </form> diff --git a/frontend/src/lib/Team.svelte b/frontend/src/lib/Team.svelte index a17ef0173f8e6aa216b9302b6546ae8950aae1d0..7255d5f69bbf42d7396ad0533c97e4aa6855e682 100644 --- a/frontend/src/lib/Team.svelte +++ b/frontend/src/lib/Team.svelte @@ -4,6 +4,8 @@ let teams = []; let availableTags = []; let year_selected = new Date().getFullYear(); + let update = false; + let id_team; let TEAMS_URL = "http://localhost:8000/teams/tag?year="; function getTeams() { @@ -37,35 +39,61 @@ // Send data function save() { - if(!team_name || !team_participants || !tag_selected || !year_selected) { + if(!update) { + if(!team_name || !team_participants || !tag_selected || !year_selected) { alert("Vous devez remplir tous les champs"); return - } + } + + let body_data = { + team_name: team_name, + team_participants: team_participants, + tag_selected: tag_selected, + year_selected: year_selected + }; - let body_data = { - team_name: team_name, - team_participants: team_participants, - tag_selected: tag_selected, - year_selected: year_selected - }; - - fetch("http://localhost:8000/teams", { - method: "POST", - body: JSON.stringify(body_data), - headers: { - "Content-type": "application/json; charset=UTF-8", - }, - }) - .then(function (response) { - if (!response.ok) return Promise.reject(response.status); - getTeams(); + fetch("http://localhost:8000/teams", { + method: "POST", + body: JSON.stringify(body_data), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, }) - .then(function (data) { - console.log("okey" + data); + .then(function (response) { + if (!response.ok) return Promise.reject(response.status); + getTeams(); + }).catch(function (error) { + console.log("Error: " + error); + }); + } else { + if(!team_name || !team_participants || !tag_selected || !year_selected) { + alert("Vous devez remplir tous les champs"); + return + } + + let body_data = { + id_team: id_team, + team_name: team_name, + team_participants: team_participants, + tag_selected: tag_selected, + year_selected: year_selected + }; + + fetch("http://localhost:8000/teams", { + method: "PUT", + body: JSON.stringify(body_data), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, }) - .catch(function (error) { - console.log("Error: " + error); - }); + .then(function (response) { + if (!response.ok) return Promise.reject(response.status); + getTeams(); + }).catch(function (error) { + console.log("Error: " + error); + }); + } + } @@ -76,11 +104,13 @@ function selectTeam(index) { for (let i in teams) { if(teams[i].id_team == index) { + id_team = teams[i].id_team; team_name = teams[i].name; team_participants = teams[i].participants; tag_selected = teams[i].id_tag; } } + update = true; } getTeams() @@ -124,7 +154,11 @@ </select> </div> - <button on:click|preventDefault={save}>Enregistrer</button> + {#if !update} + <button on:click|preventDefault={save}>Enregistrer</button> + {:else} + <button on:click|preventDefault={save}>Mettre à jour les informations</button> + {/if} </form> </div> diff --git a/frontend/src/lib/Teams.svelte b/frontend/src/lib/Teams.svelte index 20688e9112195e76d262006e88e361c514cf0895..898510306b1e937f51fc746a7bfdbfb36b49fa1e 100644 --- a/frontend/src/lib/Teams.svelte +++ b/frontend/src/lib/Teams.svelte @@ -1,5 +1,8 @@ <script> const edition_url = "http://localhost:8000/editions"; + let id_team; + let delete_mod = false; + let team_name = ""; let editions = []; fetch(edition_url) @@ -23,6 +26,38 @@ getTeams(year_selected); } + function selectTeam(id_selected) { + id_team = id_selected; + delete_mod = true; + for (let i in teams) { + if(teams[i].id_team == id_selected) { + team_name = teams[i].name; + } + } + } + + function deleteTeam(){ + let body_data = { + id_team: id_team + }; + + fetch("http://localhost:8000/teams", { + method: "DELETE", + body: JSON.stringify(body_data), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, + }) + .then(function (response) { + if (!response.ok) return Promise.reject(response.status); + getTeams(year_selected); + id_team = ""; + delete_mod = false; + }).catch(function (error) { + console.log("Error: " + error); + }); + } + getTeams(year_selected); </script> @@ -35,6 +70,10 @@ {/each} </select> + {#if delete_mod} + <button class="warning" on:click={deleteTeam}>Confirmer la suppression de la team "{team_name}"</button> + {/if} + <table> <tr> <th>Tag RFID</th> @@ -45,7 +84,7 @@ <tr> <td>{team.id_tag}</td> <td>{team.name}</td> - <td>...</td> + <td><button on:click={()=>selectTeam(team.id_team)}>Supprimer</button></td> </tr> {/each} </table>