tp 1b fin

This commit is contained in:
OMGiTzPomPom 2023-03-24 10:36:36 +01:00
parent 81a7af99d8
commit 81ba27a2bb
3 changed files with 62 additions and 5 deletions

View File

@ -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) =>
`<div>${team.name} - ${team.id}</div>`
).join('');
teamsDiv.innerHTML = mmoContent;
let div = "";
teams.forEach((t) => {
div += `<div>Equipe ID${t.id} - Nom d'équipe ${t.team}</div>\n`
})
teamsDiv.innerHTML = div;
})
}).catch(error => console.error);
}

View File

@ -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.");
});

1
tp1b/team.json Normal file
View File

@ -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}]