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
Binary file added request-response-cycle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions src/controllers/contacts/contactControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const contacts = require("../../../data/contacts.js")
const meetings = require("../../../data/meetings.js")
const {
verifyContactBody,
findContactById,
findContactMeetings,
deleteContactMeetings,
removeContact,
foundContactId,
replaceUpdatedContact,
verifyResultFound,
verifyMeetingLength,
verifyMeetingBody,
pushData
} = require("../../domain/contacts/contactRepository.js")

const getAllContacts = (req, res) => {
res.json({
contacts: contacts,
})
}

const createContact = (req, res) => {
const contact = req.body

verifyContactBody(contact, res)

const contactId = { id: contacts.length + 1, ...contact }

pushData(contacts, contactId)

res.status(201).json({
contact: contactId,
})
}

const getContactById = (req, res) => {
const id = Number(req.params.id)
const result = findContactById(contacts, id)

verifyResultFound(result, res, id)

res.json({
contact: result,
})
}

const deleteContact = (req, res) => {
const id = Number(req.params.id)
const result = findContactById(contacts, id)
const contactMeetings = findContactMeetings(meetings, result)

verifyResultFound(result, res, id)

deleteContactMeetings(contactMeetings, meetings, result)

removeContact(contacts, result)

res.json({
contact: result,
})
}

const updateContact = (req, res) => {
const contact = req.body
const id = Number(req.params.id)
const result = foundContactId(contacts, id)

verifyResultFound(result, res, id)

verifyContactBody(contact, res)

const contactId = { id: result.id, ...contact }

replaceUpdatedContact(contacts, result, contactId)

res.json({
contact: contactId,
})
}

const getContactMeetings = (req, res) => {
const id = Number(req.params.id)
const contact = foundContactId(contacts, id)

verifyResultFound(contact, res, id)

const meeting = findContactMeetings(meetings, contact)

verifyMeetingLength(meeting, res, contact)

res.json({
meetings: meeting,
})
}

const createContactMeeting = (req, res) => {
const id = Number(req.params.id)
const contact = foundContactId(contacts, id)
const meeting = req.body

verifyResultFound(contact, res, id)

verifyMeetingBody(meeting, res)

const meetingIDs = {
...meeting,
contactId: contact.id,
id: meetings.length + 1,
}

pushData(meetings, meetingIDs)

res.status(201).json({
meeting: meetingIDs,
})
}

module.exports = {
getAllContacts,
createContact,
getContactById,
deleteContact,
updateContact,
getContactMeetings,
createContactMeeting
}
67 changes: 67 additions & 0 deletions src/controllers/meetings/meetingControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const meetings = require("../../../data/meetings.js")
const { verifyMeetingBody } = require("../../domain/contacts/contactRepository.js")
const {
foundMeetings,
verifyMeetingFound,
removeMeeting,
replaceUpdatedMeeting
} = require("../../domain/meetings/meetingRepository.js")

const getAllMeetings = (req, res) => {
res.json({
meetings: meetings,
})
}

const getMeetingById = (req, res) => {
const id = Number(req.params.id)
const result = foundMeetings(meetings, id)

verifyMeetingFound(result, res, id)

res.json({
meeting: result,
})
}

const deleteMeeting = (req, res) => {
const id = Number(req.params.id)
const result = foundMeetings(meetings, id)

verifyMeetingFound(result, res, id)

removeMeeting(meetings, result)

res.json({
meeting: result,
})
}

const updateMeeting = (req, res) => {
const id = Number(req.params.id)
const result = foundMeetings(meetings, id)
const meeting = req.body

verifyMeetingFound(result, res, id)

verifyMeetingBody(meeting, res)

const meetingIDs = {
...meeting,
contactId: result.contactId,
id: result.id,
}

replaceUpdatedMeeting(meetings, result, meetingIDs)

res.json({
meeting: meetingIDs,
})
}

module.exports = {
getAllMeetings,
getMeetingById,
deleteMeeting,
updateMeeting
}
Loading