Files
hudozka/src/routes/gallery.js
T

44 lines
1.1 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(),
]);
};
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)
);
}
));
}
const processAlbum = async album => {
album.url = `/album/${album.id}`;
const previewFromImage = imgs => imgs.length ? imgs[0] : null;
const previewImageId = album.preview ? album.preview : previewFromImage(album.images);
if (previewImageId) {
const image = await c('images').findOne({_id: previewImageId});
album.preview = image.data.medium;
}
return album;
};