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 Sequence Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 146 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,155 @@
const express = require("express")
const morgan = require("morgan")
const cors = require("cors")
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(morgan('dev'))
app.use(cors())
app.use(express.json())

// write your app code here

let nextId = 3

app.get('/contacts', (req, res) => {
res.send({ contacts: contacts })
})

app.post('/contacts', (req, res) => {
const newContact = { id: nextId, ...req.body }

contacts.push(newContact)

nextId++

res.status(201).send({ contact: newContact })
})

app.get('/contacts/:id', (req, res) => {
const matchingContact = contacts.find((element) => {
return element.id === parseInt(req.params['id'])
})

if (!matchingContact) {
res.sendStatus(404)
}

res.send({ contact: matchingContact })
})

app.delete('/contacts/:id', (req, res) => {
const matchingContact = contacts.find((element) => {
return element.id === parseInt(req.params['id'])
})

if (!matchingContact) {
res.sendStatus(404)
}

contacts = contacts.filter((element) => {
return !(element.id === parseInt(req.params['id']))
})

meetings = meetings.filter((element) => {
return !(element.contactId === parseInt(req.params['id']))
})

res.send({ contact: matchingContact })
})

app.put('/contacts/:id', (req, res) => {
let matchingContact = contacts.find((element) => {
return element.id === parseInt(req.params['id'])
})

if (!matchingContact) {
res.sendStatus(404)
}

Object.keys(matchingContact).forEach((element) => {
matchingContact[element] = req.body[element]
})

matchingContact.id = parseInt(req.params['id'])

res.send({ contact: matchingContact })
})

let nextMeetingId = 4

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

app.post('/contacts/:id/meetings', (req, res) => {
const newMeeting = {
id: nextMeetingId,
contactId: parseInt(req.params['id']),
...req.body,
}

meetings.push(newMeeting)

nextMeetingId++

res.status(201).send({ meeting: newMeeting })
})

app.get('/meetings/:id', (req, res) => {
const matchingMeeting = meetings.find((element) => {
return element.contactId === parseInt(req.params['id'])
})

if (!matchingMeeting) {
res.sendStatus(404)
}

res.send({ meeting: matchingMeeting })
})

app.delete('/meetings/:id', (req, res) => {
const matchingMeeting = meetings.find((element) => {
return element.id === parseInt(req.params['id'])
})

if (!matchingMeeting) {
res.sendStatus(404)
}

meetings = meetings.filter((element) => {
return !(element.id === parseInt(req.params['id']))
})

res.send({ meeting: matchingMeeting })
})

app.put('/meetings/:id', (req, res) => {

let matchingMeeting = meetings.find((element, index) => {
return element.id === parseInt(req.params['id'])
})

if (!matchingMeeting) {
res.sendStatus(404)
}

Object.keys(matchingMeeting).forEach((element) => {
if (req.body[element]) {
matchingMeeting[element] = req.body[element]
}
})

res.send({ meeting: matchingMeeting })
})

app.get('/contacts/:id/meetings', (req, res) => {
const contactsMeetings = meetings.filter((element) => {
return element.contactId === parseInt(req.params['id'])
})

res.send({ meetings: contactsMeetings })
})

module.exports = app