[site] documents router eslinted
This commit is contained in:
1 parent
6135e57b54
commit
10bf37b094
1 file changed
+27
-26
+27
-26
@@ -1,11 +1,11 @@
|
||||
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 () {
|
||||
@@ -13,18 +13,18 @@ export default function () {
|
||||
documents(),
|
||||
document(),
|
||||
type()
|
||||
]);
|
||||
};
|
||||
])
|
||||
}
|
||||
|
||||
function documents() {
|
||||
return route.get('/documents', json(
|
||||
async(ctx) => {
|
||||
let query = {};
|
||||
let query = {}
|
||||
ctx.body = await c('documents')
|
||||
.find(query)
|
||||
.toArray();
|
||||
.toArray()
|
||||
}
|
||||
));
|
||||
))
|
||||
}
|
||||
|
||||
function document() {
|
||||
@@ -32,14 +32,14 @@ function document() {
|
||||
'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;
|
||||
let i = await c('documents').findOne({id: id})
|
||||
if (!i) ctx.status = 404
|
||||
else {
|
||||
i = await populateDocPreview(i);
|
||||
ctx.body = i;
|
||||
i = await populateDocPreview(i)
|
||||
ctx.body = i
|
||||
}
|
||||
}
|
||||
}));
|
||||
}))
|
||||
}
|
||||
|
||||
function type() {
|
||||
@@ -47,27 +47,28 @@ function type() {
|
||||
let types = {
|
||||
'document': ['documents', {type: 'document'}, populateDocPreview],
|
||||
'award': ['documents', {type: 'award'}, populateDocPreview]
|
||||
};
|
||||
}
|
||||
|
||||
if (type in types) {
|
||||
let [collection, query, mapFn] = types[type];
|
||||
let [collection, query, mapFn] = types[type]
|
||||
|
||||
let docs = await c(collection)
|
||||
.find(query)
|
||||
.toArray();
|
||||
.toArray()
|
||||
|
||||
if (mapFn) docs = await Promise.all(
|
||||
if (mapFn) {
|
||||
docs = await Promise.all(
|
||||
docs.map(mapFn)
|
||||
);
|
||||
) }
|
||||
|
||||
ctx.body = docs;
|
||||
ctx.body = docs
|
||||
} else {
|
||||
ctx.status = 404;
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user