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
10 changes: 4 additions & 6 deletions mailing-list-api/mailing-lists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module.exports = {
const mailingLists = {
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"],
};

export default mailingLists;
7 changes: 6 additions & 1 deletion mailing-list-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@module-node/mailing-list-api",
"version": "1.0.0",
"license": "CC-BY-SA-4.0",
"description": "You must update this package",
"description": "Mailing List API",
"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.4"
}
}
44 changes: 44 additions & 0 deletions mailing-list-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import express from "express";
import mailingLists from "./mailing-lists.js";

const app = express();
app.use(express.json());

// /lists - fetch all existing list names
app.get("/lists", (req,res) => {
const listsArray = Object.keys(mailingLists);
mailingLists
? res.status(200).send({mailingLists: listsArray})
: res.status(200).send({});
})

// get single list
app.get("/lists/:name", (req,res) => {
const name = req.params.name;
const list = mailingLists[name];
mailingLists
? res.status(200).send({name, members: list})
: res.status(404).send({error: "List not found"});
})

// delete single list
app.delete("/lists/:name", (req,res) => {
const name = req.params.name;
mailingLists[name]
? (delete mailingLists[name], res.status(200).send({message: `Deleted ${name} successfully`}))
: res.status(404).send({error: "Not found to delete"});
});

// update or create a single list
app.put("/lists/:name", (req, res) => {
const name = req.params.name;
const {members} = req.body;
const status = mailingLists[name] ? 200 : 201;
mailingLists[name] = members || [];
res.status(status).send();
});

const PORT = 5500;
app.listen(PORT, () => {
console.log('Server is running on http://localhost:${PORT}');
});
Loading