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: 3 additions & 6 deletions mailing-list-api/mailing-lists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module.exports = {
export default {
staff: ["talea@techtonica.org", "michelle@techtonica.org"],
"cohort-h1-2020": [
"ali@techtonica.org",
"humail@techtonica.org",
"khadar@techtonica.org",
],
"cohort-h1-2020": ["ali@techtonica.org", "humail@techtonica.org", "khadar@techtonica.org"],
};

5 changes: 5 additions & 0 deletions mailing-list-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"license": "CC-BY-SA-4.0",
"description": "You must update this package",
"type": "module",
"scripts": {
"test": "jest"
},
Expand All @@ -16,5 +17,9 @@
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"jest": "^26.6.3"
},
"dependencies": {
"express": "^4.19.2",
"nodemon": "^3.1.0"
}
}
60 changes: 60 additions & 0 deletions mailing-list-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import express from "express";
import mailinglist from "./mailing-lists.js";
const PORT = 3000;

const app = express();
app.use(express.json());
app.disable("x-powered-by");
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 particulaur reason for this line of code? It's a good question to ask yourself whenever you find anything odd in code.


const mailList = mailinglist;
Copy link

Choose a reason for hiding this comment

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

Another line that doesn't make too much sense to me - is there any particuluar reason that the var needs reassigned?


// fetch all list names
app.get("/lists", (req, res) => {
const listsArray = Object.keys(mailList);
res.status(200).json(listsArray);
});
Copy link

Choose a reason for hiding this comment

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

Good work, appreciate the comments - makes it easier to skim read


// get a single name
app.get("/lists/:name", (req, res) => {
const name = req.params.name;
const list = mailList[name];
if (list) {
res.status(200).json({ name, members: list });
} else {
res.status(404).json({ error: "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.

👍


// delete a name
app.delete("/lists/:name", (req, res) => {
const name = req.params.name;
if (mailList[name]) {
delete mailList[name];
res.status(200).json({ message: "Successfully deleted" });
} else {
res.status(404).json({ message: "Name not found for delete" });
}
});
Copy link

Choose a reason for hiding this comment

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

👍


// add or update a list
app.put("/lists/:name", (req, res) => {
const pathName = req.params.name;
const { name, members } = req.body;

if (pathName !== name) {
res.status(400).json({ error: "Name in path does not match name in body" });
return;
}

if (mailList[name]) {
mailList[name] = members;
res.status(200).json({ message: "List updated successfully." });
} else {
mailList[name] = members;
res.status(201).json({ message: "New list created successfully." });
}
});
Copy link

Choose a reason for hiding this comment

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

Amazing, nice to see you've done the stretch goal as well!


app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Loading