summaryrefslogtreecommitdiff
path: root/graphs/js/epiTinder/epiTinder.js
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/js/epiTinder/epiTinder.js')
-rw-r--r--graphs/js/epiTinder/epiTinder.js114
1 files changed, 114 insertions, 0 deletions
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,
+};