diff --git a/modules/hudozka-api/src/controllers/article.ts b/modules/hudozka-api/src/controllers/article.ts index 3529591b..4f9a2f31 100644 --- a/modules/hudozka-api/src/controllers/article.ts +++ b/modules/hudozka-api/src/controllers/article.ts @@ -1,6 +1,7 @@ import Article from '../core/Article' -import { encodeArticle, findArticles } from '../server/articles' +import { findArticles } from '../server/articles' import { Request, Response } from 'express' +import { encodeArticle } from '../factory/article' const int = (value: string | string[]) => parseInt(Array.isArray(value) ? value[0] @@ -29,7 +30,10 @@ export async function getAll(req: Request, res: Response) { const data = await findArticles(page, pageSize) if (data) { - res.json(data) + res.json({ + ...data, + items: data.items.map(encodeArticle) + }) } else { res.status(404) res.json({ diff --git a/modules/hudozka-api/src/core/Page.ts b/modules/hudozka-api/src/core/Page.ts index da26be7b..f35e1fa7 100644 --- a/modules/hudozka-api/src/core/Page.ts +++ b/modules/hudozka-api/src/core/Page.ts @@ -43,6 +43,7 @@ export default class Page { public documents: any public preview: any public tokens: any[] + public description: string constructor(data) { this.id = data.id @@ -56,6 +57,7 @@ export default class Page { this.documents = data.documents this.preview = data.preview this.tokens = data.tokens + this.description = data.description } plain() { diff --git a/modules/hudozka-api/src/factory/article.ts b/modules/hudozka-api/src/factory/article.ts new file mode 100644 index 00000000..0afe3140 --- /dev/null +++ b/modules/hudozka-api/src/factory/article.ts @@ -0,0 +1,9 @@ +import Article from '../core/Article' +import { encodeImage } from './image' + +export function encodeArticle(article: Article) { + return { + ...article, + preview: encodeImage(article.preview), + } +} \ No newline at end of file diff --git a/modules/hudozka-api/src/factory/page.ts b/modules/hudozka-api/src/factory/page.ts index 7d0fee31..5a3f2805 100644 --- a/modules/hudozka-api/src/factory/page.ts +++ b/modules/hudozka-api/src/factory/page.ts @@ -8,6 +8,7 @@ export function encodePage(page: Page, breadcrumb: Breadcrumb) { title: page.title, url: page.url, data: page.data, + description: page.description, preview: encodeImage(page.preview), tokens: page.tokens, breadcrumb, diff --git a/modules/hudozka-api/src/server/articles.ts b/modules/hudozka-api/src/server/articles.ts index 2dbca9f5..df418f0a 100644 --- a/modules/hudozka-api/src/server/articles.ts +++ b/modules/hudozka-api/src/server/articles.ts @@ -2,7 +2,6 @@ import Article from '../core/Article' import ImageArtifactType from '../core/ImageArtifactType' import { sortBy } from '../utils/sort' import { timestamp } from '../lib/date' -import { encodeImage } from '../factory/image' export const sortArticleByDate = sortBy( x => timestamp(new Date(x.date)) @@ -24,16 +23,17 @@ export async function findArticles(page: number, pageSize: number) { const limit = pageSize const skip = (page - 1) * pageSize - const pinnedArticles = page === 1 - ? await Article.findPinned() - : [] + // const pinnedArticles = page === 1 + // ? await Article.findPinned() + // : [] const total = await Article.total() const totalPages = Math.ceil(total / pageSize) const id = i => i._id - const pinnedIds = pinnedArticles.map(id) - const articles = await findArticlesNin(pinnedIds, skip, limit, { date: -1 }) + // const pinnedIds = pinnedArticles.map(id) + // const articles = await findArticlesNin(pinnedIds, skip, limit, { date: -1 }) + const articles = await Article.find({}, { skip, limit, sort: { date: -1 } }) const prevPage = page > 1 ? page - 1 @@ -44,16 +44,8 @@ export async function findArticles(page: number, pageSize: number) { : null const items = [ - ...pinnedArticles.sort(sortArticleByDate), ...articles ] - .map(x => x.plain()) - .map(x => ({ - ...x, - preview: !x.preview ? null : ( - x.preview.getPicture(ImageArtifactType.MEDIUM) - ) - })) return { items, @@ -62,10 +54,3 @@ export async function findArticles(page: number, pageSize: number) { totalPages, } } - -export function encodeArticle(article) { - return { - ...article, - preview: encodeImage(article.preview), - } -} \ No newline at end of file