Skip to content

Code Review #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,27 @@ We have provided test data for all the resources.
Be prepared to demonstrate your understanding of this week's concepts by answering questions on the following topics. You might prepare by writing down your own answers before hand.

1. The core features of Node.js and Express and why they are useful.
1. Understand and explain the use of Middleware.
1. The basic principles of the REST architectural style.
1. Understand and explain the use of Express Routers.
1. Describe tooling used to manually test the correctness of an API.

The core features of Node.js are that it allows for asynchronous API calls and provides a faster response.
Express allows us to use middleware and routing to better handle client requests to our apis.

2. Understand and explain the use of Middleware.

Middleware is software that intercepts or act as an intermediary with in our software programs.
Middleware is highly useful in that it can help in wide array of things, from validation to routing.

3. The basic principles of the REST architectural style.

The basic principles of the REST style are that it is stateless, a Uniform interface, cacheable, layered, and client-server based.
REST stands for representational state transfer, which implies that there is no need for state since all that is handled by Uniform resource identifiers
that provide information from a server.

4. Understand and explain the use of Express Routers.

Express router allows us to deviate calls to specific paths within our api's to different parts of our code.
This in turn allows us to handle various types of calls to specifically provide accurate information to each individual use case.

5. Describe tooling used to manually test the correctness of an API.

Manual test tooling for API, include software such as postman and the debugger in vscode.
The ses programs allow us to test our API response and readjust or fix any specific errors that arise within each specific call path.
39 changes: 39 additions & 0 deletions api/actions/actions-middlware.js
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
// add middlewares here related to actions
const Actions = require('../actions/actions-model')

function handleError(err, req, res, next) {
res.status(err.status || 500).json({
message: err.message
})
}

function validateID (req, res, next) {
Actions.get(req.params.id)
.then(action => {
if (action) {
req.action = action
next()
} else {
next({ status: 404, message: 'Project not found'})
}
})
.catch(next)
}

function validatePost (req, res, next) {
const { description, notes } = req.body
if (!description) {
next({status: 400, message: 'Description is missing'})
} else if (description.trim().length > 128) {
next({status: 400, message: 'Description is limited to 128 chars'})
} else if (!notes) {
next({status: 400, message: 'Use notes to explain how to complete the action'})
} else {
next()
}
}

module.exports = {
handleError,
validateID,
validatePost
}
61 changes: 60 additions & 1 deletion api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
// Write your "actions" router here!
const express = require('express');

const {
handleError,
validateID,
validatePost
} = require('./actions-middlware');

const Actions = require('./actions-model');

const router = express.Router();

//[GET] /api/actions
router.get('/', (req, res, next) => {
Actions.get()
.then(actions => {
res.status(200).send(actions)
})
.catch(next)
});

//[GET] /api/actions/:id
router.get('/:id', validateID, (req, res) => {
res.status(200).send(req.action)
})

//[POST] /api/actions
router.post('/', validatePost, (req, res, next) => {
const newAction = {
...req.body,
project_id: req.params.id
}
Actions.insert(newAction)
.then(action => {
res.status(201).json(action);
})
.catch(next)
})

//[PUT] /api/actions/:id
router.put('/:id', validatePost, validateID, (req, res, next) => {
Actions.update(req.params.id, req.body)
.then(action => {
res.status(200).json(action);
})
.catch(next)
});

//[DELETE] /api/actions/:id
router.delete('/:id', validateID, (req, res, next) => {
Actions.remove(req.params.id)
.then(() => {
res.status(200);
})
.catch(next);
});

router.use(handleError);

module.exports = router;
51 changes: 51 additions & 0 deletions api/projects/projects-middleware.js
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
// add middlewares here related to projects
const Projects = require('../projects/projects-model');

function handleError(err, req, res) {
res.status(err.status || 500).json({
message: err.message
})
}

function validateID (req, res, next) {
Projects.get(req.params.id)
.then(project => {
if (project) {
req.project = project
next()
} else {
next({ status: 404, message: 'Project not found'})
}
})
.catch(next)
}

function validatePost (req, res, next) {
const { name, description } = req.body
if (!name) {
next({ status: 400, message: 'Missing name'})
} else if (!description) {
next({ status: 400, message: 'Missing description'})
} else {
next()
}
}

function validateUpdatedPost (req, res, next) {
const { name, description, completed } = req.body
if (!name) {
next({ status: 400, message: 'Missing name'})
} else if (!description) {
next({ status: 400, message: 'Missing description'})
} else if (completed === undefined ) {
next({ status: 400, message: 'Project completion status is missing'})
} else {
next()
}
}

module.exports = {
handleError,
validateID,
validatePost,
validateUpdatedPost
}
67 changes: 66 additions & 1 deletion api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
// Write your "projects" router here!
const express = require('express');

const {
handleError,
validateID,
validatePost,
validateUpdatedPost
} = require('./projects-middleware');

const Projects = require('./projects-model');

const router = express.Router();

//[GET] /api/projects
router.get('/', (req, res, next) => {
Projects.get()
.then(projects => {
res.status(200).json(projects)
})
.catch(next);
});

//[GET] /api/projects/:id
router.get('/:id', validateID, (req, res) => {
res.status(200).json(req.project)
})

//[POST] /api/projects
router.post('/', validatePost, (req, res, next) => {
Projects.insert(req.body)
.then(project => {
res.status(201).json(project)
})
.catch(err => next(err));
})

//[PUT] /api/projects/:id
router.put('/:id', validateUpdatedPost, validateID, (req, res, next) => {
Projects.update(req.params.id, req.body)
.then(project => {
res.status(200).json(project);
})
.catch(next);
});

//[DELETE] /api/projects/:id
router.delete('/:id', validateID, (req, res, next) => {
Projects.remove(req.params.id)
.then(() => {
res.status(200);
})
.catch(next);
});

//[GET] /api/projects/:id/actions
router.get('/:id/actions', validateID, (req, res, next) => {
Projects.getProjectActions(req.params.id)
.then(actions => {
res.status(200).send(actions);
})
.catch(err => next(err));
})

router.use(handleError);

module.exports = router;
11 changes: 7 additions & 4 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const express = require('express');

const projectsRouter = require('./projects/projects-router');
const actionsRouter = require('./actions/actions-router');

const server = express();
server.use(express.json());

// Configure your server here
// Build your actions router in /api/actions/actions-router.js
// Build your projects router in /api/projects/projects-router.js
// Do NOT `server.listen()` inside this file!
server.use('/api/projects', projectsRouter);
server.use('/api/actions', actionsRouter);

module.exports = server;
17 changes: 6 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/*
play this: https://www.youtube.com/watch?v=d-diB65scQU
require('dotenv').config()

Sing along:
const server = require('./api/server')

here's a little code I wrote, please read the README word for word, don't worry, you got this
in every task there may be trouble, but if you worry you make it double, don't worry, you got this
ain't got no sense of what is REST? just concentrate on learning Express, don't worry, you got this
your file is getting way too big, bring a Router and make it thin, don't worry, be crafty
there is no data on that route, just write some code, you'll sort it out… don't worry, just hack it…
I need this code, but don't know where, perhaps should make some middleware, don't worry, just hack it
const PORT = process.env.PORT || 5000

Pull your server into this file and start it!
*/
server.listen(PORT, () => {
console.log(`listening on ${PORT}`)
})
Loading