api cleaned up from person/teacher model at all

This commit is contained in:
tmshv committed 2020-02-25 04:35:05 +03:00
1 parent ebd6290ccf
commit fdac9d9ff8
10 files changed
+1 -222

No files matched your search

@@ -1,60 +0,0 @@
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`
})
}
}
-2
View File
@@ -20,7 +20,6 @@ export default class Config {
public file: any
public server: any
public articlesPerPage: any
public collectiveOrder: any
public articleCardDefaultPreview: any
public collectiveImage: any
public redirect: any
@@ -30,7 +29,6 @@ export default class Config {
this.file = file
this.server = data.server
this.articlesPerPage = data.articlesPerPage || 5
this.collectiveOrder = data.collectiveOrder || []
this.articleCardDefaultPreview = data.articleCardDefaultPreview
this.collectiveImage = data.collectiveImage
-87
View File
@@ -1,87 +0,0 @@
import Data from './Data'
import Image from './Image'
import { find, findOne } from '../lib/store'
const store = () => Data.getStore('Teacher')
export default class Teacher {
static async find(query, options = {}): Promise<Teacher[]> {
const items = await find(store(), query, options as any)
return Promise.all(items.map(processProfile))
}
static async findById(id) {
const data = await findOne(store(), { id })
if (!data) return null
return processProfile(data)
}
public id: any
public post: any
public file: any
public edu: any
public picture: any
public preview: any
public diploma: any
public position: any
public name: any
public status: any
public hash: any
public url: any
constructor(data) {
this.id = data.id
this.post = data.post
this.file = data.file
this.edu = data.edu
this.picture = data.picture
this.preview = data.preview
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 previewId = profile.preview
const picture = await Image.findById(imageId)
const preview = await Image.findById(previewId)
return new Teacher({
...profile,
picture,
preview,
})
}
-19
View File
@@ -1,19 +0,0 @@
import {c} from '../core/db';
const PAGES = 'pages';
export async function getPageUrls() {
return await c(PAGES)
.find({}, {url: 1})
.toArray();
}
/**
* Find a person by it's id
*
* @param {string} url
* @returns {Promise|*}
*/
export async function getPage(url) {
return await c(PAGES).findOne({url: url});
}
-1
View File
@@ -30,7 +30,6 @@ function init(db: Db) {
Data.setStore('Article', db.collection('articles'))
Data.setStore('Album', db.collection('albums'))
Data.setStore('Image', db.collection('images'))
Data.setStore('Teacher', db.collection('collective'))
Data.setStore('Document', db.collection('documents'))
Data.setStore('Settings', db.collection('settings'))
-13
View File
@@ -1,13 +0,0 @@
import ImageArtifactType from '../core/ImageArtifactType'
import { encodeImage } from './image'
const profilePicture = profile => profile.picture.getPicture(ImageArtifactType.BIG)
export function encodePerson(person) {
return {
...person,
shortName: person.shortName(),
name: person.splitName(),
picture: profilePicture(person),
preview: encodeImage(person.picture),
}
}
-15
View File
@@ -1,17 +1,3 @@
function prioritySort(priority, key, list) {
priority = priority.reverse()
let weight = i => {
const p = key(i)
const index = priority.indexOf(p)
return index > -1
? index + 1
: 0
}
return sort(list, weight)
}
function sort(list, fn, reverse = false) {
return list
.map(i => [fn(i), i])
@@ -32,6 +18,5 @@ function sortBy(fn, reverse = false) {
}
}
exports.prioritySort = prioritySort
exports.sortBy = sortBy
exports.sort = sort
-5
View File
@@ -1,7 +1,6 @@
import { config } from 'dotenv'
import express from 'express'
import { connect } from './db'
import * as person from './controllers/person'
import * as article from './controllers/article'
import * as album from './controllers/album'
import * as schedule from './controllers/schedule'
@@ -25,10 +24,6 @@ async function main() {
}
const app = express()
app.get('/persons', person.getAll)
app.get('/persons/urls', person.getUrls)
app.get('/persons/:slug', person.getItem)
app.get('/articles', article.getAll)
app.get('/articles/urls', article.getUrls)
app.get('/articles/:slug', article.getItem)
@@ -1,18 +0,0 @@
const { collection } = require('../db')
// import { collection } from '../core/db'
export async function getCollective(query = {}) {
return await collection('collective')
.find(query)
.toArray();
}
/**
* Find a person by it's id
*
* @param id
* @returns {Promise|*}
*/
export async function getPerson(id) {
return await collection('collective').findOne({ id: id });
}
+1 -2
View File
@@ -1,5 +1,4 @@
import { createMap } from '../utils/common'
import { getCollective } from './collective'
import {
scheduleDate,
getPeriod as schedulePeriod,
@@ -96,7 +95,7 @@ function migrate10to20(schedule) {
async function migrate20to30(schedule) {
const days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat']//, 'sun']
const persons = await getCollective()
const persons = []
const person = createMap(i => i.id, persons)
const week = week => days