documents update

This commit is contained in:
tmshv committed 2016-02-27 18:13:24 +03:00
1 parent 7b6559e57d
commit 10008a323a
7 files changed
+65 -25

No files matched your search

+28 -12
View File
@@ -1,7 +1,7 @@
import co from 'co';
import route from 'koa-route';
import {index, json} from './';
import db from '../core/db';
import {c} from '../core/db';
export default function (app) {
[
@@ -9,25 +9,41 @@ export default function (app) {
'/documents',
json(function *() {
let query = {};
this.body = yield db.c('documents')
this.body = yield c('documents')
.find(query)
.toArray();
})
],
[
'/documents/:type',
json(function *(type) {
this.body = yield db.c(type)
.find()
.toArray();
})
],
[
'/document/:doc',
index('../templates/document.html')
'/documents/type/:type',
function *(type) {
let types = {
'document': ['documents', {type: 'document'}, populateDocPreview],
'award': ['awards', {}]
};
if (type in types) {
let [collection, query, mapFn] = types[type];
let docs = yield c(collection)
.find(query)
.toArray();
if (mapFn) docs = yield docs.map(mapFn);
this.body = docs;
} else {
this.status = 404;
}
}
]
].forEach(item => {
let [path, fn] = item;
app.use(route.get(path, fn));
});
};
function* populateDocPreview(doc) {
doc.preview = yield c('images').findOne({_id: doc.preview});
return doc;
}