[site] documents router eslinted
This commit is contained in:
1 parent
6135e57b54
commit
10bf37b094
1 file changed
+53
-52
+53
-52
@@ -1,73 +1,74 @@
|
|||||||
import compose from 'koa-compose';
|
import compose from 'koa-compose'
|
||||||
import route from 'koa-route';
|
import route from 'koa-route'
|
||||||
import {index, json, accepts} from './';
|
import {index, json, accepts} from './'
|
||||||
import {c} from '../core/db';
|
import {c} from '../core/db'
|
||||||
|
|
||||||
async function exists(ctx, id) {
|
async function exists(ctx, id) {
|
||||||
let i = await c('documents').findOne({id: id});
|
let i = await c('documents').findOne({id: id})
|
||||||
return i ? true : false;
|
return Boolean(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
return compose([
|
return compose([
|
||||||
documents(),
|
documents(),
|
||||||
document(),
|
document(),
|
||||||
type()
|
type()
|
||||||
]);
|
])
|
||||||
};
|
}
|
||||||
|
|
||||||
function documents() {
|
function documents() {
|
||||||
return route.get('/documents', json(
|
return route.get('/documents', json(
|
||||||
async(ctx) => {
|
async(ctx) => {
|
||||||
let query = {};
|
let query = {}
|
||||||
ctx.body = await c('documents')
|
ctx.body = await c('documents')
|
||||||
.find(query)
|
.find(query)
|
||||||
.toArray();
|
.toArray()
|
||||||
}
|
}
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
function document() {
|
function document() {
|
||||||
return route.get('/documents/:id', accepts({
|
return route.get('/documents/:id', accepts({
|
||||||
'text/html': index(exists),
|
'text/html': index(exists),
|
||||||
'text/plain': index(exists),
|
'text/plain': index(exists),
|
||||||
'application/json': async(ctx, id) => {
|
'application/json': async(ctx, id) => {
|
||||||
let i = await c('documents').findOne({id: id});
|
let i = await c('documents').findOne({id: id})
|
||||||
if (!i) ctx.status = 404;
|
if (!i) ctx.status = 404
|
||||||
else {
|
else {
|
||||||
i = await populateDocPreview(i);
|
i = await populateDocPreview(i)
|
||||||
ctx.body = i;
|
ctx.body = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
function type() {
|
function type() {
|
||||||
return route.get('/documents/type/:type', async(ctx, type) => {
|
return route.get('/documents/type/:type', async(ctx, type) => {
|
||||||
let types = {
|
let types = {
|
||||||
'document': ['documents', {type: 'document'}, populateDocPreview],
|
'document': ['documents', {type: 'document'}, populateDocPreview],
|
||||||
'award': ['documents', {type: 'award'}, populateDocPreview]
|
'award': ['documents', {type: 'award'}, populateDocPreview]
|
||||||
};
|
}
|
||||||
|
|
||||||
if (type in types) {
|
if (type in types) {
|
||||||
let [collection, query, mapFn] = types[type];
|
let [collection, query, mapFn] = types[type]
|
||||||
|
|
||||||
let docs = await c(collection)
|
let docs = await c(collection)
|
||||||
.find(query)
|
.find(query)
|
||||||
.toArray();
|
.toArray()
|
||||||
|
|
||||||
if (mapFn) docs = await Promise.all(
|
if (mapFn) {
|
||||||
docs.map(mapFn)
|
docs = await Promise.all(
|
||||||
);
|
docs.map(mapFn)
|
||||||
|
) }
|
||||||
|
|
||||||
ctx.body = docs;
|
ctx.body = docs
|
||||||
} else {
|
} else {
|
||||||
ctx.status = 404;
|
ctx.status = 404
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function populateDocPreview(doc) {
|
async function populateDocPreview(doc) {
|
||||||
doc.preview = await c('images').findOne({_id: doc.preview});
|
doc.preview = await c('images').findOne({_id: doc.preview})
|
||||||
return doc;
|
return doc
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user