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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"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"
},
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const app = require('./server.js')
const port = 3030
const app = require('./server');
const port = process.env.PORT || 3000;

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}/`)
})
console.log(`Server is running on http://localhost:${port}`);
});
109 changes: 100 additions & 9 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,104 @@
const express = require("express")
const morgan = require("morgan")
const cors = require("cors")
const app = express()
const express = require('express');
const app = express();

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

// write your app code here
// Data
let contacts = [
{ id: 1, firstName: "John", lastName: "Carmack" },
{ id: 2, firstName: "Grace", lastName: "Hopper" }
];

let meetings = [
{ id: 1, name: "a test meeting about life", contactId: 1 },
{ id: 2, name: "another test meeting for wondering about existence", contactId: 2 },
{ id: 3, name: "a new meeting for the hopeful", contactId: 1 }
];

module.exports = app
// Routes for contacts
app.get('/contacts', (req, res) => {
res.json({ contacts });
});

app.get('/contacts/:id', (req, res) => {
const contact = contacts.find(c => c.id === parseInt(req.params.id));
if (!contact) {
return res.status(404).send('Contact not found');
}
res.json({ contact });
});

app.post('/contacts', (req, res) => {
const newContact = { ...req.body, id: contacts.length + 1 };
contacts.push(newContact);
res.status(201).json({ contact: newContact });
});

app.put('/contacts/:id', (req, res) => {
const contact = contacts.find(c => c.id === parseInt(req.params.id));
if (!contact) {
return res.status(404).send('Contact not found');
}
Object.assign(contact, req.body);
res.json({ contact });
});

app.delete('/contacts/:id', (req, res) => {
const contactIndex = contacts.findIndex(c => c.id === parseInt(req.params.id));
if (contactIndex === -1) {
return res.status(404).send('Contact not found');
}
const [deletedContact] = contacts.splice(contactIndex, 1);
meetings = meetings.filter(m => m.contactId !== deletedContact.id);
res.json({ contact: deletedContact });
});

// Routes for meetings
app.get('/meetings', (req, res) => {
res.json({ meetings });
});

app.get('/meetings/:id', (req, res) => {
const meeting = meetings.find(m => m.id === parseInt(req.params.id));
if (!meeting) {
return res.status(404).send('Meeting not found');
}
res.json({ meeting });
});

app.post('/contacts/:id/meetings', (req, res) => {
const contactId = parseInt(req.params.id);
const contact = contacts.find(c => c.id === contactId);
if (!contact) {
return res.status(404).send('Contact not found');
}
const newMeeting = { ...req.body, id: meetings.length + 1, contactId };
meetings.push(newMeeting);
res.status(201).json({ meeting: newMeeting });
});

app.put('/meetings/:id', (req, res) => {
const meeting = meetings.find(m => m.id === parseInt(req.params.id));
if (!meeting) {
return res.status(404).send('Meeting not found');
}
Object.assign(meeting, req.body);
res.json({ meeting });
});

app.delete('/meetings/:id', (req, res) => {
const meetingIndex = meetings.findIndex(m => m.id === parseInt(req.params.id));
if (meetingIndex === -1) {
return res.status(404).send('Meeting not found');
}
const [deletedMeeting] = meetings.splice(meetingIndex, 1);
res.json({ meeting: deletedMeeting });
});

app.get('/contacts/:id/meetings', (req, res) => {
const contactId = parseInt(req.params.id);
const contactMeetings = meetings.filter(m => m.contactId === contactId);
res.json({ meetings: contactMeetings });
});

module.exports = app;