Serverside Teachers

This commit is contained in:
tmshv committed 2017-05-26 01:38:49 +03:00
1 parent f257e08e7c
commit 525f630812
11 files changed
+275 -87

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
const Data = require('./Data')
const Image = require('./Image')
const {find, findOne} = require('../lib/store')
const store = () => Data.getStore(Teacher)
class Teacher {
static async find(query, options = {}) {
const items = await find(store(), query, options)
return Promise.all(items.map(processProfile))
}
static async findById(id) {
const data = await findOne(store(), {id})
if (!data) return null
return processProfile(data)
}
constructor(data) {
this.id = data.id
this.biography = data.biography
this.file = data.file
this.edu = data.edu
this.picture = data.picture
this.diploma = data.diploma
this.position = data.position
this.name = data.name
this.status = data.status
this.hash = data.hash
this.url = `/teacher/${this.id}`
}
/**
*
* "First Middle Last" -> "F. M. Last"
*
* @returns {string}
*/
shortName() {
const f = i => i.replace(/^(.).+/, '$1')
const [first, middle, last] = this.splitName()
return `${f(first)}. ${f(middle)}. ${last}`
}
/**
*
* "First Middle Last" -> ["First", "Middle", "Last"]
*
* @returns {Array|{index: number, input: string}}
*/
splitName() {
const name = this.name
const m = /(.+)\s(.+)\s(.+)/.exec(name)
return m.slice(1)
}
}
const processProfile = async profile => {
const imageId = profile.picture
const picture = await Image.findById(imageId)
return new Teacher({
...profile,
picture,
})
}
module.exports = Teacher
+2
View File
@@ -1,5 +1,6 @@
const Data = require('./Data')
const Album = require('./Album')
const Teacher = require('./Teacher')
const Image = require('./Image')
const {MongoClient, ObjectId} = require('mongodb')
@@ -28,6 +29,7 @@ export function id(i) {
function init(db) {
Data.setStore(Album, db.collection('albums'))
Data.setStore(Image, db.collection('images'))
Data.setStore(Teacher, db.collection('collective'))
return db
}