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
9 changes: 7 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 server.js"
},
"repository": {
"type": "git",
Expand All @@ -15,6 +16,10 @@
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"jest": "^26.6.3"
"jest": "^26.6.3",
"nodemon": "^3.1.0"
},
"dependencies": {
"express": "^4.19.2"
}
}
50 changes: 50 additions & 0 deletions mailing-list-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const express = require("express");
const data = require("./mailing-lists");
const app = express();
app.use(express.json());
const lists = new Map(Object.entries(data));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particluar reason you wanted to copy the data into a diffrent object?

app.get("/lists", (req, res) => {
const listsArray = Array.from(lists.keys());
if (listsArray.length > 0) {
res.status(200).send(listsArray);
} else {
res.status(200).send({});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct I'm afraid - the readme asked for an empty array not an empty object

}
});
app.get("/lists/:name", (req, res) => {
const name = req.params.name.toLowerCase();
const listsArray = lists.get(name);
if (listsArray) {
res.status(200).send(listsArray);
} else {
res.status(404).send("list not found");
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

app.delete("/lists/:name", (req, res) => {
const name = req.params.name.toLowerCase();
if (lists.has(name)) {
lists.delete(name);
const updatedList = Array.from(lists.keys());
res.status(200).send(updatedList);
} else {
res.status(404).send("list not found");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work - apprecate the good error checking you have here :)

}
});
app.put("/lists/:name", (req, res) => {
const name = req.params.name.toLowerCase();
if (lists.has(name)) {
const putListBody = req.body;
lists.set(name, putListBody);
const updatedList = Array.from(lists.keys());
res.status(200).send(updatedList);
} else {
const putListBody = req.body;
console.log(putListBody);
lists.set(name, putListBody);
const updatedList = Array.from(lists.keys());
res.status(201).send(updatedList);
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work :)

app.listen(3000, () => {
console.log("Server is listen to 3000");
});
147 changes: 94 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.