From 81ba27a2bb94c25b26f1a2b0c41d3450c35d10ed Mon Sep 17 00:00:00 2001 From: OMGiTzPomPom Date: Fri, 24 Mar 2023 10:36:36 +0100 Subject: [PATCH] tp 1b fin --- tp1b/index.html | 11 +++++----- tp1b/service.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ tp1b/team.json | 1 + 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 tp1b/team.json diff --git a/tp1b/index.html b/tp1b/index.html index 9183a78..e72e712 100644 --- a/tp1b/index.html +++ b/tp1b/index.html @@ -6,14 +6,15 @@ const teamsDiv = document.getElementById('teams'); function fetchTeams() { - fetch('http://127.0.0.1:9090/equipes') + fetch('http://127.0.0.1:9090/api/teams') .then((response) => { response.json() .then((teams) => { - const mmoContent = teams.equipes.map((team) => - `
${team.name} - ${team.id}
` - ).join(''); - teamsDiv.innerHTML = mmoContent; + let div = ""; + teams.forEach((t) => { + div += `
Equipe ID${t.id} - Nom d'équipe ${t.team}
\n` + }) + teamsDiv.innerHTML = div; }) }).catch(error => console.error); } diff --git a/tp1b/service.js b/tp1b/service.js index e69de29..380f3b7 100644 --- a/tp1b/service.js +++ b/tp1b/service.js @@ -0,0 +1,55 @@ +const express = require("express"); + +const app = express(); + +app.listen(9090); + +app.use(function(req, res, next) { + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); + res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept'); + next(); +}); + + +const fs = require('fs'); + +app.get('/', (req, res) => { + res.sendFile(__dirname + "/index.html"); +}); + +app.get('/api/teams', (req, res) => { + const data = fs.readFileSync( __dirname + '/team.json', 'utf8'); + res.json(JSON.parse(data)); +}); + +app.get('/api/teams/count', (req, res) => { + const data = fs.readFileSync( __dirname + '/team.json', 'utf8'); + + res.json(Object.keys(JSON.parse(data)).length); +}); + +app.get('/api/team/:id', (req, res) => { + const data = fs.readFileSync( __dirname + '/team.json', 'utf8'); + const id = req.params.id; + res.json(JSON.parse(data)[id-1]); +}); + +app.put('/api/team/reservation/:id', (req, res) => { + const json = JSON.parse(fs.readFileSync( __dirname + '/team.json', 'utf8')); + const id = req.params.id; + const team = json[id-1]; + team.isReserved = true; + fs.writeFileSync(__dirname + '/team.json', JSON.stringify(json), "utf8",) + res.status(201).send("L'équipe " + team.team + " a été correctement réservée."); +}); + +app.put('/api/team/cancel/:id', (req, res) => { + const json = JSON.parse(fs.readFileSync( __dirname + '/team.json', 'utf8')); + const id = req.params.id; + const team = json[id-1]; + team.isReserved = false; + fs.writeFileSync(__dirname + '/team.json', JSON.stringify(json), "utf8",) + res.status(201).send("La réservation de l'équipe " + team.team + " a été annulée."); +}); + diff --git a/tp1b/team.json b/tp1b/team.json new file mode 100644 index 0000000..5294310 --- /dev/null +++ b/tp1b/team.json @@ -0,0 +1 @@ +[{"id":1,"team":"Friendship is Magic","members":[{"name":"Twilight Sparkle","race":"unicorn","element":"magic"},{"name":"Applejack","race":"earth pony","element":"honesty"},{"name":"Rainbow Dash","race":"pegasus","element":"loyalty"},{"name":"Pinkie Pie","race":"earth pony","element":"laughter"},{"name":"Rarity","race":"unicorn","element":"generosity"},{"name":"Fluttershy","race":"pegasus","element":"kindness"}],"isReserved":false},{"id":2,"team":"New Generation","members":[{"name":"Sunny Starscout","race":"earth pony","element":"hope"},{"name":"Izzy Moonbow","race":"unicorn","element":"creativity"},{"name":"Pipp Petals","race":"pegasus","element":"confidence"},{"name":"Zipp Storm","race":"pegasus","element":"courage"},{"name":"Hitch Trailblazer","race":"earth pony","element":"kindness"}],"isReserved":false},{"id":3,"team":"Cutie Mark Crusaders","members":[{"name":"Apple Bloom","race":"earth pony","element":null},{"name":"Sweetie Belle","race":"unicorn","element":null},{"name":"Scootaloo","race":"pegasus","element":null}],"isReserved":false},{"id":4,"team":"Young 6","members":[{"name":"Sandbar","race":"earth pony","element":null},{"name":"Gallus","race":"griffon","element":null},{"name":"Smolder","race":"dragon","element":null},{"name":"Ocellus","race":"changeling","element":null},{"name":"Silverstream","race":"hippogriff","element":null},{"name":"Yona","race":"yak","element":null}],"isReserved":true}] \ No newline at end of file