api Page updated: refactor to make it looks like article
This commit is contained in:
1 parent
68b7f4225d
commit
348c3baba8
11 files changed
+160
-63
No files matched your search
@@ -2,11 +2,7 @@ import Article from '../core/Article'
|
|||||||
import { findArticles } from '../server/articles'
|
import { findArticles } from '../server/articles'
|
||||||
import { Request, Response } from 'express'
|
import { Request, Response } from 'express'
|
||||||
import { encodeArticle } from '../factory/article'
|
import { encodeArticle } from '../factory/article'
|
||||||
|
import { readQueryInt } from '../lib/url'
|
||||||
const int = (value: string | string[]) => parseInt(Array.isArray(value)
|
|
||||||
? value[0]
|
|
||||||
: value
|
|
||||||
)
|
|
||||||
|
|
||||||
export async function getItem(req: Request, res: Response) {
|
export async function getItem(req: Request, res: Response) {
|
||||||
const id = req.params.slug
|
const id = req.params.slug
|
||||||
@@ -23,9 +19,8 @@ export async function getItem(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getAll(req: Request, res: Response) {
|
export async function getAll(req: Request, res: Response) {
|
||||||
const { query } = req
|
const page = readQueryInt(req, 'page')
|
||||||
const page = int(query.page)
|
const pageSize = readQueryInt(req, 'pageSize')
|
||||||
const pageSize = int(query.pageSize)
|
|
||||||
|
|
||||||
const data = await findArticles(page, pageSize)
|
const data = await findArticles(page, pageSize)
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,27 @@
|
|||||||
import { Request, Response } from 'express'
|
import { Request, Response } from 'express'
|
||||||
import Page from '../core/Page'
|
import Page from '../core/Page'
|
||||||
import { dropTrailingSlash, getPathWaterfall } from '../lib/url'
|
import { dropTrailingSlash, getPathWaterfall, readQueryArray, readQueryInt } from '../lib/url'
|
||||||
import { encodePage } from '../factory/page'
|
import { encodePage } from '../factory/page'
|
||||||
import * as breadcrumb from './breadcrumb'
|
import * as breadcrumb from './breadcrumb'
|
||||||
|
|
||||||
|
async function resolveItem(page: Page) {
|
||||||
|
const breadcrumbs = await breadcrumb.getItems(page.getUrl())
|
||||||
|
page.setBreadcrumb(breadcrumbs)
|
||||||
|
|
||||||
|
return page
|
||||||
|
}
|
||||||
|
|
||||||
export async function getItem(req: Request, res: Response) {
|
export async function getItem(req: Request, res: Response) {
|
||||||
const page = dropTrailingSlash(req.query.page)
|
const pageUrl = dropTrailingSlash(req.query.page)
|
||||||
const breadcrumbs = await breadcrumb.getItems(page)
|
const resource = await Page.findByUrl(pageUrl)
|
||||||
const resource = await Page.findByUrl(page)
|
|
||||||
|
|
||||||
if (resource) {
|
if (resource) {
|
||||||
res.json(encodePage(resource, breadcrumbs))
|
const page = await resolveItem(resource)
|
||||||
|
res.json(encodePage(page))
|
||||||
} else {
|
} else {
|
||||||
res.status(404)
|
res.status(404)
|
||||||
res.json({
|
res.json({
|
||||||
error: `Resource ${page} not found`
|
error: `Resource ${pageUrl} not found`
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,6 +41,38 @@ export async function getAll(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getByTags(req: Request, res: Response) {
|
||||||
|
const skip = readQueryInt(req, 'skip')
|
||||||
|
const limit = readQueryInt(req, 'limit')
|
||||||
|
|
||||||
|
const tags = readQueryArray(req, 'tag')
|
||||||
|
if (tags.length === 0) {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({
|
||||||
|
error: `No tags in request`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = { skip, limit }
|
||||||
|
const models = await Page.find({
|
||||||
|
tags: { $in: ['event', 'album'] },
|
||||||
|
}, options)
|
||||||
|
|
||||||
|
if (models) {
|
||||||
|
const pages = await Promise.all(models.map(resolveItem))
|
||||||
|
const items = pages.map(encodePage)
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
items,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
res.status(404)
|
||||||
|
res.json({
|
||||||
|
error: `Not found`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function getUrls(req: Request, res: Response) {
|
export async function getUrls(req: Request, res: Response) {
|
||||||
const data = await Page.find({})
|
const data = await Page.find({})
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,6 @@ import { Tag } from './Tag'
|
|||||||
|
|
||||||
const store = () => Data.getStore<Article>('Article')
|
const store = () => Data.getStore<Article>('Article')
|
||||||
|
|
||||||
function make(C) {
|
|
||||||
return x => new C(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Article {
|
export default class Article {
|
||||||
static async find(query, options = {}): Promise<Article[]> {
|
static async find(query, options = {}): Promise<Article[]> {
|
||||||
const items = await find(store(), query, options as any)
|
const items = await find(store(), query, options as any)
|
||||||
@@ -63,7 +59,7 @@ export default class Article {
|
|||||||
this.date = data.date
|
this.date = data.date
|
||||||
this.title = data.title
|
this.title = data.title
|
||||||
this.until = data.until
|
this.until = data.until
|
||||||
this.tags = (data.tags || []).map(make(Tag))
|
this.tags = (data.tags || []).map((x: string) => Tag.fromCyrillic(x))
|
||||||
this.content = data.content
|
this.content = data.content
|
||||||
this.hash = data.hash
|
this.hash = data.hash
|
||||||
this.type = data.type
|
this.type = data.type
|
||||||
@@ -76,27 +72,27 @@ export default class Article {
|
|||||||
this.url = `/article/${this.id}`
|
this.url = `/article/${this.id}`
|
||||||
}
|
}
|
||||||
|
|
||||||
plain() {
|
// plain() {
|
||||||
return {
|
// return {
|
||||||
id: this.id,
|
// id: this.id,
|
||||||
images: this.images,
|
// images: this.images,
|
||||||
post: this.post,
|
// post: this.post,
|
||||||
folder: this.folder,
|
// folder: this.folder,
|
||||||
date: this.date,
|
// date: this.date,
|
||||||
title: this.title,
|
// title: this.title,
|
||||||
until: this.until,
|
// until: this.until,
|
||||||
tags: this.tags,
|
// tags: this.tags,
|
||||||
content: this.content,
|
// content: this.content,
|
||||||
hash: this.hash,
|
// hash: this.hash,
|
||||||
type: this.type,
|
// type: this.type,
|
||||||
origin: this.origin,
|
// origin: this.origin,
|
||||||
file: this.file,
|
// file: this.file,
|
||||||
version: this.version,
|
// version: this.version,
|
||||||
preview: this.preview,
|
// preview: this.preview,
|
||||||
url: this.url,
|
// url: this.url,
|
||||||
featured: this.featured,
|
// featured: this.featured,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
const previewFromImage = (imgs = []) => imgs.length
|
const previewFromImage = (imgs = []) => imgs.length
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ import Data from './Data'
|
|||||||
import ImageArtifactType from './ImageArtifactType'
|
import ImageArtifactType from './ImageArtifactType'
|
||||||
import { findOne } from '../lib/store'
|
import { findOne } from '../lib/store'
|
||||||
import { mapOf } from '../server/lib'
|
import { mapOf } from '../server/lib'
|
||||||
|
import { ObjectId } from 'mongodb'
|
||||||
|
|
||||||
const store = () => Data.getStore('Image')
|
const store = () => Data.getStore('Image')
|
||||||
|
|
||||||
export default class Image {
|
export default class Image {
|
||||||
static async findById(id) {
|
static async findById(id: ObjectId) {
|
||||||
const data = await findOne(store(), { _id: id })
|
const data = await findOne(store(), { _id: id })
|
||||||
if (!data) return null
|
if (!data) return null
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,17 @@ import Data from './Data'
|
|||||||
import { find as storeFind, findOne, total } from '../lib/store'
|
import { find as storeFind, findOne, total } from '../lib/store'
|
||||||
import Image from './Image'
|
import Image from './Image'
|
||||||
import { Tag } from './Tag'
|
import { Tag } from './Tag'
|
||||||
|
import { ObjectId } from 'mongodb'
|
||||||
|
import { Breadcrumb } from '../types'
|
||||||
|
|
||||||
const store = () => Data.getStore('Page')
|
const store = () => Data.getStore('Page')
|
||||||
|
|
||||||
|
type PageDto = {
|
||||||
|
[name: string]: any
|
||||||
|
preview?: ObjectId
|
||||||
|
images: ObjectId[]
|
||||||
|
}
|
||||||
|
|
||||||
export default class Page {
|
export default class Page {
|
||||||
static async find(query, options = {}): Promise<Page[]> {
|
static async find(query, options = {}): Promise<Page[]> {
|
||||||
const items = await storeFind(store(), query, options as any)
|
const items = await storeFind(store(), query, options as any)
|
||||||
@@ -20,7 +28,7 @@ export default class Page {
|
|||||||
: processPage(item)
|
: processPage(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
static async findByUrl(url) {
|
static async findByUrl(url: string) {
|
||||||
const item = await findOne(store(), { url })
|
const item = await findOne(store(), { url })
|
||||||
|
|
||||||
return !item
|
return !item
|
||||||
@@ -41,13 +49,16 @@ export default class Page {
|
|||||||
public date: Date
|
public date: Date
|
||||||
public tags: Tag[]
|
public tags: Tag[]
|
||||||
public data: any
|
public data: any
|
||||||
public images: any
|
public images: ObjectId[]
|
||||||
public documents: any
|
public documents: any
|
||||||
public preview: any
|
public preview?: Image
|
||||||
public tokens: any[]
|
public tokens: any[]
|
||||||
public description: string
|
public description: string
|
||||||
|
public featured: boolean
|
||||||
|
|
||||||
constructor(data) {
|
private breadcrumb: Breadcrumb
|
||||||
|
|
||||||
|
constructor(data: { [name: string]: any }) {
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
this.hash = data.hash
|
this.hash = data.hash
|
||||||
this.file = data.file
|
this.file = data.file
|
||||||
@@ -60,27 +71,43 @@ 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.tags = (data.tags || []).map(x => new Tag(x))
|
this.tags = (data.tags || []).map((x: string) => new Tag(x, x))
|
||||||
|
this.featured = data.featured
|
||||||
this.description = data.description
|
this.description = data.description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public setBreadcrumb(value: Breadcrumb) {
|
||||||
|
this.breadcrumb = value
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
public getBreadcrumb() {
|
||||||
|
return this.breadcrumb
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUrl() {
|
||||||
|
return this.url
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const previewFromImage = (imgs = []) => imgs.length
|
function firstItem<T>(items: T[] = []): T {
|
||||||
? imgs[0]
|
return items.length
|
||||||
: null
|
? items[0]
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
|
||||||
async function processPage(article) {
|
async function processPage(item: PageDto): Promise<Page> {
|
||||||
let preview = null
|
let preview: Image = null
|
||||||
const previewId = article.preview
|
const previewId = item.preview
|
||||||
? article.preview
|
? item.preview
|
||||||
: previewFromImage(article.images)
|
: firstItem(item.images)
|
||||||
|
|
||||||
if (previewId) {
|
if (previewId) {
|
||||||
preview = await Image.findById(previewId)
|
preview = await Image.findById(previewId)
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Page({
|
return new Page({
|
||||||
...article,
|
...item,
|
||||||
preview,
|
preview,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,20 @@
|
|||||||
import { translit } from '../lib/translit'
|
import { translit } from '../lib/translit'
|
||||||
|
|
||||||
export class Tag {
|
export class Tag {
|
||||||
|
public static fromCyrillic(tag: string) {
|
||||||
|
const slug = tag
|
||||||
|
? translit(tag).toLowerCase()
|
||||||
|
: tag
|
||||||
|
|
||||||
|
return new Tag(tag, slug)
|
||||||
|
}
|
||||||
|
|
||||||
public name: string
|
public name: string
|
||||||
public slug: string
|
public slug: string
|
||||||
|
|
||||||
constructor(tag: string) {
|
constructor(name: string, slug: string) {
|
||||||
this.name = tag
|
this.name = name
|
||||||
|
this.slug = slug
|
||||||
if (tag) {
|
|
||||||
this.slug = translit(tag).toLowerCase()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public isValid() {
|
public isValid() {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import ImageArtifactType from '../core/ImageArtifactType'
|
import ImageArtifactType from '../core/ImageArtifactType'
|
||||||
|
import Image from '../core/Image'
|
||||||
|
|
||||||
export type ImageArtifactDto = {
|
export type ImageArtifactDto = {
|
||||||
height: number
|
height: number
|
||||||
@@ -23,7 +24,7 @@ export type ImageDto = {
|
|||||||
file: string
|
file: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodeImage(image): ImageDto {
|
export function encodeImage(image: Image): ImageDto {
|
||||||
const artifacts = [
|
const artifacts = [
|
||||||
ImageArtifactType.SMALL,
|
ImageArtifactType.SMALL,
|
||||||
ImageArtifactType.MEDIUM,
|
ImageArtifactType.MEDIUM,
|
||||||
|
|||||||
@@ -18,12 +18,14 @@ type PageDto = {
|
|||||||
preview?: ImageDto
|
preview?: ImageDto
|
||||||
tokens: Token[]
|
tokens: Token[]
|
||||||
tags: Tag[]
|
tags: Tag[]
|
||||||
|
featured: boolean
|
||||||
breadcrumb: Breadcrumb
|
breadcrumb: Breadcrumb
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodePage(page: Page, breadcrumb: Breadcrumb): PageDto {
|
export function encodePage(page: Page): PageDto {
|
||||||
const preview = page.preview ? encodeImage(page.preview) : null
|
const preview = page.preview ? encodeImage(page.preview) : null
|
||||||
const date = page.date ? page.date.toString() : null
|
const date = page.date ? page.date.toString() : null
|
||||||
|
const breadcrumb = page.getBreadcrumb()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: page.id,
|
id: page.id,
|
||||||
@@ -33,6 +35,7 @@ export function encodePage(page: Page, breadcrumb: Breadcrumb): PageDto {
|
|||||||
description: page.description,
|
description: page.description,
|
||||||
tokens: page.tokens,
|
tokens: page.tokens,
|
||||||
tags: page.tags,
|
tags: page.tags,
|
||||||
|
featured: page.featured,
|
||||||
preview,
|
preview,
|
||||||
date,
|
date,
|
||||||
breadcrumb,
|
breadcrumb,
|
||||||
|
|||||||
@@ -3,3 +3,16 @@ export function unique<T>(items: T[], key: (item: T) => string): T[]{
|
|||||||
|
|
||||||
return [...map.values()]
|
return [...map.values()]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ensureArray<T>(value: T | T[]): T[] {
|
||||||
|
if (!value) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
return [value]
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import { Request } from 'express'
|
||||||
|
import { ensureArray } from './array'
|
||||||
|
|
||||||
export function dropTrailingSlash(path: string) {
|
export function dropTrailingSlash(path: string) {
|
||||||
if (path === '/' || path === '') {
|
if (path === '/' || path === '') {
|
||||||
return '/'
|
return '/'
|
||||||
@@ -26,3 +29,16 @@ export function getPathWaterfall(path: string): string[] {
|
|||||||
|
|
||||||
return ['/', ...parts]
|
return ['/', ...parts]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function readQueryInt(req: Request, key: string) {
|
||||||
|
const int = (value: string | string[]) => parseInt(Array.isArray(value)
|
||||||
|
? value[0]
|
||||||
|
: value
|
||||||
|
)
|
||||||
|
|
||||||
|
return int(req.query[key]) || 0
|
||||||
|
}
|
||||||
|
|
||||||
|
export function readQueryArray(req: Request, key: string) {
|
||||||
|
return ensureArray(req.query[key])
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ async function main() {
|
|||||||
|
|
||||||
app.get('/pages', page.getAll)
|
app.get('/pages', page.getAll)
|
||||||
app.get('/pages/urls', page.getUrls)
|
app.get('/pages/urls', page.getUrls)
|
||||||
|
app.get('/pages/tags', page.getByTags)
|
||||||
app.get('/page', page.getItem)
|
app.get('/page', page.getItem)
|
||||||
|
|
||||||
app.get('/image', image.getItem)
|
app.get('/image', image.getItem)
|
||||||
|
|||||||
Reference in new issue
Block a user