updated schedule api; updated index routing; + routing based on Accept header;

This commit is contained in:
tmshv committed 2015-03-01 01:59:00 +03:00
1 parent 6af90258c9
commit a694abb3e5
6 files changed
+148 -29

No files matched your search

+3 -1
View File
@@ -46,10 +46,12 @@ app.config(function ($locationProvider, $routeProvider) {
});
});
app.run(function($location, $rootScope) {
app.run(function($location, $rootScope, $http) {
$rootScope.$on("$routeChangeSuccess", function (event, current, previous) {
$rootScope.title = current.$$route.title;
});
$http.defaults.headers.common["Accept"] = "application/json";
});
require("./api/api")(app);
+16 -8
View File
@@ -2,20 +2,28 @@ var populate = require("../../utils/populate").populate;
const course = require("../../models/course");
module.exports = function (app) {
app.controller("ScheduleController", function ($scope, schedule, team) {
app.controller("ScheduleController", function ($scope, $http, schedule, team) {
var now = new Date();
var dates = getDates(now);
var sem = "late";
$scope.sem = [["early", "весеннего"], ["late", "осеннего"]].reduce(function (s, word) {
var sem = "autumn";
$scope.sem = [["spring", "весеннего"], ["autumn", "осеннего"]].reduce(function (s, word) {
if (word[0] === sem) return word[1];
});
$scope.year = 2014;
$scope.year = "2014-2015";
$scope.schedule = schedule($scope.year, sem, [
populate(team.short, "teacher"),
populate(course.name, "lesson")
]);
$http.get("/schedule/2014-2015/spring")
.success(function (s) {
$scope.schedule = schedule.populate(s.schedule, [
populate(team.short, "teacher"),
populate(course.name, "lesson")
]);
});
//$scope.schedule = schedule($scope.year, sem, [
// populate(team.short, "teacher"),
// populate(course.name, "lesson")
//]);
$scope.isToday = function (weekDayIndex) {
return (now.getDay() - 1) === weekDayIndex;
+8 -5
View File
@@ -6,7 +6,14 @@ const SCHEDULES = {
"2014-late": require("./schedule-2014-late.json")
};
module.exports = function (year, name, pops) {
function schedule (year, name, pops){
var s = SCHEDULES[year + "-" + name];
return populateSchedule(s, pops);
}
schedule.populate = populateSchedule;
module.exports = schedule;
//var s = require(
// [year, name].reduce(function (r, v, i) {
// var n = "{i}".replace("i", i);
@@ -14,10 +21,6 @@ module.exports = function (year, name, pops) {
// }, "./schedule-{0}-{1}.json")
//);
var s = SCHEDULES[year + "-" + name];
return populateSchedule(s, pops);
};
function populateSchedule(s, pops){
var days = function (group) {
var result = [];
+42
View File
@@ -0,0 +1,42 @@
/**
* Created by tmshv on 01/03/15.
*/
var path = require("path");
var fs = require("mz/fs");
function accepts(routes, def) {
return function *() {
var req = this.request;
var types = Object.keys(routes)
.filter(function (type) {
return req.accepts(type);
});
if (types.length) {
var type = types[0];
yield routes[type].apply(this, arguments);
} else if(def){
yield def;
}else{
this.status = 406;
return;
}
};
}
function index(filename) {
if (!filename){
filename = path.join(__dirname, "../templates/index.html");
}
return function *() {
this.type = "text/html";
this.body = fs.createReadStream(filename);
}
}
module.exports = {
index: index,
accepts: accepts
};
+47
View File
@@ -0,0 +1,47 @@
/**
* Created by tmshv on 22/11/14.
*/
var route = require("koa-route");
var router = require("./");
var db = require("../core/db");
var schedule = require("../models/schedule");
var populate = require("../utils/populate").populate;
const course = require("../models/course");
const team = require("../models/team");
module.exports = function (app) {
app.use(
route.get("/schedule/:period?/:semester?", router.accepts(
{
"text/html": router.index(),
"text/plain": router.index(),
"application/json": function *(period, semester) {
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(course.name, "lesson")
]);
}
this.body = data;
}
}
))
);
};
+32 -15
View File
@@ -8,6 +8,7 @@ var logger = require("koa-logger");
var prerender = require('koa-prerender');
var config = require("./config");
var routes = require("./routes");
var app = koa();
app.use(logger());
@@ -17,27 +18,43 @@ app.use(prerender(config.prerender));
app.use(serve(path.join(__dirname, "public")));
app.use(serve(path.join(__dirname, "templates")));
app.use(route.get("/", index));
app.use(route.get("/schedule", index));
app.use(route.get("/team", index));
app.use(route.get("/docs", index));
app.use(function *(next) {
var q = this.query;
this.query = Object.keys(q)
.reduce(function (q, key) {
var v = q[key];
if(v === "true"){
v = true;
}else if(v === "false"){
v = false;
}
q[key] = v;
return q;
}, q);
app.use(route.get("/document/:doc", document));
yield next;
});
function *index(){
var fn = path.join(__dirname, "templates/index.html");
this.type = "text/html";
this.body = fs.createReadStream(fn);
}
app.use(route.get("/", routes.index()));
app.use(route.get("/team", routes.index()));
app.use(route.get("/docs", routes.index()));
function *document(){
var fn = path.join(__dirname, "templates/document.html");
this.type = "text/html";
this.body = fs.createReadStream(fn);
}
app.use(route.get("/document/:doc", routes.index(
path.join(__dirname, "templates/document.html")
)));
require("./api/feed")(app);
require("./routes/schedule")(app);
module.exports = function (port) {
app.listen(port);
};
function *schedule(){
if(this.request.accepts("json")){
this.body = {schedule: ["hello", "lol"]};
this.type = "application/json";
}else{
yield index;
}
}