/news, /events deleted, Articles update

This commit is contained in:
tmshv committed 2017-05-22 13:30:05 +03:00
1 parent 7bc34f9710
commit e21425fd52
8 files changed
+66 -221

No files matched your search

+53 -35
View File
@@ -47,6 +47,46 @@ async function findPinned(page) {
.toArray()
}
async function getArticleListComponent(path, page, pageSize) {
path = getPathWithNoTrailingSlash(path)
const limit = pageSize
const skip = (page - 1) * pageSize
const pinnedArticles = await findPinned(page)
const total = await totalArticles()
const totalPages = total / pageSize
const id = i => i._id
const pinnedIds = pinnedArticles.map(id)
const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1})
const prevPage = page > 1
? page - 1
: null
const nextPage = page < totalPages
? page + 1
: null
const content = [
...pinnedArticles.sort(sortArticleByDate),
...articles
]
.map(article => ({
...article,
url: articleUrl(article),
}))
return (
<ArticleList
articles={content}
prevPage={prevPage}
nextPage={nextPage}
/>
)
}
function getMeta(article) {
return {
title: article.title,
@@ -55,45 +95,22 @@ function getMeta(article) {
function getArticles(pageSize) {
return get('/articles/:page?', async (ctx, page) => {
const path = getPathWithNoTrailingSlash(ctx.path)
page = parseInt(page) || 1
const limit = pageSize
const skip = (page - 1) * pageSize
if (page === 1) {
ctx.redirect('/')
} else {
const path = getPathWithNoTrailingSlash(ctx.path)
const pinnedArticles = await findPinned(page)
const total = await totalArticles()
const totalPages = total / pageSize
const articles = await getArticleListComponent(path, page, pageSize)
const Component = (
<div className="content content_thin">
{articles}
</div>
)
const id = i => i._id
const pinnedIds = pinnedArticles.map(id)
const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1})
const prevPage = page > 1
? page - 1
: null
const nextPage = page < totalPages
? page + 1
: null
const content = [
...pinnedArticles.sort(sortArticleByDate),
...articles
]
.map(article => ({
...article,
url: articleUrl(article),
}))
const Component = (
<ArticleList
articles={content}
prevPage={prevPage}
nextPage={nextPage}
/>
)
ctx.body = await render(path, Component, getMeta({title: `Статьи`}))
ctx.body = await render(path, Component, getMeta({title: `Статьи`}))
}
})
}
@@ -132,5 +149,6 @@ function getArticle() {
}))
}
exports.getArticleListComponent = getArticleListComponent
exports.getArticles = getArticles
exports.getArticle = getArticle
-36
View File
@@ -1,36 +0,0 @@
import route from 'koa-route';
import {c} from '../core/db';
import {json} from './';
import {sortBy} from '../utils/sort';
let sortByDate = sortBy(
i => (new Date(i.date))
.getTime()
);
export default function () {
return route.get('/events/:page?', json(
async(ctx) => {
let limit = parseInt(ctx.query['limit']) || 10;
let skip = parseInt(ctx.query['skip']) || 0;
let query = {};
let documents = await c('events')
.find(query)
.sort({date: -1})
.skip(skip)
.limit(limit)
.toArray();
let total = await c('events')
.find(query).count();
ctx.body = {
skip: skip,
limit: limit,
total: total,
data: documents
};
}
));
};
+11 -35
View File
@@ -2,6 +2,7 @@ const React = require('react')
const Article = require('../components/Article')
const {get} = require('koa-route')
const {c} = require('../core/db')
const getArticleListComponent = require('./articles').getArticleListComponent
const {render} = require('../lib/render')
const getPathWithNoTrailingSlash = require('../lib/url').getPathWithNoTrailingSlash
const timestamp = require('../lib/date').timestamp
@@ -34,17 +35,6 @@ async function findPinned(page) {
.toArray()
}
function getComponent(path, article) {
return (
<Article
url={path}
title={article.title}
date={article.date}
data={article.post}
/>
)
}
function getMeta(article) {
return {
title: article.title,
@@ -52,33 +42,19 @@ function getMeta(article) {
}
function getHome(pageSize) {
return get('/', async (ctx, page) => {
const path = getPathWithNoTrailingSlash(ctx.path) || '/'
page = parseInt(page) || 1
return get('/', async ctx => {
const path = getPathWithNoTrailingSlash(ctx.path)
const articles = await getArticleListComponent(path, 1, pageSize)
const limit = pageSize
const skip = (page - 1) * pageSize
const pinnedArticles = await findPinned(page)
const id = i => i._id
const pinnedIds = pinnedArticles.map(id)
const articles = await findArticlesNin(pinnedIds, skip, limit, {date: -1})
const content = [
...pinnedArticles.sort(sortArticleByDate),
...articles
]
.map(article => getComponent(articleUrl(article), article))
const Component = (
<div className="content content_thin">
<div className="hudozka-title">
<p>МБУДО</p>
<h1>Шлиссельбургская детская художественная&nbsp;школа</h1>
</div>
{content}
<div className="content content_thin">
<div className="hudozka-title">
<p>МБУДО</p>
<h1>Шлиссельбургская детская художественная&nbsp;школа</h1>
</div>
{articles}
</div>
)
ctx.body = await render(path, Component, getMeta({title: 'Шлиссельбургская ДХШ'}), {showAuthor: true})
-4
View File
@@ -7,22 +7,18 @@ import {index as indexFile} from '../config'
import sitemap from './sitemap'
import schedule from './schedule'
import news from './news'
import gallery from './gallery'
import documents from './documents'
import teachers from './teachers'
import events from './events'
import pages from './pages'
export function routes(store) {
return compose([
sitemap(),
schedule(),
news(),
gallery(),
documents(store),
teachers(),
events(),
pages(),
])
}
-67
View File
@@ -1,67 +0,0 @@
import compose from 'koa-compose';
import route from 'koa-route';
import {accepts} from './';
import {c} from '../core/db';
import {sortBy} from '../utils/sort';
const timestamp = i => i.getTime();
const sortNewsByDate = sortBy(
i => timestamp(new Date(i.date))
);
const redirect = async ctx => ctx.redirect('/');
export default function () {
return compose([
news(),
pinned()
]);
};
function news() {
return route.get('/news', accepts({
'text/html': redirect,
'text/plain': redirect,
'application/json': async ctx => {
const limit = parseInt(ctx.query['limit']) || 20;
const skip = parseInt(ctx.query['skip']) || 0;
const now = new Date();
const pinned = skip !== 0 ? [] : await c('timeline')
.find({until: {$gte: now}})
.sort({date: -1})
.toArray();
const id = i => i._id;
const pinnedIds = pinned.map(id);
let posts = await c('timeline')
.find({
_id: {$nin: pinnedIds}
})
.sort({date: -1})
.skip(skip)
.limit(limit)
.toArray();
ctx.body = [
...pinned.sort(sortNewsByDate),
...posts
];
}
}));
}
function pinned(){
return route.get('/news/pinned', accepts({
'text/html': redirect,
'text/plain': redirect,
'application/json': async ctx => {
let now = new Date();
ctx.body = await c('timeline')
.find({until: {$gte: now}})
.toArray()
.then(i => i.sort(sortNewsByDate));
}
}));
}