diff --git a/api/index.js b/api/index.js index f740836d86df56e570ddd3ec2e4dc25de06dd3a0..778ab4452ded781dd8e2d3a83a2267d9bf81ff20 100644 --- a/api/index.js +++ b/api/index.js @@ -61,6 +61,55 @@ app.get('/tags/edition', (req, res) => { res.json(result); }); }); +// Insert a tag +app.post('/tags', (req, res) => { + let id_tag = req.body.id_tag; + if (!id_tag) { + res.status(http_status_codes_1.default.BAD_REQUEST).send({ error: "Missing arguments" }); + return; + } + const query = "INSERT INTO tag (id_tag) VALUES (?)"; + exports.db.query(query, [id_tag], (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(); + }); +}); +// Update tag +app.put('/tags', (req, res) => { + let id_tag = req.body.id_tag; + let new_id = req.body.new_id; + if (!id_tag) { + res.status(http_status_codes_1.default.BAD_REQUEST).send({ error: "Missing arguments" }); + return; + } + const query = "UPDATE tag SET id_tag = ? WHERE id_tag = ?"; + exports.db.query(query, [new_id, id_tag], (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(); + }); +}); +// Insert a tag +app.delete('/tags', (req, res) => { + let id_tag = req.body.id_tag; + if (!id_tag) { + res.status(http_status_codes_1.default.BAD_REQUEST).send({ error: "Missing arguments" }); + return; + } + const query = "DELETE FROM tag WHERE id_tag = ?"; + exports.db.query(query, [id_tag], (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(); + }); +}); /* * ------------------------------- TEAMS -------------------------------------- */ diff --git a/api/index.ts b/api/index.ts index 1159760416597e98181f1f06a0ac1350d732422a..acf8429ac0bac759950d300f736f0e04b77f3256 100644 --- a/api/index.ts +++ b/api/index.ts @@ -72,6 +72,67 @@ app.get('/tags/edition', (req: Request, res: Response) => { }) }); +// Insert a tag +app.post('/tags', (req: Request, res: Response) => { + let id_tag = req.body.id_tag; + + if (!id_tag) { + res.status(StatusCode.BAD_REQUEST).send({error: "Missing arguments"}); + return + } + + const query = "INSERT INTO tag (id_tag) VALUES (?)"; + + db.query(query, [id_tag], (err, result) => { + if (err) { + res.status(StatusCode.INTERNAL_SERVER_ERROR).send({error: "Something wrong append"}); + return + } + res.status(StatusCode.OK).send(); + }) +}); + +// Update tag +app.put('/tags', (req: Request, res: Response) => { + let id_tag = req.body.id_tag; + let new_id = req.body.new_id; + + if (!id_tag) { + res.status(StatusCode.BAD_REQUEST).send({error: "Missing arguments"}); + return + } + + const query = "UPDATE tag SET id_tag = ? WHERE id_tag = ?"; + + db.query(query, [new_id, id_tag], (err, result) => { + if (err) { + res.status(StatusCode.INTERNAL_SERVER_ERROR).send({error: "Something wrong append"}); + return + } + res.status(StatusCode.OK).send(); + }) + +}); + +// Insert a tag +app.delete('/tags', (req: Request, res: Response) => { + let id_tag = req.body.id_tag; + + if (!id_tag) { + res.status(StatusCode.BAD_REQUEST).send({error: "Missing arguments"}); + return + } + + const query = "DELETE FROM tag WHERE id_tag = ?"; + + db.query(query, [id_tag], (err, result) => { + if (err) { + res.status(StatusCode.INTERNAL_SERVER_ERROR).send({error: "Something wrong append"}); + return + } + res.status(StatusCode.OK).send(); + }) +}); /* * ------------------------------- TEAMS -------------------------------------- diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index a2a535f355cbafd90b2c76fc5a4d161065876dfe..7f868e77b47090cc14ad7c3a02ce5911ca0fc14f 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -4,6 +4,7 @@ import Teams from "./lib/Teams.svelte"; import Entry from "./lib/Entry.svelte"; import Team from "./lib/Team.svelte"; + import Tags from "./lib/Tags.svelte"; </script> <Router> @@ -13,6 +14,7 @@ <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> </header> <main> @@ -47,6 +49,10 @@ <Route path="entry"> <Entry /> </Route> + + <Route path="tags"> + <Tags /> + </Route> </main> </Router> diff --git a/frontend/src/lib/Tags.svelte b/frontend/src/lib/Tags.svelte new file mode 100644 index 0000000000000000000000000000000000000000..55b033c940e7f44a983c0701f95365ab1759a05d --- /dev/null +++ b/frontend/src/lib/Tags.svelte @@ -0,0 +1,127 @@ +<script> + let TAG_URL = "http://localhost:8000/tags"; + let tags = []; + let id_tag; + let id_tag_to_update = ""; + let update = false; + let delete_tag = false; + + function getTags() { + fetch(TAG_URL) + .then((response) => response.json()) + .then((tags_data) => { + tags = tags_data; + }); + } + + function createTag() { + let body_data = { + id_tag: id_tag + }; + + fetch(TAG_URL, { + 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); + getTags(); + id_tag = ""; + }).catch(function (error) { + console.log("Error: " + error); + }); + } + + function selectTag(tag_selected, del) { + id_tag_to_update = tag_selected; + id_tag = id_tag_to_update + update = true + delete_tag = del + } + + function updateTag() { + update = false + + let body_data = { + id_tag: id_tag_to_update, + new_id: id_tag + }; + + fetch(TAG_URL, { + method: "PUT", + body: JSON.stringify(body_data), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, + }).then(function (response) { + if (!response.ok) return Promise.reject(response.status); + getTags(); + id_tag = ""; + }).catch(function (error) { + console.log("Error: " + error); + }); + } + + function deleteTag() { + let body_data = { + id_tag: id_tag + }; + + fetch(TAG_URL, { + 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); + getTags(); + id_tag = ""; + delete_tag = false + update = false + }).catch(function (error) { + console.log("Error: " + error); + }); + } + + getTags(); +</script> + +<div class="container"> + <form action="#"> + <div class="form-group"> + <label for="tag">Id du nouveau tag</label> + <input type="text" id="tag" bind:value={id_tag} /> + </div> + {#if !delete_tag} + {#if !update} + <button on:click|preventDefault={createTag}>Créer le tag</button> + {:else} + <button on:click|preventDefault={updateTag}>Mettre à jour</button> + {/if} + {:else} + <button on:click|preventDefault={deleteTag}>Supprimer le tag</button> + {/if} + + </form> + <table> + <tr> + <th>Id tag</th> + <th>Action</th> + </tr> + {#each tags as tag, i} + <tr> + <td>{tag.id_tag}</td> + <td> + <button on:click={()=>selectTag(tag.id_tag, false)}>Edit</button> + <button class="warning" on:click={()=>selectTag(tag.id_tag, true)}>Delete</button> + </td> + </tr> + {/each} + </table> +</div> + +<style> +</style> diff --git a/frontend/src/lib/Team.svelte b/frontend/src/lib/Team.svelte index c84531f9b2fe670708d699f23fc071cbf434f9e3..a17ef0173f8e6aa216b9302b6546ae8950aae1d0 100644 --- a/frontend/src/lib/Team.svelte +++ b/frontend/src/lib/Team.svelte @@ -59,7 +59,6 @@ .then(function (response) { if (!response.ok) return Promise.reject(response.status); getTeams(); - return response.json(); }) .then(function (data) { console.log("okey" + data);