diff --git a/client/app.js b/client/app.js index cf8d94a3..83a9a678 100644 --- a/client/app.js +++ b/client/app.js @@ -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); diff --git a/client/pages/schedule.js b/client/pages/schedule.js index 2fc0b0b9..d5131c04 100644 --- a/client/pages/schedule.js +++ b/client/pages/schedule.js @@ -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; diff --git a/models/schedule.js b/models/schedule.js index fbdcf2fb..e7c1a6bf 100644 --- a/models/schedule.js +++ b/models/schedule.js @@ -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 = []; diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 00000000..1bdc7865 --- /dev/null +++ b/routes/index.js @@ -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 +}; diff --git a/routes/schedule.js b/routes/schedule.js new file mode 100644 index 00000000..82febb79 --- /dev/null +++ b/routes/schedule.js @@ -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; + } + } + )) + ); +}; \ No newline at end of file diff --git a/server.js b/server.js index a496c38c..d98fb5c5 100644 --- a/server.js +++ b/server.js @@ -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; + } +}