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
34 changes: 17 additions & 17 deletions package-lock.json

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

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
"homepage": "https://github.com/boolean-uk/api-address-book#readme",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"express": "^4.19.2",
"morgan": "^1.10.0",
"nodemon": "^3.0.1"
},
"devDependencies": {
"jest": "^29.7.0",
"supertest": "^6.2.3"
}
},
"directories": {
"doc": "docs",
"test": "test"
},
"keywords": []
}
93 changes: 85 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,90 @@
const express = require("express")
const morgan = require("morgan")
const cors = require("cors")
const app = express()
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const app = express();
let contacts = require("../data/contacts.js");
let meetings = require("../data/meetings.js");

app.use(morgan("dev"))
app.use(cors())
app.use(express.json())
app.use(morgan("dev"));
app.use(cors());
app.use(express.json());

// write your app code here

app.get("/contacts", function (req, res) {
res.status(200).json({ contacts: contacts });
});

module.exports = app
app.post("/contacts", function (req, res) {
const { firstName, lastName, street, city, type, email, linkedin, twitter } =
req.body;
if (
!firstName ||
!lastName ||
!street ||
!city ||
!type ||
!email ||
!linkedin ||
!twitter
) {
return res.status(400);
}
const currentHighId = contacts.reduce((max, obj) => {
Copy link

Choose a reason for hiding this comment

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

nice, you could make this a generic function in a utils.js file :

function getNextId(items) {
   return items.reduce(...) + 1
}

and then call your function when needed, makes the code tidier

return obj.id > max ? obj.id : max;
}, 0);
req.body.id = currentHighId + 1;
contacts.push(req.body);
res.status(201).json({ contact: req.body });
});

app.get("/contacts/:id", function (req, res) {
const toFind = parseInt(req.params.id, 10);
Copy link

Choose a reason for hiding this comment

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

the name of this var is not great, it'd be better to use targetId

const index = contacts.findIndex((obj) => obj.id === toFind);
const foundContact = contacts[index];
if (foundContact) {
res.send({ contact: foundContact });
} else {
res.sendStatus(404);
}
});

app.delete("/contacts/:id", function (req, res) {
const toRemove = parseInt(req.params.id, 10);
const contactIndex = contacts.findIndex((obj) => obj.id === toRemove);
if (contactIndex === -1) {
return res.status(404).json({ error: "Contact not found" });
}

const deletedContact = contacts[contactIndex];
contacts = contacts.filter((obj) => obj.id !== toRemove);

res.status(200).json({ contact: deletedContact });
});

app.put("/contacts/:id", function (req, res) {
const id = parseInt(req.params.id, 10);
const contactIndex = contacts.findIndex((contact) => contact.id === id);
if (contactIndex === -1) {
return res.status(404).json({ error: "Contact not found" });
}

const { firstName, lastName, street, city, type, email, linkedin, twitter } =
req.body;
const updatedContact = {
...contacts[contactIndex],
firstName,
lastName,
street,
city,
type,
email,
linkedin,
twitter,
};
contacts[contactIndex] = updatedContact;

res.status(200).json({ contact: updatedContact });
});

module.exports = app;