Created
November 2, 2023 18:53
-
-
Save Nostromos/53aedfd296a85f8cf43bc44135561919 to your computer and use it in GitHub Desktop.
Off-Platform Project: Boss Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const apiRouter = express.Router(); | |
const minionsRouter = require('./minionsRouter'); | |
apiRouter.use('/minions', minionsRouter); | |
const ideasRouter = require('./ideasRouter'); | |
apiRouter.use('/ideas', ideasRouter); | |
const meetingsRouter = require('./meetingsRouter'); | |
apiRouter.use('/meetings', meetingsRouter); | |
module.exports = apiRouter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const checkMillionDollarIdea = (req, res, next) => { | |
const numWeeks = Number(req.body.numWeeks); | |
const weeklyRevenue = Number(req.body.weeklyRevenue); | |
const totalYield = numWeeks * weeklyRevenue; | |
if (!req.body.numWeeks || !req.body.weeklyRevenue || isNaN(req.body.numWeeks) || isNaN(req.body.weeklyRevenue) || totalYield < 1000000) { | |
res.status(400).send(); | |
} else { | |
next(); | |
} | |
}; | |
module.exports = checkMillionDollarIdea; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Imports & Setup | |
*/ | |
const express = require('express'); | |
const { | |
getAllFromDatabase, | |
getFromDatabaseById, | |
addToDatabase, | |
updateInstanceInDatabase, | |
deleteFromDatabasebyId, | |
} = require('./db.js'); | |
const checkMillionDollarIdea = require('./checkMillionDollarIdea'); | |
const ideasRouter = express.Router(); // Set up the router | |
/* | |
* Param Parsing | |
*/ | |
ideasRouter.param('ideaId', (req, res, next, id) => { // Middleware to get ideaId param | |
const testIfValid = getFromDatabaseById('ideas', id); | |
if (isNaN(id)) { | |
res.status(404).send(); | |
} else if (testIfValid == null) { | |
res.status(404).send(); | |
} else { | |
req.id = id; | |
next(); | |
} | |
}); | |
/* | |
* Routes | |
*/ | |
ideasRouter.route('/') | |
.get((req, res, next) => { // GET /api/ideas to get an array of all ideas. | |
const allIdeas = getAllFromDatabase('ideas'); | |
res.status(200).send(allIdeas); | |
}) | |
.post(checkMillionDollarIdea, (req, res, next) => { // DONE POST /api/ideas to create a new idea and save it to the database. | |
const newIdea = req.body; | |
const addIdeaToDatabase = addToDatabase('ideas', newIdea); | |
res.status(201).send(addIdeaToDatabase); | |
}); | |
ideasRouter.route('/:ideaId') | |
.get((req, res, next) => { // DONE GET /api/ideas/:ideaId to get a single idea by id. | |
const ideaToGet = req.id; | |
const ideaWeGot = getFromDatabaseById('ideas', ideaToGet); | |
res.status(200).send(ideaWeGot); | |
}) | |
.put(checkMillionDollarIdea, (req, res, next) => { // DONE PUT /api/ideas/:ideaId to update a single idea by id. | |
const updatedIdeaBody = req.body; | |
const ideaToUpdate = req.id; | |
const updatedIdea = updateInstanceInDatabase('ideas', updatedIdeaBody) | |
res.status(200).send(updatedIdea); | |
}) | |
.delete((req, res, next) => { // DONE DELETE /api/ideas/:ideaId to delete a single idea by id. | |
const ideaToDelete = req.id; | |
const deletedIdea = deleteFromDatabasebyId('ideas', ideaToDelete.toString()); | |
res.status(204).send(deletedIdea); | |
}); | |
module.exports = ideasRouter; // export the router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Imports | |
*/ | |
const express = require('express'); | |
const { | |
createMeeting, | |
getAllFromDatabase, | |
addToDatabase, | |
deleteAllFromDatabase, | |
} = require('./db.js'); | |
const meetingsRouter = express.Router(); | |
/* | |
* Routes | |
*/ | |
meetingsRouter.route('/') | |
.get((req, res, next) => { // get all meetings | |
const allMeetings = getAllFromDatabase('meetings'); | |
res.status(200).send(allMeetings); | |
}) | |
.post((req, res, next) => { // create a meeting and add it | |
const newMeeting = createMeeting(); | |
const meetingToCreate = addToDatabase('meetings', newMeeting); | |
res.status(201).send(meetingToCreate); | |
}) | |
.delete((req, res, next) => { // delete all meetings | |
const allDeletedMeetings = deleteAllFromDatabase('meetings'); | |
res.status(204).send(allDeletedMeetings); | |
}); | |
module.exports = meetingsRouter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Imports & Setup | |
*/ | |
const express = require('express'); | |
const { | |
getAllFromDatabase, | |
getFromDatabaseById, | |
addToDatabase, | |
updateInstanceInDatabase, | |
deleteFromDatabasebyId, | |
} = require('./db.js'); | |
const minionsRouter = express.Router({ mergeParams: true }); | |
/* | |
* Param Parsing | |
*/ | |
minionsRouter.param('minionId', (req, res, next, id) => { // Get the ID from params, test validity, and attach to request | |
const minionId = id; | |
const minionToGet = getFromDatabaseById('minions', minionId.toString()); | |
if (isNaN(minionId)) { | |
res.status(404).send('The minion ID you sent is not a number.') | |
} else if (minionToGet == null) { | |
res.status(404).send('There is no such minion.') | |
} else { | |
req.minionId = minionId; | |
next(); | |
} | |
}); | |
minionsRouter.param('workId', (req, res, next, id) => { // Get workId from params, test validity, and attach to request | |
const workId = id; | |
const minionId = req.params.minionId; | |
const findable = getFromDatabaseById('work', workId); | |
if (isNaN(workId) || findable == null) { | |
res.sendStatus(404); | |
} else if (findable.minionId !== minionId) { | |
res.status(400).send('Minion ids dont match'); | |
} else { | |
req.workId = workId; | |
next(); | |
} | |
}); | |
/* | |
* Routes | |
*/ | |
minionsRouter.route('/') | |
.get((req, res, next) => { // DONE GET /api/minions to get an array of all minions. | |
const allMinions = getAllFromDatabase('minions'); | |
res.status(200).send(allMinions); | |
}) | |
.post((req, res, next) => { // DONE POST /api/minions to create a new minion and save it to the database. | |
const newMinion = req.body; | |
const minionToAdd = addToDatabase('minions', newMinion); | |
res.status(201).send(minionToAdd); | |
}); | |
minionsRouter.route('/:minionId') | |
.get((req, res, next) => { // DONE GET /api/minions/:minionId to get a single minion by id. | |
const minionToGet = getFromDatabaseById('minions', req.minionId.toString()); | |
if (isNaN(req.minionId) || minionToGet == null) { | |
res.sendStatus(404); | |
} else { | |
res.status(200).send(minionToGet); | |
} | |
}) | |
.put((req, res, next) => { // DONE PUT /api/minions/:minionId to update a single minion by id. | |
const requestedMinionToUpdate = req.body; | |
const minionToUpdate = updateInstanceInDatabase('minions', requestedMinionToUpdate); | |
res.status(200).send(minionToUpdate); | |
}) | |
.delete((req, res, next) => { // DONE DELETE /api/minions/:minionId to delete a single minion by id. | |
const deleteMinion = deleteFromDatabasebyId('minions', req.minionId.toString()); | |
if (deleteMinion) { | |
res.status(204).send('Minion Deleted'); | |
} else { | |
res.status(404).send('Attempted to delete minion but not found'); | |
} | |
}); | |
minionsRouter.route('/:minionId/work') | |
.get((req, res, next) => { | |
const allWork = getAllFromDatabase('work'); | |
const specifiedMinionWork = allWork.filter((workItem) => { | |
return workItem.minionId === req.minionId; | |
}); | |
res.status(200).send(specifiedMinionWork); | |
}) | |
.post((req, res, next) => { | |
const workToAdd = req.body; | |
const workAdded = addToDatabase('work', workToAdd); | |
res.status(201).send(workAdded); | |
}); | |
minionsRouter.route('/:minionId/work/:workId') | |
.put((req, res, next) => { | |
const workToUpdate = req.body; | |
const updatedWork = updateInstanceInDatabase('work', workToUpdate); | |
res.status(200).send(updatedWork); | |
}) | |
.delete((req, res, next) => { | |
const workToDelete = req.workId; | |
const deletedWork = deleteFromDatabasebyId('work', workToDelete); | |
if (deletedWork) { | |
res.status(204).send(); | |
} else { | |
res.status(404).send('Couldnt delete work'); | |
} | |
}); | |
module.exports = minionsRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! A lot of this work ended up being needlessly complex compared to the sample solution. A lot of the test messaging wasn't descriptive so I had to break things down into component parts. If I rewrote this, it would look pretty similar to the sample solution tbh.