Documents serverside
This commit is contained in:
1 parent
244f1efd03
commit
864d895105
17 files changed
+528
-119
No files matched your search
@@ -0,0 +1,54 @@
|
||||
const Data = require('./Data')
|
||||
const Image = require('./Image')
|
||||
const {find, findOne} = require('../lib/store')
|
||||
|
||||
const store = () => Data.getStore(Document)
|
||||
|
||||
class Document {
|
||||
static async find(query, options = {}) {
|
||||
const items = await find(store(), query, options)
|
||||
|
||||
return Promise.all(items.map(processDocument))
|
||||
}
|
||||
|
||||
static async findById(id) {
|
||||
const data = await findOne(store(), {id})
|
||||
if (!data) return null
|
||||
|
||||
return processDocument(data)
|
||||
}
|
||||
|
||||
constructor(data) {
|
||||
this.id = data.id
|
||||
this.hash = data.hash
|
||||
this.file = new DocumentFile(data.file)
|
||||
this.type = data.type
|
||||
this.title = data.title
|
||||
this.category = data.category
|
||||
this.url = data.url
|
||||
|
||||
this.preview = data.preview
|
||||
this.viewUrl = `/document/${this.id}`
|
||||
}
|
||||
}
|
||||
|
||||
class DocumentFile {
|
||||
constructor(data) {
|
||||
this.name = data.name
|
||||
this.size = data.size
|
||||
}
|
||||
}
|
||||
|
||||
const
|
||||
processDocument = async data => {
|
||||
const imageId = data.preview
|
||||
const preview = await Image.findById(imageId)
|
||||
|
||||
return new Document({
|
||||
...data,
|
||||
preview,
|
||||
})
|
||||
}
|
||||
|
||||
module
|
||||
.exports = Document
|
||||
@@ -1,6 +1,7 @@
|
||||
const Data = require('./Data')
|
||||
const Album = require('./Album')
|
||||
const Teacher = require('./Teacher')
|
||||
const Document = require('./Document')
|
||||
const Image = require('./Image')
|
||||
|
||||
const {MongoClient, ObjectId} = require('mongodb')
|
||||
@@ -30,6 +31,7 @@ function init(db) {
|
||||
Data.setStore(Album, db.collection('albums'))
|
||||
Data.setStore(Image, db.collection('images'))
|
||||
Data.setStore(Teacher, db.collection('collective'))
|
||||
Data.setStore(Document, db.collection('documents'))
|
||||
|
||||
return db
|
||||
}
|
||||
Reference in new issue
Block a user