Skip to content
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

completed mvp #288

Open
wants to merge 1 commit 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
52 changes: 51 additions & 1 deletion api/middleware/middleware.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
const Users = require('../users/users-model')

function logger(req, res, next) {
// DO YOUR MAGIC
console.log(req.url, req.method)

next()
}

function validateUserId(req, res, next) {
const validateUserId = async (req, res, next) => {
// DO YOUR MAGIC

try {
const {id} = req.params

const user = await Users.getById(id)

if(!user) {
res.status(404).json({ message: "user not found" })
} else {
res.status(200).json(user)
req.user = user
next()
}

} catch(err) {
res.status(500).json({message: err.message})
}
}

function validateUser(req, res, next) {
// DO YOUR MAGIC
try {
const user = req.body

if(!user.name) {
res.status(400).json({message: 'Missing required name field'})
} else {
res.status(200).json(user)
next()
}
} catch(err) {
res.status(500).json({message: err.message})
}
}

function validatePost(req, res, next) {
// DO YOUR MAGIC
try{
const { text } = req.body
if(!text){
res.status(400).json({message: 'Missing required text field'})
}else{
next()
}
}catch(err){
res.status(500).json({message: err.message})
}
}

// do not forget to expose these functions to other modules
module.exports = {
logger,
validateUserId,
validateUser,
validatePost
}
16 changes: 13 additions & 3 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
const express = require('express');

const server = express();

// remember express by default cannot parse JSON in request bodies

// global middlewares and the user's router need to be connected here

const express = require('express');

const userRouter = require('./users/users-router.js');

const {logger} = require('./middleware/middleware');


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

server.use('/api/users', logger, userRouter);

server.get('/', (req, res) => {
res.send(`<h2>Let's write some middleware!</h2>`);
});

module.exports = server;

63 changes: 57 additions & 6 deletions api/users/users-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,76 @@ const express = require('express');
// You will need `users-model.js` and `posts-model.js` both
// The middleware functions also need to be required

const Users = require('./users-model')
const Posts = require('../posts/posts-model')

const { validateUserId, validateUser, validatePost} = require('../middleware/middleware');

const router = express.Router();

router.get('/', (req, res) => {
router.get('/', (req, res, next) => {
// RETURN AN ARRAY WITH ALL THE USERS
Users.get(req.query)
.then(users => {
res.status(200).json(users)
})
.cactch(err => {
next(err)
})
});

router.get('/:id', (req, res) => {
router.get('/:id', validateUserId, (req, res) => {
// RETURN THE USER OBJECT
// this needs a middleware to verify user id
res.status(200).json(req.user)
});

router.post('/', (req, res) => {
router.post('/', validateUser, async (req, res) => {
// RETURN THE NEWLY CREATED USER OBJECT
// this needs a middleware to check that the request body is valid

try {
const newUser = await Users.insert(req.body)
res.status(201).json(newUser)
} catch(err) {
next(err)
}
});

router.put('/:id', (req, res) => {
router.put('/:id', validateUser, validateUserId, (req, res) => {
// RETURN THE FRESHLY UPDATED USER OBJECT
// this needs a middleware to verify user id
// and another middleware to check that the request body is valid
const changes = req.body
const { id } = req.params
const updatedUser = await Users.update(id, changes)

res.status(201).json(updatedUser)
});

router.delete('/:id', (req, res) => {
router.delete('/:id', validateUser, async (req, res) => {
// RETURN THE FRESHLY DELETED USER OBJECT
// this needs a middleware to verify user id
try{
const user = await Users.getById(req.params.id)
res.status(200).json(user)
}catch(err){
res.status(500).json({message: 'something went wrong'})
}
});

router.get('/:id/posts', (req, res) => {
router.get('/:id/posts', validateUserId, (req, res) => {
// RETURN THE ARRAY OF USER POSTS
// this needs a middleware to verify user id
const {id} = req.params
Users.getUserPosts(id)
.then(messages => {
res.status(200).json(messages)
})
.catch(err => {
console.log(err)
res.status(500).json({message: 'Error getting the posts for the user'})
})
});

router.post('/:id/posts', (req, res) => {
Expand All @@ -41,4 +81,15 @@ router.post('/:id/posts', (req, res) => {
// and another middleware to check that the request body is valid
});

// ERROR ENDPOINT
// ERROR ENDPOINT
// ERROR ENDPOINT
router.use((err, req, res, next) => {
res.status(500).json({
message: 'Error somewhere in there my dude',
error: err.message
});
})

// do not forget to export the router
module.exports = router;
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// require your server and launch it
const server = require('./api/server.js');

const port = 9000;

server.listen(port, () => {
console.log(`\n*** Server Running on http://localhost:${port} ***\n`);
});
Loading