core update, schedule router update, schedule populator update, collective update, other
This commit is contained in:
1 parent
7d97953877
commit
f3aaf5a547
8 files changed
+113
-178
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
import {c} from '../core/db';
|
||||||
|
|
||||||
|
export function* getCollective(query={}, show={}) {
|
||||||
|
let data = yield c('collective').find(query, show);
|
||||||
|
if (!data) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
@@ -16,6 +16,10 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
close: function () {
|
||||||
|
client.close();
|
||||||
|
},
|
||||||
|
|
||||||
get db() {
|
get db() {
|
||||||
return client;
|
return client;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import {c} from '../core/db';
|
||||||
|
import {getCollective} from './collective';
|
||||||
|
|
||||||
|
import {populate} from '../models/schedule';
|
||||||
|
import {shortifyName} from '../models/collective';
|
||||||
|
import {courses} from '../models/course';
|
||||||
|
|
||||||
|
export function* getSchedule(period, semester) {
|
||||||
|
let data = yield c('schedules').findOne({
|
||||||
|
period: period,
|
||||||
|
semester: semester
|
||||||
|
});
|
||||||
|
if (data) return data;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function* getScheduleList() {
|
||||||
|
return yield c('schedules')
|
||||||
|
.find({}, {period: 1, semester: 1})
|
||||||
|
.toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function* populateSchedule(schedule) {
|
||||||
|
let collective = yield getCollective({}, {id: 1, name: 1});
|
||||||
|
let collective_dict = collective.reduce((dict, i) => {
|
||||||
|
dict[i.id] = i.name;
|
||||||
|
return dict;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
let populateLesson = populate(courses);
|
||||||
|
let populateTeacher = populate(collective_dict, i => shortifyName(i));
|
||||||
|
|
||||||
|
return schedule.map(group => {
|
||||||
|
return {
|
||||||
|
name: group.name,
|
||||||
|
time: group.time,
|
||||||
|
days: group.days.map(record => {
|
||||||
|
if (!record) return record;
|
||||||
|
|
||||||
|
let lesson = record.lesson;
|
||||||
|
let teacher = record.teacher;
|
||||||
|
let time = record.time;
|
||||||
|
|
||||||
|
return {
|
||||||
|
time: time,
|
||||||
|
lesson: populateLesson(lesson),
|
||||||
|
teacher: populateTeacher(teacher)
|
||||||
|
};
|
||||||
|
})
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,36 +1,9 @@
|
|||||||
//getPersonByID
|
export function shortifyName(name) {
|
||||||
export function person(collective, id){
|
let $ = i => i.replace(/^(.).+/, '$1');
|
||||||
for(var i=0; i<collective.length; i++){
|
let [_, first, middle, last] = splitName(name);
|
||||||
var p = collective[i];
|
return `${$(first)}. ${$(middle)}. ${last}`;
|
||||||
if(p.id == id){
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//getNameByID
|
export function splitName(name) {
|
||||||
export function name(collective, id){
|
return /(.+)\s(.+)\s(.+)/.exec(name);
|
||||||
var p = person(collective, id);
|
|
||||||
if(p) return p.name;
|
|
||||||
else return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//getShortNameByID
|
|
||||||
export function short(collective, id){
|
|
||||||
var n = name(collective, id);
|
|
||||||
if(n) return shortifyName(n);
|
|
||||||
else return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function shortifyName(name){
|
|
||||||
var m = splitName(name);
|
|
||||||
var n = m[1].replace(/^(.).+/, "$1. ") +
|
|
||||||
m[2].replace(/^(.).+/, "$1. ") +
|
|
||||||
m[3];
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function splitName(name){
|
|
||||||
return /(.+)\s(.+)\s(.+)/.exec(name);
|
|
||||||
}
|
}
|
||||||
+9
-30
@@ -1,33 +1,12 @@
|
|||||||
export const courses = [
|
export const courses = {
|
||||||
{
|
painting: "Живопись",
|
||||||
"id": "painting",
|
drawing: "Рисунок",
|
||||||
"name": "Живопись"
|
sculpting: "Скульптура",
|
||||||
},
|
ceramic: "Керамика",
|
||||||
{
|
composition: "Композиция",
|
||||||
"id": "drawing",
|
history: "История искусства",
|
||||||
"name": "Рисунок"
|
cg: "Комп. графика"
|
||||||
},
|
};
|
||||||
{
|
|
||||||
"id": "sculpting",
|
|
||||||
"name": "Скульптура"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "ceramic",
|
|
||||||
"name": "Керамика"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "composition",
|
|
||||||
"name": "Композиция"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "history",
|
|
||||||
"name": "История искусства"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "cg",
|
|
||||||
"name": "Комп. графика"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
export function getCourseByID(id){
|
export function getCourseByID(id){
|
||||||
for(var i=0; i<courses.length; i++){
|
for(var i=0; i<courses.length; i++){
|
||||||
|
|||||||
+4
-61
@@ -1,62 +1,5 @@
|
|||||||
var pop = require("../utils/populate").pop;
|
let tryReplace = (dict, record, map = i=>i) =>
|
||||||
|
record in dict ? map(dict[record]) : record;
|
||||||
|
|
||||||
const WEEK = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
|
export const populate = (dict, map) =>
|
||||||
|
id => tryReplace(dict, id, map);
|
||||||
module.exports = {
|
|
||||||
populate: populateSchedule
|
|
||||||
};
|
|
||||||
|
|
||||||
function populateSchedule(s, pops) {
|
|
||||||
var days = function (group) {
|
|
||||||
var result = [];
|
|
||||||
|
|
||||||
WEEK.forEach(function (i) {
|
|
||||||
var lesson = group[i];
|
|
||||||
if (lesson) {
|
|
||||||
result.push(
|
|
||||||
pop(lesson)
|
|
||||||
.populate(time())
|
|
||||||
.populate(pops)
|
|
||||||
.result()
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
result.push(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
var time = function () {
|
|
||||||
return function (lesson) {
|
|
||||||
for (var i = 0; i < lesson.time.length; i++) {
|
|
||||||
lesson.time[i] = lesson.time[i].replace(" ", "—");
|
|
||||||
}
|
|
||||||
return lesson;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var mondayTime = function (group) {
|
|
||||||
var day;
|
|
||||||
for (var i = 0; i < WEEK.length; i++) {
|
|
||||||
if (group[WEEK[i]]) {
|
|
||||||
day = WEEK[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (day) {
|
|
||||||
return group[day].time[0].substr(0, 5);
|
|
||||||
} else {
|
|
||||||
return "00:00"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var result = [];
|
|
||||||
s.forEach(function (group) {
|
|
||||||
result.push({
|
|
||||||
name: group.name,
|
|
||||||
days: days(group),
|
|
||||||
time: mondayTime(group)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
+23
-48
@@ -1,62 +1,37 @@
|
|||||||
/**
|
import route from 'koa-route';
|
||||||
* Created by tmshv on 22/11/14.
|
import {json} from './';
|
||||||
*/
|
|
||||||
|
|
||||||
var route = require("koa-route");
|
import {getSchedule, getScheduleList, populateSchedule} from '../core/schedule';
|
||||||
var router = require("./");
|
|
||||||
var db = require("../core/db");
|
|
||||||
var schedule = require("../models/schedule");
|
|
||||||
var populate = require("../utils/populate").populate;
|
|
||||||
//import {getCourseNameByID} from "../models/course";
|
|
||||||
//const team = require("../models/team");
|
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function (app) {
|
||||||
app.use(
|
app.use(
|
||||||
route.get("/schedule/:period?/:semester?", router.accepts(
|
route.get('/schedule/list', json(
|
||||||
{
|
function *() {
|
||||||
"text/html": router.index(),
|
this.type = 'application/json';
|
||||||
"text/plain": router.index(),
|
let data = yield getScheduleList();
|
||||||
|
if (!data) this.status = 404;
|
||||||
"application/json": function *(period, semester) {
|
else this.body = data;
|
||||||
this.type = "application/json";
|
|
||||||
|
|
||||||
var doPopulate = this.query.populate || false;
|
|
||||||
|
|
||||||
var data = yield db.c("schedules").findOne({
|
|
||||||
period: period,
|
|
||||||
semester: semester
|
|
||||||
});
|
|
||||||
|
|
||||||
if(!data) {
|
|
||||||
this.status = 404;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(doPopulate) {
|
|
||||||
//data.schedule = schedule.populate(data.schedule, [
|
|
||||||
// populate(team.short, "teacher"),
|
|
||||||
// populate(getCourseNameByID, "lesson")
|
|
||||||
//]);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.body = data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
route.get("/schedules", router.accepts(
|
route.get('/schedule/:period?/:semester?', json(
|
||||||
{
|
function *(period, semester) {
|
||||||
"text/html": router.index(),
|
this.type = 'application/json';
|
||||||
"text/plain": router.index(),
|
|
||||||
|
|
||||||
"application/json": function *() {
|
let data = yield getSchedule(period, semester);
|
||||||
this.type = "application/json";
|
if (!data) {
|
||||||
var data = yield db.c("schedules").find({}, {period:1 , semester: 1}).toArray();
|
this.status = 404;
|
||||||
if(!data) this.status = 404;
|
return;
|
||||||
else this.body = data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let doPopulate = this.query.populate || false;
|
||||||
|
if (doPopulate) {
|
||||||
|
data.schedule = yield populateSchedule(data.schedule);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.body = data;
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -25,11 +25,11 @@ export function pop(data){
|
|||||||
return fun;
|
return fun;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function populate(find, field){
|
export function populate(fn, key){
|
||||||
return function(lesson){
|
return (dict) => {
|
||||||
var id = lesson[field];
|
let id = dict[key];
|
||||||
var val = find(id);
|
let value = fn(id);
|
||||||
if(val) lesson[field] = val;
|
dict[key] = value ? value : id;
|
||||||
return lesson;
|
return dict;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user