This commit is contained in:
tmshv committed 2016-11-11 02:36:03 +03:00
1 parent be398bf5ce
commit f121eff445
38 files changed
+498 -267

No files matched your search

+17 -53
View File
@@ -1,4 +1,3 @@
import co from 'co';
import compose from 'koa-compose';
import route from 'koa-route';
import {c} from '../core/db';
@@ -9,53 +8,29 @@ export default function () {
gallery(),
album()
]);
//app.use(route.get('/gallery/:year', json(
// function *(year) {
// year = parseInt(year);
// let start = new Date(year, 0, 1);
// let end = new Date(year, 11, 31);
// let query = {
// date: {$gte: start, $lt: end}
// };
// let albums = yield c('albums')
// .find(query)
// .toArray();
//
// albums = yield albums.map(processAlbum);
// this.body = albums;
// }
//)));
};
function gallery(){
function gallery() {
return route.get('/gallery', json(
async(ctx) => {
let query = {};
let fields = ['_id', 'id', 'title', 'date'];
//.reduce((f, i) => {
// f[i] = 1;
// return f;
//}, {});
let albums = await c('albums')
.find(query)
.toArray();
//albums = albums.map(i => {
// return fields.reduce((a, key)=> {
// a[key] = i[key];
// return a;
// }, {});
//});
albums = await Promise.all(albums.map(processAlbum));
ctx.body = albums;
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(){
function album() {
return route.get('/album/:id', json(
async(ctx, id) => {
let record = await c('albums').findOne({
@@ -67,25 +42,14 @@ function album(){
));
}
let processAlbum = album => co(function *() {
//album.url = `/gallery/{year}{course}{album}`
// .replace('{year}', album.date.getFullYear())
// .replace('{course}', album.course_uri)
// .replace('{album}', album.uri);
const processAlbum = async album => {
album.url = `/album/${album.id}`;
//album.content = yield album.content.map(product_id => co(function *() {
// let product = yield c('products').findOne({_id: product_id});
// if (product) return product;
// else return null;
//}));
//let teacher = yield c('collective').findOne({id: album.teacher});
//album.teacher = teacher.name;
let previewImageId = album.images[0];
let image = yield c('images').findOne({_id: previewImageId});
album.preview = image.data.medium;
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;
});
};
+16 -18
View File
@@ -23,35 +23,33 @@ function news() {
'text/html': redirect,
'text/plain': redirect,
'application/json': async ctx => {
let limit = parseInt(ctx.query['limit']) || 10;
let skip = parseInt(ctx.query['skip']) || 0;
const limit = parseInt(ctx.query['limit']) || 20;
const skip = parseInt(ctx.query['skip']) || 0;
let query = {};
const query = {};
if (ctx.query.type) query.type = ctx.query.type;
let pinned = [];
if(skip == 0) {
const now = new Date();
pinned = await c('timeline')
.find({until: {$gte: now}})
.toArray()
.then(i => i.sort(sortNewsByDate));
}
const now = new Date();
const pinned = skip != 0 ? [] : await c('timeline')
.find({until: {$gte: now}})
.toArray();
let sid = id => id + '';
let pinnedIds = pinned.map(p => sid(p['_id']));
const id = i => i._id;
const pinnedIds = pinned.map(id);
let posts = await c('timeline')
.find(query)
.sort({date: -1})
.skip(skip)
.limit(limit)
.toArray()
.then(items => items.filter(
i => pinnedIds.indexOf(sid(i['_id'])) < 0
));
.toArray();
ctx.body = pinned.concat(posts);
ctx.body = [
...pinned.sort(sortNewsByDate),
...posts.filter(
i => !pinnedIds.includes(id(i))
)
];
}
}));
}
+4 -9
View File
@@ -1,9 +1,8 @@
import compose from 'koa-compose';
import co from 'co';
import route from 'koa-route';
import {json} from './';
import {getSchedule, getScheduleList, populateSchedule} from '../core/schedule';
import {getSchedule, getSchedules} from '../core/schedule';
export default function () {
return compose([
@@ -16,11 +15,12 @@ function list() {
return route.get('/schedule/list', json(
async(ctx) => {
ctx.type = 'application/json';
let data = await getScheduleList();
const data = await getSchedules();
if (!data) ctx.status = 404;
else ctx.body = data;
}
))
));
}
function schedule() {
@@ -34,11 +34,6 @@ function schedule() {
return;
}
let doPopulate = ctx.query.populate || false;
if (doPopulate) {
data.schedule = await populateSchedule(data.schedule);
}
ctx.body = data;
}
))