diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/js/epiTinder | |
add: graphs et rushs
Diffstat (limited to 'graphs/js/epiTinder')
| -rw-r--r-- | graphs/js/epiTinder/data.json | 118 | ||||
| -rw-r--r-- | graphs/js/epiTinder/epiTinder.js | 114 |
2 files changed, 232 insertions, 0 deletions
diff --git a/graphs/js/epiTinder/data.json b/graphs/js/epiTinder/data.json new file mode 100644 index 0000000..cd325a5 --- /dev/null +++ b/graphs/js/epiTinder/data.json @@ -0,0 +1,118 @@ +{ + "users": [ + { + "id": 0, + "name": "Puff", + "age": 2, + "description": "I like everyone who does javascript (I'm the mascot of the workshop)" + }, + { + "id": 1, + "name": "Bubble", + "age": 1, + "description": "I'm the real javascript mascot but don't tell puff he'll cry again" + }, + { + "id": 2, + "name": "Maxime Buisson", + "age": 40, + "description": "I already have my other half but I want to make friends." + }, + { + "id": 3, + "name": "Dorian Penso", + "age": 25, + "description": "Gym enthusiast by day, code lover by night, seeking a partner to write code and lift weights together." + }, + { + "id": 4, + "name": "Pierre-Alexis Valbrun", + "age": 32, + "description": "I'm a full-stack developer, I can handle both your front-end and back-end." + }, + { + "id": 5, + "name": "Paul Genillon", + "age": 26, + "description": "It’s not humid.. it’s raining" + }, + { + "id": 6, + "name": "Botumrath Morchoisne", + "age": 24, + "description": "What am I doing here??" + }, + { + "id": 7, + "name": "Pierre Santamaria", + "age": 22, + "description": "I'll love you like I love JavaScript, I won't get anything you tell me but it'll be passionate." + }, + { + "id": 8, + "name": "Baptiste Schaudel", + "age": 9, + "description": "I am operational to manage my team, I will let you discover if I am too with love." + }, + { + "id": 9, + "name": "Maxim Payeur", + "age": 8, + "description": "I lost my e, can you help me find it." + }, + { + "id": 10, + "name": "Vinh-Toan Phan", + "age": 17, + "description": "I'm an excelent cooker, do you want to be my cookware." + }, + { + "id": 11, + "name": "Angelina Kuntz", + "age": 44, + "description": "You can call me Ada" + }, + { + "id": 12, + "name": "Ewan Lemonnier", + "age": 19, + "description": "Barely legal" + }, + { + "id": 13, + "name": "Anaïs Druelle", + "age": 21, + "description": "What's the only part of a vegetable you can't eat?" + }, + { + "id": 14, + "name": "Mathéo Romé", + "age": 22, + "description": "I can’t sing but I will charm you anyway" + }, + { + "id": 15, + "name": "Noé Bauvineau", + "age": 58, + "description": "Searching for someone who can appreciate the elegance of a well-designed API." + }, + { + "id": 16, + "name": "Vincent Thirouin", + "age": 39, + "description": "I may not be able to dance, but I can sure write some smooth code." + }, + { + "id": 17, + "name": "Thibault Viennot", + "age": 30, + "description": "Searching for someone who knows how to have fun while debugging" + }, + { + "id": 18, + "name": "Pauline Chautard", + "age": 22, + "description": "I'm like a compiler, I can take your high-level needs and turn them into low-level satisfaction." + } + ] +}
\ No newline at end of file diff --git a/graphs/js/epiTinder/epiTinder.js b/graphs/js/epiTinder/epiTinder.js new file mode 100644 index 0000000..881a2c8 --- /dev/null +++ b/graphs/js/epiTinder/epiTinder.js @@ -0,0 +1,114 @@ +const fs = require("fs"); +const express = require("express"); + +function readUsersFromJSONFile(JSON_filename) { + /* + ** Return the list of users stored in the JSON file + ** JSON_filename: path to the JSON file + */ + const content = fs.readFileSync(JSON_filename, (err) => { + if (err) { + console.error(err); + return; + } + }); + + return JSON.parse(content).users; +} + +function writeUsersToJSONFile(JSON_filename, users) { + /* + ** Overwrite the given JSON_filename with the given + ** list of users. + ** JSON_filename: path to the JSON file + ** users : list of users objects + */ + const usersJSON = JSON.stringify({ users: users }); + + fs.writeFileSync(JSON_filename, usersJSON, (err) => { + if (err) { + console.error(err); + return; + } + }); +} +function epiTinderWebServer(host, port, filename) { + const app = express(); + + app.use(express.json()); + const users = readUsersFromJSONFile(filename); + let maxID = 0; + + if (users.length > 0) { + maxID = Math.max(...users.map((e) => e.id)) + 1; + } + + app.get("/", (req, res) => { + res.status(200).send({ message: "Hello World!" }); + }); + app.get("/users", (req, res) => { + res.status(200).send(users); + }); + app.post("/users", (req, res) => { + const new_user = { + id: maxID++, + name: req.body.name, + age: req.body.age, + description: req.body.description, + }; + + users.push(new_user); + writeUsersToJSONFile(filename, users); + res.status(201).send(new_user); + }); + app.get("/users/:id", (req, res) => { + const u = users.find((e) => e.id == req.params.id); + + if (u == undefined) { + res.status(404).send({ + message: `No user with id: ${req.params.id} found`, + }); + } else { + res.status(200).send(u); + } + }); + app.put("/users/:id", (req, res) => { + const u = users.findIndex((e) => e.id == req.params.id); + + if (u == -1) { + res.status(404).send({ + message: `No user with id: ${req.params.id} found`, + }); + } else { + users[u].name = req.body.name; + users[u].age = req.body.age; + users[u].description = req.body.description; + writeUsersToJSONFile(filename, users); + res.status(201).send(users[u]); + } + }); + app.delete("/users/:id", (req, res) => { + const u = users.findIndex((e) => e.id == req.params.id); + + if (u == -1) { + res.status(404).send({ + message: `No user with id: ${req.params.id} found`, + }); + } else { + const usr = users[u]; + + users.splice(u, 1); + writeUsersToJSONFile(filename, users); + res.status(200).send(usr); + } + }); + app.get("*", (req, res) => { + res.status(404).send({ message: "Not found" }); + }); + return app.listen(port, () => { + console.log("Server running at http://" + host + ":" + port + "/"); + }); +} +module.exports = { + epiTinderWebServer, +}; |
