Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Commit

Permalink
Merge branch 'articles-metadata'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbovis committed Jun 12, 2020
2 parents f6088fd + 95cbeac commit a1b68d8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
75 changes: 52 additions & 23 deletions src/app/routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,69 @@ const Router = require('koa-router');

const Article = require('../../../model/article');
const getArticleSources = require('../../../articles/get-article-sources');
const middleware = require('../../middleware');
const transformArticle = require('./util/transform-article');

const parseBoolean = booleanString => {
if (
booleanString === undefined ||
booleanString === null ||
booleanString.trim().length === 0
) {
return undefined;
}

return booleanString === 'true';
};

const createRouter = () => {
const router = new Router({ prefix: '/articles' });

router.get('/', async ({ response, request }, next) => {
const sources = await getArticleSources();
const page = request.query.page || 1;
router.get(
'/',
middleware.pagination({
defaultLimit: 12,
maxLimit: 50,
maxPage: Infinity,
}),
async ({ pagination, response, request }, next) => {
const sources = await getArticleSources();
const editorsChoice = parseBoolean(request.query.editorsChoice);

const feed = _.findKey(
sources,
source => source.slug === request.query.source,
);

const feed = _.findKey(
sources,
source => source.slug === request.query.source,
);
const query = _.pickBy(
{
feed: request.query.source ? feed : undefined,
editorsChoice:
editorsChoice === true || editorsChoice === undefined
? editorsChoice
: { $in: [false, null] },
},
i => i !== undefined,
);

const articles = await Article.paginate(
request.query.source ? { feed } : {},
{
const articles = await Article.paginate(query, {
sort: { date: -1 },
lean: true,
limit: 12,
page,
},
);
limit: pagination.limit,
page: pagination.page,
});

response.body = {
articles: _(articles.docs).map(_.partial(transformArticle, sources)),
limit: articles.limit,
page: parseInt(articles.page, 10),
pageCount: articles.pages,
total: articles.total,
};
response.body = {
articles: _(articles.docs).map(_.partial(transformArticle, sources)),
limit: articles.limit,
page: parseInt(articles.page, 10),
pageCount: articles.pages,
total: articles.total,
};

await next();
});
await next();
},
);

router.get('/:feedSlug/:articleSlug', async ({ response, params }, next) => {
const { feedSlug, articleSlug } = params;
Expand Down
1 change: 1 addition & 0 deletions src/app/routes/v1/util/transform-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const transformArticle = (sources, article) => {

return {
..._.pick(article, ['date', 'id', 'slug', 'summary', 'title', 'url']),
imageUrl: _.get(article, 'metadata.og:image', null),
source,
};
};
Expand Down
1 change: 1 addition & 0 deletions src/model/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const schema = mongoose.Schema({
date: Date,
feed: String,
guid: String,
metadata: mongoose.Schema.Types.Mixed,
slug: String,
summary: String,
title: String,
Expand Down

0 comments on commit a1b68d8

Please sign in to comment.