Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions mailing-list-api/mailing-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,22 @@ module.exports = {
"khadar@techtonica.org",
],
};

// import express from "express";
const express = require("express");

const app = express();
port = 4000;
app.listen(port, () => {
console.log(`listening on port: ${port}`);
});

app.get("/", (req, res) => {
res.send(mails);
});

app.get("/lists", (req, res) => {
const list = new Map();
const listOfName = Array.from(mails);
res.send(listOfName);
});
8 changes: 6 additions & 2 deletions mailing-list-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "CC-BY-SA-4.0",
"description": "You must update this package",
"scripts": {
"test": "jest"
"test": "jest",
"start": "nodemon"
},
"repository": {
"type": "git",
Expand All @@ -16,5 +17,8 @@
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"jest": "^26.6.3"
}
},
"main": "mailing-lists.js",
"keywords": [],
"author": ""
}
81 changes: 81 additions & 0 deletions mailing-list-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const express = require("express");
const data = require("./mailing-lists");
const app = express();
app.use(express.json());
port = 3000;

const list = new Map(Object.entries(data));

app.listen(port, () => {
console.log(`listening on porttt: ${port}`);
});

app.get("/", (req, res) => {
res.status(200).send("This is a Mailing-list-api project!");
});

app.get("/list", (req, res) => {
const arrayOfList = Array.from(list.keys());

if (arrayOfList.length > 0) {
res.status(200).send(arrayOfList);
} else {
res.status(200).send([]);
}
});

app.get("/list/:name", (req, res) => {
const endpointName = req.params.name;

// const listOfKeys = Array.from(list.keys());
const hasEndpontName = list.has(endpointName);

if (hasEndpontName) {
const emailsOfMemebers = list.get(endpointName);
const responseBody = {
name: endpointName,
members: emailsOfMemebers,
};
res.status(200).send(responseBody);
} else {
res.status(404).send("404 , Data not found!");
}
});

app.delete("/list/:name", (req, res) => {
const nameOfList = req.params.name;

const hasDelted = list.delete(nameOfList);

if (hasDelted) {
res.status(200).send("Delte was successful");
} else {
res.status(404).send("item wasn't found");
}
});

app.put("/list/:name", (req, res) => {
const nameInParams = req.params.name;

const nameInBody = req.body.name;
const newMembers = req.body.members;

if (nameInParams != nameInBody || !nameInBody || newMembers.length == 0) {
res
.status(409)
.send(
"List can not be updated due to the conflict in the name of path and updated request!"
);
} else {
const hasTheName = list.has(nameInBody);
if (hasTheName) {
const oldMembers = list.get(nameInParams);
const updatedmembers = oldMembers.concat(newMembers);
list.set(nameInBody, updatedmembers);
res.status(200).send("Updated successfully");
} else {
list.set(nameInBody, newMembers);
res.status(201).send("New list added!");
}
}
});
Loading