Files
hudozka/modules/hudozka-api/src/controllers/person.ts
T
2020-02-15 01:41:17 +03:00

61 lines
1.4 KiB
TypeScript

import Teacher from '../core/Teacher'
import { encodePerson } from '../factory/person'
import { Request, Response } from 'express'
import { prioritySort } from '../lib/sort'
export async function getItem(req: Request, res: Response) {
const id = req.params.slug
const data = await Teacher.findById(id)
if (data) {
res.json(encodePerson(data))
} else {
res.status(404)
res.json({
error: `Resource ${id} not found`
})
}
}
export async function getAll(req: Request, res: Response) {
const order = [
'mg-timasheva',
'va-sarzhin',
'od-gogoleva',
'my-valkova',
'nv-andreeva',
'vv-voronova',
]
const teachersSorted = prioritySort.bind(null, [...order], t => t.id)
const teachers = await Teacher.find({ hidden: false })
const data = teachersSorted(teachers).map(encodePerson)
if (data) {
res.json({
items: data,
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
}
export async function getUrls(req: Request, res: Response) {
const data = await Teacher.find({})
if (data) {
const items = data.map(x => `/teacher/${x.id}`)
res.json({
items
})
} else {
res.status(404)
res.json({
error: `Not found`
})
}
}