diff --git a/src/routes/documents.js b/src/routes/documents.js index b5de9a82..dda6a0c9 100644 --- a/src/routes/documents.js +++ b/src/routes/documents.js @@ -1,73 +1,74 @@ -import compose from 'koa-compose'; -import route from 'koa-route'; -import {index, json, accepts} from './'; -import {c} from '../core/db'; +import compose from 'koa-compose' +import route from 'koa-route' +import {index, json, accepts} from './' +import {c} from '../core/db' async function exists(ctx, id) { - let i = await c('documents').findOne({id: id}); - return i ? true : false; + let i = await c('documents').findOne({id: id}) + return Boolean(i) } export default function () { - return compose([ - documents(), - document(), - type() - ]); -}; + return compose([ + documents(), + document(), + type() + ]) +} function documents() { - return route.get('/documents', json( - async(ctx) => { - let query = {}; - ctx.body = await c('documents') - .find(query) - .toArray(); - } - )); + return route.get('/documents', json( + async(ctx) => { + let query = {} + ctx.body = await c('documents') + .find(query) + .toArray() + } + )) } function document() { - return route.get('/documents/:id', accepts({ - 'text/html': index(exists), - 'text/plain': index(exists), - 'application/json': async(ctx, id) => { - let i = await c('documents').findOne({id: id}); - if (!i) ctx.status = 404; - else { - i = await populateDocPreview(i); - ctx.body = i; - } - } - })); + return route.get('/documents/:id', accepts({ + 'text/html': index(exists), + 'text/plain': index(exists), + 'application/json': async(ctx, id) => { + let i = await c('documents').findOne({id: id}) + if (!i) ctx.status = 404 + else { + i = await populateDocPreview(i) + ctx.body = i + } + } + })) } function type() { - return route.get('/documents/type/:type', async(ctx, type) => { - let types = { - 'document': ['documents', {type: 'document'}, populateDocPreview], - 'award': ['documents', {type: 'award'}, populateDocPreview] - }; + return route.get('/documents/type/:type', async(ctx, type) => { + let types = { + 'document': ['documents', {type: 'document'}, populateDocPreview], + 'award': ['documents', {type: 'award'}, populateDocPreview] + } - if (type in types) { - let [collection, query, mapFn] = types[type]; + if (type in types) { + let [collection, query, mapFn] = types[type] - let docs = await c(collection) - .find(query) - .toArray(); + let docs = await c(collection) + .find(query) + .toArray() - if (mapFn) docs = await Promise.all( - docs.map(mapFn) - ); + if (mapFn) { + docs = await Promise.all( + docs.map(mapFn) + ) } - ctx.body = docs; - } else { - ctx.status = 404; - } - }); + ctx.body = docs + } else { + ctx.status = 404 + } + }) } async function populateDocPreview(doc) { - doc.preview = await c('images').findOne({_id: doc.preview}); - return doc; + doc.preview = await c('images').findOne({_id: doc.preview}) + return doc }