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

feat: Add postwithuser route #11

Merged
merged 1 commit into from
Nov 28, 2020
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| /users/:id | GET | Return user with the specified id |
| /posts | GET | Returns a collection of posts |
| /posts/:id | GET | Returns the post with the specified ID |
| /postwithuser/:id | GET | Returns the post with the specified ID and the user data |
| /products | GET | Returns a collection of products |
| /products/:sku | GET | Returns the products with the specified sku|
| /restaurants | GET | Returns a collection of restaurant data |
Expand Down
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const cors = require('cors')
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const postsRouter = require('./routes/posts');
const postWithUserRouter = require('./routes/postwithuser');
const productRouter = require('./routes/products');
const restaurantsRouter = require('./routes/restaurants');

Expand All @@ -22,6 +23,7 @@ app.use(cors())
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/posts', postsRouter);
app.use('/postwithuser', postWithUserRouter);
app.use('/products', productRouter)
app.use('/restaurants', restaurantsRouter)

Expand Down
38 changes: 38 additions & 0 deletions routes/postwithuser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const router = express.Router();
const fs = require('fs')
const posts = JSON.parse(fs.readFileSync('database/posts.json', 'utf-8'))
const users = JSON.parse(fs.readFileSync('database/users.json', 'utf-8'))

router.get('/', function (req, res, _next) {
res.status(404)
res.json('Add post id to your request - /postwithuser/:id')
return
});

/**
* GET post with user data using post_id
*/
router.get('/:post', (req, res, _next) => {
const postId = req.params.post
if (postId == 0 || postId > posts.length) {
res.status(404)
res.json('post Not Found')
return
}
const postWithUserData = posts[postId - 1]

const userId = posts[postId - 1].user_id
if (userId == 0 || userId > users.length) {
res.status(404)
res.json('User Not Found')
return
}

postWithUserData.user_data = users[userId - 1]
res.json(postWithUserData)

});


module.exports = router;