Files
hudozka/src/routes/gallery.js
T
2016-11-11 02:36:03 +03:00

56 lines
1.3 KiB
JavaScript

import compose from 'koa-compose';
import route from 'koa-route';
import {c} from '../core/db';
import {json} from './';
export default function () {
return compose([
gallery(),
album()
]);
};
function gallery() {
return route.get('/gallery', json(
async(ctx) => {
let query = {};
let albums = await c('albums')
.find(query)
.toArray();
ctx.body = await Promise.all(albums
.sort((a, b) => {
const da = a.date.getTime();
const db = b.date.getTime();
return db - da;
})
.map(processAlbum)
);
}
));
}
function album() {
return route.get('/album/:id', json(
async(ctx, id) => {
let record = await c('albums').findOne({
id: id
});
record = await processAlbum(record);
ctx.body = record;
}
));
}
const processAlbum = async album => {
album.url = `/album/${album.id}`;
const previewImageId = album.images.length ? album.images[0] : null;
if (previewImageId) {
const image = await c('images').findOne({_id: previewImageId});
album.preview = image.data.medium;
}
return album;
};