Album model to ts
This commit is contained in:
1 parent
aa22f882bd
commit
eed3a3c007
3 files changed
+64
-66
No files matched your search
@@ -1,65 +0,0 @@
|
||||
const Data = require('./Data')
|
||||
const Image = require('./Image')
|
||||
const ImageArtifactType = require('./ImageArtifactType')
|
||||
const {find, findOne} = require('../lib/store')
|
||||
|
||||
const previewTypes = [
|
||||
ImageArtifactType.SMALL,
|
||||
ImageArtifactType.MEDIUM,
|
||||
ImageArtifactType.BIG,
|
||||
ImageArtifactType.ORIGIN,
|
||||
]
|
||||
const store = () => Data.getStore(Album)
|
||||
|
||||
class Album {
|
||||
static async find(query, options = {}) {
|
||||
const items = await find(store(), query, options)
|
||||
|
||||
return Promise.all(items.map(processAlbum))
|
||||
}
|
||||
|
||||
static async findById(id) {
|
||||
const item = await findOne(store(), {id})
|
||||
|
||||
return !item
|
||||
? null
|
||||
: processAlbum(item)
|
||||
}
|
||||
}
|
||||
|
||||
const previewFromImage = (imgs = []) => imgs.length
|
||||
? imgs[0]
|
||||
: null
|
||||
|
||||
const processAlbum = async album => {
|
||||
let preview = {}
|
||||
// {
|
||||
// data: [
|
||||
// {
|
||||
// url: null,
|
||||
// width: 0,
|
||||
// height: 0,
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
|
||||
const previewImageId = album.preview ? album.preview : previewFromImage(album.images)
|
||||
if (previewImageId) {
|
||||
const image = await Image.findById(previewImageId)
|
||||
if (image) {
|
||||
// const imageArtifacts = image.findArtifact(previewTypes)
|
||||
// if (imageArtifacts) {
|
||||
// preview = imageArtifacts
|
||||
// }
|
||||
preview = image.getPicture(ImageArtifactType.SMALL)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...album,
|
||||
preview,
|
||||
url: `/album/${album.id}`,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Album
|
||||
@@ -0,0 +1,63 @@
|
||||
import Data from './Data'
|
||||
import Image from './Image'
|
||||
import ImageArtifactType from './ImageArtifactType'
|
||||
import { find, findOne } from '../lib/store'
|
||||
|
||||
const previewTypes = [
|
||||
ImageArtifactType.SMALL,
|
||||
ImageArtifactType.MEDIUM,
|
||||
ImageArtifactType.BIG,
|
||||
ImageArtifactType.ORIGIN,
|
||||
]
|
||||
const store = () => Data.getStore('Album')
|
||||
|
||||
export default class Album {
|
||||
static async find(query, options = {}) {
|
||||
const items = await find(store(), query, options as any)
|
||||
|
||||
return Promise.all(items.map(processAlbum))
|
||||
}
|
||||
|
||||
static async findById(id) {
|
||||
const item = await findOne(store(), { id })
|
||||
|
||||
return !item
|
||||
? null
|
||||
: processAlbum(item)
|
||||
}
|
||||
}
|
||||
|
||||
const previewFromImage = (imgs = []) => imgs.length
|
||||
? imgs[0]
|
||||
: null
|
||||
|
||||
const processAlbum = async album => {
|
||||
let preview = {}
|
||||
// {
|
||||
// data: [
|
||||
// {
|
||||
// url: null,
|
||||
// width: 0,
|
||||
// height: 0,
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
|
||||
const previewImageId = album.preview ? album.preview : previewFromImage(album.images)
|
||||
if (previewImageId) {
|
||||
const image = await Image.findById(previewImageId)
|
||||
if (image) {
|
||||
// const imageArtifacts = image.findArtifact(previewTypes)
|
||||
// if (imageArtifacts) {
|
||||
// preview = imageArtifacts
|
||||
// }
|
||||
preview = image.getPicture(ImageArtifactType.SMALL)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...album,
|
||||
preview,
|
||||
url: `/album/${album.id}`,
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -38,7 +38,7 @@ function init(db) {
|
||||
|
||||
Data.setStore('Page', db.collection('pages'))
|
||||
Data.setStore('Article', db.collection('articles'))
|
||||
Data.setStore(Album, db.collection('albums'))
|
||||
Data.setStore('Album', db.collection('albums'))
|
||||
Data.setStore('Image', db.collection('images'))
|
||||
Data.setStore('Teacher', db.collection('collective'))
|
||||
Data.setStore(Document, db.collection('documents'))
|
||||
|
||||
Reference in new issue
Block a user