Merge pull request #30 from tmshv/fix/api-page-and-article-updated
api updates
This commit is contained in:
5 files changed
+24
-23
No files matched your search
@@ -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({
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import Article from '../core/Article'
|
||||
import { encodeImage } from './image'
|
||||
|
||||
export function encodeArticle(article: Article) {
|
||||
return {
|
||||
...article,
|
||||
preview: encodeImage(article.preview),
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user