Skip to content

Commit

Permalink
feat: api to fetch a single profile - GET /api/profile/:id (#25)
Browse files Browse the repository at this point in the history
* feat: api to fetch a single profile - GET /api/profile/:id

* chore: separate the use of unary operator by parenthesis

Co-authored-by: Anwari Ilman <anwari.ilman@ctw.bmwgroup.com>
  • Loading branch information
anwari666 and Anwari Ilman committed Oct 31, 2021
1 parent 390265d commit cd6c65a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/data/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ const getRandomNew = async (n = 8) => {
return randUserData;
}

const getProfileById = async(id) => {
const profile = rawNew.find((item) => item.id === (+id) )
return !!profile
? profile
: { error: `cannot find profile ${id}` }
}

module.exports = {
getRandom,
getRandomNew,
getProfileById
}
5 changes: 5 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { renderToString } from 'react-dom/server';
const bodyParser = require("body-parser");
const {
getRandomNew,
getProfileById
} = require('./data');

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);
Expand Down Expand Up @@ -37,6 +38,10 @@ server
const randUsers = await getRandomNew();
res.json(randUsers);
})
.get('/api/profile/:id', async (req, res) => {
const user = await getProfileById(req.params.id);
res.json(user);
})
.get('/*', (req, res) => {
const context = {};
const markup = renderToString(
Expand Down

0 comments on commit cd6c65a

Please sign in to comment.