core/Teacher format

This commit is contained in:
tmshv committed 2019-10-14 17:14:45 +03:00
1 parent 57454962f1
commit 4fc424b145
1 file changed
+43 -43
+43 -43
View File
@@ -1,38 +1,38 @@
const Data = require('./Data') const Data = require('./Data')
const Image = require('./Image') const Image = require('./Image')
const {find, findOne} = require('../lib/store') const { find, findOne } = require('../lib/store')
const store = () => Data.getStore(Teacher) const store = () => Data.getStore(Teacher)
class Teacher { class Teacher {
static async find(query, options = {}) { static async find(query, options = {}) {
const items = await find(store(), query, options) const items = await find(store(), query, options)
return Promise.all(items.map(processProfile)) return Promise.all(items.map(processProfile))
} }
static async findById(id) { static async findById(id) {
const data = await findOne(store(), {id}) const data = await findOne(store(), { id })
if (!data) return null if (!data) return null
return processProfile(data) return processProfile(data)
} }
constructor(data) { constructor(data) {
this.id = data.id this.id = data.id
this.post = data.post this.post = data.post
this.file = data.file this.file = data.file
this.edu = data.edu this.edu = data.edu
this.picture = data.picture this.picture = data.picture
this.preview = data.preview this.preview = data.preview
this.diploma = data.diploma this.diploma = data.diploma
this.position = data.position this.position = data.position
this.name = data.name this.name = data.name
this.status = data.status this.status = data.status
this.hash = data.hash this.hash = data.hash
this.url = `/teacher/${this.id}` this.url = `/teacher/${this.id}`
} }
/** /**
* *
@@ -40,12 +40,12 @@ class Teacher {
* *
* @returns {string} * @returns {string}
*/ */
shortName() { shortName() {
const f = i => i.replace(/^(.).+/, '$1') const f = i => i.replace(/^(.).+/, '$1')
const [first, middle, last] = this.splitName() const [first, middle, last] = this.splitName()
return `${f(first)}. ${f(middle)}. ${last}` return `${f(first)}. ${f(middle)}. ${last}`
} }
/** /**
* *
@@ -53,24 +53,24 @@ class Teacher {
* *
* @returns {Array|{index: number, input: string}} * @returns {Array|{index: number, input: string}}
*/ */
splitName() { splitName() {
const name = this.name const name = this.name
const m = /(.+)\s(.+)\s(.+)/.exec(name) const m = /(.+)\s(.+)\s(.+)/.exec(name)
return m.slice(1) return m.slice(1)
} }
} }
const processProfile = async profile => { const processProfile = async profile => {
const imageId = profile.picture const imageId = profile.picture
const previewId = profile.preview const previewId = profile.preview
const picture = await Image.findById(imageId) const picture = await Image.findById(imageId)
const preview = await Image.findById(previewId) const preview = await Image.findById(previewId)
return new Teacher({ return new Teacher({
...profile, ...profile,
picture, picture,
preview, preview,
}) })
} }
module.exports = Teacher module.exports = Teacher