Skip to content

Commit

Permalink
Fixed issue Seneca-CDOT#3615: added more values to /post
Browse files Browse the repository at this point in the history
  • Loading branch information
liutng committed Nov 16, 2022
1 parent 01a8b2f commit aa3046e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/api/posts/src/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ posts.get('/', validatePostsQuery(), async (req, res, next) => {
const defaultNumberOfPosts = process.env.MAX_POSTS_PER_PAGE || 30;
const capNumOfPosts = 100;
const page = parseInt(req.query.page || 1, 10);

const expand = req.query.expand ? parseInt(req.query.expand) : 0;
let ids;
let perPage;
let postsCount;
Expand Down Expand Up @@ -60,14 +60,25 @@ posts.get('/', validatePostsQuery(), async (req, res, next) => {
first: `/posts?per_page=${perPage}&page=${1}`,
last: `/posts?per_page=${perPage}&page=${Math.floor(postsCount / perPage)}`,
});
res.json(
ids
let data;
if (expand === 1) {
data = await Promise.all(
ids.map(async (id, title, published) => {
let post = await Post.byId(id); //obtain the corresponding feed to populate the author's name to the return data.
return {
id,
url: `${postsUrl}/${id}`,
author: post.feed.author,
title: post.title,
publishDate: post.published,
};
})
);
} else
data = ids
// Return id and url for a specific post
.map((id) => ({
id,
url: `${postsUrl}/${id}`,
}))
);
.map((id) => ({ id, url: `${postsUrl}/${id}` }));
res.json(data);
});

posts.get('/:id', validatePostsIdParam(), async (req, res, next) => {
Expand Down

0 comments on commit aa3046e

Please sign in to comment.