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
@@ -5,10 +5,6 @@ import { Tag } from './Tag'
|
||||
|
||||
const store = () => Data.getStore<Article>('Article')
|
||||
|
||||
function make(C) {
|
||||
return x => new C(x)
|
||||
}
|
||||
|
||||
export default class Article {
|
||||
static async find(query, options = {}): Promise<Article[]> {
|
||||
const items = await find(store(), query, options as any)
|
||||
@@ -63,7 +59,7 @@ export default class Article {
|
||||
this.date = data.date
|
||||
this.title = data.title
|
||||
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.hash = data.hash
|
||||
this.type = data.type
|
||||
@@ -76,27 +72,27 @@ export default class Article {
|
||||
this.url = `/article/${this.id}`
|
||||
}
|
||||
|
||||
plain() {
|
||||
return {
|
||||
id: this.id,
|
||||
images: this.images,
|
||||
post: this.post,
|
||||
folder: this.folder,
|
||||
date: this.date,
|
||||
title: this.title,
|
||||
until: this.until,
|
||||
tags: this.tags,
|
||||
content: this.content,
|
||||
hash: this.hash,
|
||||
type: this.type,
|
||||
origin: this.origin,
|
||||
file: this.file,
|
||||
version: this.version,
|
||||
preview: this.preview,
|
||||
url: this.url,
|
||||
featured: this.featured,
|
||||
}
|
||||
}
|
||||
// plain() {
|
||||
// return {
|
||||
// id: this.id,
|
||||
// images: this.images,
|
||||
// post: this.post,
|
||||
// folder: this.folder,
|
||||
// date: this.date,
|
||||
// title: this.title,
|
||||
// until: this.until,
|
||||
// tags: this.tags,
|
||||
// content: this.content,
|
||||
// hash: this.hash,
|
||||
// type: this.type,
|
||||
// origin: this.origin,
|
||||
// file: this.file,
|
||||
// version: this.version,
|
||||
// preview: this.preview,
|
||||
// url: this.url,
|
||||
// featured: this.featured,
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
const previewFromImage = (imgs = []) => imgs.length
|
||||
|
||||
@@ -2,11 +2,12 @@ import Data from './Data'
|
||||
import ImageArtifactType from './ImageArtifactType'
|
||||
import { findOne } from '../lib/store'
|
||||
import { mapOf } from '../server/lib'
|
||||
import { ObjectId } from 'mongodb'
|
||||
|
||||
const store = () => Data.getStore('Image')
|
||||
|
||||
export default class Image {
|
||||
static async findById(id) {
|
||||
static async findById(id: ObjectId) {
|
||||
const data = await findOne(store(), { _id: id })
|
||||
if (!data) return null
|
||||
|
||||
|
||||
@@ -2,9 +2,17 @@ import Data from './Data'
|
||||
import { find as storeFind, findOne, total } from '../lib/store'
|
||||
import Image from './Image'
|
||||
import { Tag } from './Tag'
|
||||
import { ObjectId } from 'mongodb'
|
||||
import { Breadcrumb } from '../types'
|
||||
|
||||
const store = () => Data.getStore('Page')
|
||||
|
||||
type PageDto = {
|
||||
[name: string]: any
|
||||
preview?: ObjectId
|
||||
images: ObjectId[]
|
||||
}
|
||||
|
||||
export default class Page {
|
||||
static async find(query, options = {}): Promise<Page[]> {
|
||||
const items = await storeFind(store(), query, options as any)
|
||||
@@ -20,7 +28,7 @@ export default class Page {
|
||||
: processPage(item)
|
||||
}
|
||||
|
||||
static async findByUrl(url) {
|
||||
static async findByUrl(url: string) {
|
||||
const item = await findOne(store(), { url })
|
||||
|
||||
return !item
|
||||
@@ -41,13 +49,16 @@ export default class Page {
|
||||
public date: Date
|
||||
public tags: Tag[]
|
||||
public data: any
|
||||
public images: any
|
||||
public images: ObjectId[]
|
||||
public documents: any
|
||||
public preview: any
|
||||
public preview?: Image
|
||||
public tokens: any[]
|
||||
public description: string
|
||||
public featured: boolean
|
||||
|
||||
constructor(data) {
|
||||
private breadcrumb: Breadcrumb
|
||||
|
||||
constructor(data: { [name: string]: any }) {
|
||||
this.id = data.id
|
||||
this.hash = data.hash
|
||||
this.file = data.file
|
||||
@@ -60,27 +71,43 @@ export default class Page {
|
||||
this.documents = data.documents
|
||||
this.preview = data.preview
|
||||
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
|
||||
}
|
||||
|
||||
public setBreadcrumb(value: Breadcrumb) {
|
||||
this.breadcrumb = value
|
||||
return this
|
||||
}
|
||||
|
||||
public getBreadcrumb() {
|
||||
return this.breadcrumb
|
||||
}
|
||||
|
||||
public getUrl() {
|
||||
return this.url
|
||||
}
|
||||
}
|
||||
|
||||
const previewFromImage = (imgs = []) => imgs.length
|
||||
? imgs[0]
|
||||
: null
|
||||
function firstItem<T>(items: T[] = []): T {
|
||||
return items.length
|
||||
? items[0]
|
||||
: null
|
||||
}
|
||||
|
||||
async function processPage(article) {
|
||||
let preview = null
|
||||
const previewId = article.preview
|
||||
? article.preview
|
||||
: previewFromImage(article.images)
|
||||
async function processPage(item: PageDto): Promise<Page> {
|
||||
let preview: Image = null
|
||||
const previewId = item.preview
|
||||
? item.preview
|
||||
: firstItem(item.images)
|
||||
|
||||
if (previewId) {
|
||||
preview = await Image.findById(previewId)
|
||||
}
|
||||
|
||||
return new Page({
|
||||
...article,
|
||||
...item,
|
||||
preview,
|
||||
})
|
||||
}
|
||||
@@ -1,15 +1,20 @@
|
||||
import { translit } from '../lib/translit'
|
||||
|
||||
export class Tag {
|
||||
public static fromCyrillic(tag: string) {
|
||||
const slug = tag
|
||||
? translit(tag).toLowerCase()
|
||||
: tag
|
||||
|
||||
return new Tag(tag, slug)
|
||||
}
|
||||
|
||||
public name: string
|
||||
public slug: string
|
||||
|
||||
constructor(tag: string) {
|
||||
this.name = tag
|
||||
|
||||
if (tag) {
|
||||
this.slug = translit(tag).toLowerCase()
|
||||
}
|
||||
constructor(name: string, slug: string) {
|
||||
this.name = name
|
||||
this.slug = slug
|
||||
}
|
||||
|
||||
public isValid() {
|
||||
|
||||
Reference in new issue
Block a user