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
Binary file added contacts sequence diagram-2.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions data/deletedContacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const deletedContacts = []

module.exports = deletedContacts
3 changes: 3 additions & 0 deletions data/deletedMeetings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const deletedMeetings = []

module.exports = deletedMeetings
99 changes: 68 additions & 31 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"homepage": "https://github.com/boolean-uk/api-address-book#readme",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"morgan": "^1.10.0",
"nodemon": "^3.0.1"
"express": "^4.19.2",
"morgan": "^1.10.0"
},
"devDependencies": {
"jest": "^29.7.0",
"nodemon": "^3.1.3",
"supertest": "^6.2.3"
}
}
6 changes: 6 additions & 0 deletions src/filterByContactId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function filterByContactId(data, id) {
const found = data.filter((d) => d.contactId === id)
return found
}

module.exports = filterByContactId
6 changes: 6 additions & 0 deletions src/findID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function findID(data, id) {
const found = data.find((d) => d.id === id)
return found
}

module.exports = findID
6 changes: 6 additions & 0 deletions src/newID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function newID(data) {
const newID = data.reverse().find((d) => d.id)
return newID.id +1
}

module.exports = newID
225 changes: 224 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,235 @@ const express = require("express")
const morgan = require("morgan")
const cors = require("cors")
const app = express()
const contacts = require('../data/contacts.js')
const meetings = require('../data/meetings.js')
const findID = require('./findID.js')
const filterByContactId = require('./filterByContactId.js')
const deletedContacts = require('../data/deletedContacts.js')
const deletedMeetings = require("../data/deletedMeetings.js")
const newID = require("./newID.js")


let newContact = {
id: 0,
firstName: 'string',
lastName: 'string',
street: 'string',
city: 'string',
type: 'string',
email: 'string',
linkedin: 'string',
twitter: 'string'
}

const idError = {
error: 'invalid-ID',
message: 'ID provided does not exist, please ensure a valid ID is provided'
}

let newMeeting = {
id: 0,
contactId: 'string',
name: 'string'
}

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

// write your app code here
app.get('/contacts', (req, res) => {
res.status(200).json({
contacts: contacts
})
})

app.post('/contacts', (req, res) => {
const firstName = req.body.firstName
const lastName = req.body.lastName
const street = req.body.street
const city = req.body.city
const type = req.body.type
const email = req.body.email
const linkedin = req.body.linkedin
const twitter = req.body.twitter

newContact.id = newID(contacts)
newContact.firstName = firstName
newContact.lastName = lastName
newContact.street = street
newContact.city = city
newContact.type = type
newContact.email = email
newContact.linkedin = linkedin
newContact.twitter = twitter

contacts.push(newContact)
res.status(201).json({
contact: newContact
})
})

app.get('/contacts/:id', (req, res) => {
const id = Number(req.params.id)
const found = findID(contacts, id)

if(!found) {
return res.status(404).json({
idError
})
}

res.status(200).json({
contact: found
})
})

app.delete('/contacts/:id', (req, res) => {
const id = Number(req.params.id)
const found = findID(contacts, id)
const findMeetings = findID(meetings, id)


if(!found) {
return res.status(404).json({
idError
})
}
const index = contacts.indexOf(found)
const indexOfContact = meetings.indexOf(findMeetings)

deletedContacts.push(found)
contacts.splice(index, 1)
meetings.splice(indexOfContact, 1)
res.status(200).json({
contact: found
})
})

app.put('/contacts/:id', (req, res) => {
const id = Number(req.params.id)
const found = findID(contacts, id)

if(!found) {
return res.status(404).json({
idError
})
}
const firstName = req.body.firstName
const lastName = req.body.lastName
const street = req.body.street
const city = req.body.city
const type = req.body.type
const email = req.body.email
const linkedin = req.body.linkedin
const twitter = req.body.twitter


found.firstName = firstName
found.lastName = lastName
found.street = street
found.city = city
found.type = type
found.email = email
found.linkedin = linkedin
found.twitter = twitter

res.status(200).json({
contact: found
})
})

app.get('/meetings', (req, res) => {
res.status(200).json({
meetings: meetings
})
})

app.get('/meetings/:id', (req, res) => {
const id = Number(req.params.id)
const found = findID(meetings, id)

if(!found) {
return res.status(404).json({
idError
})
}

res.status(200).json({
meeting: found
})
})

app.delete('/meetings/:id', (req, res) => {
const id = Number(req.params.id)
const found = findID(meetings, id)

if(!found) {
return res.status(404).json({
idError
})
}

const index = meetings.indexOf(found)
deletedMeetings.push(found)
meetings.splice(index, 1)

res.status(200).json({
meeting: found
})
})

app.put('/meetings/:id', (req, res) => {
const id = Number(req.params.id)
const foundMeeting = findID(meetings, id)
const foundContact = findID(contacts, id)

if(!foundMeeting) {
return res.status(404).json({
idError
})
}

foundMeeting.contactId = foundContact.id
foundMeeting.name = req.body.name
res.status(200).json({
meeting: foundMeeting
})
})

app.get('/contacts/:id/meetings', (req, res) => {
const id = Number(req.params.id)
const foundMeeting = filterByContactId(meetings, id)

if(!foundMeeting) {
return res.status(404).json({
idError
})
}

res.status(200).json({
meetings: foundMeeting
})
})

app.post('/contacts/:id/meetings', (req, res) => {
const id = Number(req.params.id)
const foundContact = findID(contacts, id)

if(!foundContact) {
return res.status(404).json({
idError
})
}

newMeeting.id = newID(meetings)
newMeeting.contactId = foundContact.id
newMeeting.name = req.body.name

meetings.push(newMeeting)
res.status(201).json({
meeting: newMeeting
})
})

module.exports = app