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: api to fetch a single profile - GET /api/profile/:id #25

Merged
merged 2 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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)
ans-4175 marked this conversation as resolved.
Show resolved Hide resolved
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);
anwari666 marked this conversation as resolved.
Show resolved Hide resolved
})
.get('/*', (req, res) => {
const context = {};
const markup = renderToString(
Expand Down