56 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2023-03-24 10:36:36 +01:00
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.");
});