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 Article from '../core/Article'
|
||||||
import { encodeArticle, findArticles } from '../server/articles'
|
import { findArticles } from '../server/articles'
|
||||||
import { Request, Response } from 'express'
|
import { Request, Response } from 'express'
|
||||||
|
import { encodeArticle } from '../factory/article'
|
||||||
|
|
||||||
const int = (value: string | string[]) => parseInt(Array.isArray(value)
|
const int = (value: string | string[]) => parseInt(Array.isArray(value)
|
||||||
? value[0]
|
? value[0]
|
||||||
@@ -29,7 +30,10 @@ export async function getAll(req: Request, res: Response) {
|
|||||||
const data = await findArticles(page, pageSize)
|
const data = await findArticles(page, pageSize)
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
res.json(data)
|
res.json({
|
||||||
|
...data,
|
||||||
|
items: data.items.map(encodeArticle)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
res.status(404)
|
res.status(404)
|
||||||
res.json({
|
res.json({
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ export default class Page {
|
|||||||
public documents: any
|
public documents: any
|
||||||
public preview: any
|
public preview: any
|
||||||
public tokens: any[]
|
public tokens: any[]
|
||||||
|
public description: string
|
||||||
|
|
||||||
constructor(data) {
|
constructor(data) {
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
@@ -56,6 +57,7 @@ export default class Page {
|
|||||||
this.documents = data.documents
|
this.documents = data.documents
|
||||||
this.preview = data.preview
|
this.preview = data.preview
|
||||||
this.tokens = data.tokens
|
this.tokens = data.tokens
|
||||||
|
this.description = data.description
|
||||||
}
|
}
|
||||||
|
|
||||||
plain() {
|
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,
|
title: page.title,
|
||||||
url: page.url,
|
url: page.url,
|
||||||
data: page.data,
|
data: page.data,
|
||||||
|
description: page.description,
|
||||||
preview: encodeImage(page.preview),
|
preview: encodeImage(page.preview),
|
||||||
tokens: page.tokens,
|
tokens: page.tokens,
|
||||||
breadcrumb,
|
breadcrumb,
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import Article from '../core/Article'
|
|||||||
import ImageArtifactType from '../core/ImageArtifactType'
|
import ImageArtifactType from '../core/ImageArtifactType'
|
||||||
import { sortBy } from '../utils/sort'
|
import { sortBy } from '../utils/sort'
|
||||||
import { timestamp } from '../lib/date'
|
import { timestamp } from '../lib/date'
|
||||||
import { encodeImage } from '../factory/image'
|
|
||||||
|
|
||||||
export const sortArticleByDate = sortBy(
|
export const sortArticleByDate = sortBy(
|
||||||
x => timestamp(new Date(x.date))
|
x => timestamp(new Date(x.date))
|
||||||
@@ -24,16 +23,17 @@ export async function findArticles(page: number, pageSize: number) {
|
|||||||
const limit = pageSize
|
const limit = pageSize
|
||||||
const skip = (page - 1) * pageSize
|
const skip = (page - 1) * pageSize
|
||||||
|
|
||||||
const pinnedArticles = page === 1
|
// const pinnedArticles = page === 1
|
||||||
? await Article.findPinned()
|
// ? await Article.findPinned()
|
||||||
: []
|
// : []
|
||||||
|
|
||||||
const total = await Article.total()
|
const total = await Article.total()
|
||||||
const totalPages = Math.ceil(total / pageSize)
|
const totalPages = Math.ceil(total / pageSize)
|
||||||
|
|
||||||
const id = i => i._id
|
const id = i => i._id
|
||||||
const pinnedIds = pinnedArticles.map(id)
|
// const pinnedIds = pinnedArticles.map(id)
|
||||||
const articles = await findArticlesNin(pinnedIds, skip, limit, { date: -1 })
|
// const articles = await findArticlesNin(pinnedIds, skip, limit, { date: -1 })
|
||||||
|
const articles = await Article.find({}, { skip, limit, sort: { date: -1 } })
|
||||||
|
|
||||||
const prevPage = page > 1
|
const prevPage = page > 1
|
||||||
? page - 1
|
? page - 1
|
||||||
@@ -44,16 +44,8 @@ export async function findArticles(page: number, pageSize: number) {
|
|||||||
: null
|
: null
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
...pinnedArticles.sort(sortArticleByDate),
|
|
||||||
...articles
|
...articles
|
||||||
]
|
]
|
||||||
.map(x => x.plain())
|
|
||||||
.map(x => ({
|
|
||||||
...x,
|
|
||||||
preview: !x.preview ? null : (
|
|
||||||
x.preview.getPicture(ImageArtifactType.MEDIUM)
|
|
||||||
)
|
|
||||||
}))
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
items,
|
items,
|
||||||
@@ -62,10 +54,3 @@ export async function findArticles(page: number, pageSize: number) {
|
|||||||
totalPages,
|
totalPages,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodeArticle(article) {
|
|
||||||
return {
|
|
||||||
...article,
|
|
||||||
preview: encodeImage(article.preview),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user