From 636de8e37a696469a637424336762e09966363f5 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Fri, 18 Mar 2016 01:55:33 +0300 Subject: [PATCH] + Articles --- src/client/api/API.js | 4 ++- src/client/api/APIArticle.js | 8 +++++ src/client/api/APIEvent.js | 5 --- src/client/app.js | 22 ++++++++++--- src/client/components/PageArticle.js | 30 +++++++++++++++++ src/client/components/PageEvent.js | 21 ------------ src/routes/articles.js | 38 ++++++++++++++++++++++ src/server.js | 4 ++- src/templates/components/page-article.html | 4 +++ src/templates/components/page-event.html | 3 -- 10 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 src/client/api/APIArticle.js create mode 100644 src/client/components/PageArticle.js delete mode 100644 src/client/components/PageEvent.js create mode 100644 src/routes/articles.js create mode 100644 src/templates/components/page-article.html delete mode 100644 src/templates/components/page-event.html diff --git a/src/client/api/API.js b/src/client/api/API.js index 5040cddc..d6f79aae 100644 --- a/src/client/api/API.js +++ b/src/client/api/API.js @@ -4,6 +4,7 @@ import APIDocument from './APIDocument'; import APITeacher from './APITeacher'; import APISchedule from './APISchedule'; import APIEvent from './APIEvent'; +import APIArticle from './APIArticle'; export default class API { constructor(request) { @@ -13,5 +14,6 @@ export default class API { this.teacher = new APITeacher(request); this.schedule = new APISchedule(request); this.event = new APIEvent(request); + this.article = new APIArticle(request); } -} \ No newline at end of file +} diff --git a/src/client/api/APIArticle.js b/src/client/api/APIArticle.js new file mode 100644 index 00000000..829ef67d --- /dev/null +++ b/src/client/api/APIArticle.js @@ -0,0 +1,8 @@ +import EndPoint from './EndPoint'; + +export default class APIArticle extends EndPoint { + id(id) { + let url = `/article/${id}`; + return this.get(url); + } +} \ No newline at end of file diff --git a/src/client/api/APIEvent.js b/src/client/api/APIEvent.js index 086bee8f..2c218548 100644 --- a/src/client/api/APIEvent.js +++ b/src/client/api/APIEvent.js @@ -8,9 +8,4 @@ export default class APIEvent extends EndPoint { let url = `/events?skip=${skip}&limit=${limit}`; return this.get(url); } - - post(id) { - let url = `/event/${id}`; - return this.get(url); - } } \ No newline at end of file diff --git a/src/client/app.js b/src/client/app.js index 4eeff5d8..dadddbc7 100644 --- a/src/client/app.js +++ b/src/client/app.js @@ -24,7 +24,7 @@ import Parallax from './components/Parallax'; import PageSchool from './components/PageSchool'; import PageEvents from './components/PageEvents'; -import PageEvent from './components/PageEvent'; +import PageArticle from './components/PageArticle'; import PageSchedule from './components/PageSchedule'; import PageCollective from './components/PageCollective'; import PageTeacher from './components/PageTeacher'; @@ -93,8 +93,17 @@ app.config(($locationProvider, $routeProvider) => { } }, { - name: '/article/:event', - template: '' + name: '/article/:id', + template: '', + resolveAs: '$', + resolve:{ + data: ($route, api) => { + let id = $route.current.params.id; + if(!id) return null; + return api.article.id(id) + .then(i => i.data) + } + } }, { name: '/schedule/:period?/:semester?', @@ -194,6 +203,11 @@ app.run(($location, $rootScope, $http) => { if (title) $rootScope.title = title; }); + $rootScope.$on('$routeChangeError', (event, current) => { + //let title = current['$$route'].title; + //if (title) $rootScope.title = title; + }); + $http.defaults.headers.common['Accept'] = 'application/json'; }); @@ -224,7 +238,7 @@ app.run(($location, $rootScope, $http) => { AlbumCollection, TeacherProfile, PageSchool, - PageEvent, + PageArticle, PageEvents, PageSchedule, PageCollective, diff --git a/src/client/components/PageArticle.js b/src/client/components/PageArticle.js new file mode 100644 index 00000000..7b92b931 --- /dev/null +++ b/src/client/components/PageArticle.js @@ -0,0 +1,30 @@ +import template from '../../templates/components/page-article.html'; + +export default function (app) { + app.component('pageArticle', { + bindings: { + article: '<' + }, + template: template, + controllerAs: '$', + controller: function ($rootScope, $routeParams, api, menu) { + menu.activate('/events'); + + if(this.article){ + $rootScope.title = this.article.title; + } + + //this.posts = [this.article]; + + //let id = $routeParams.id; + //if(!id) return; + // + //api.article.id(id) + // .then(i => i.data) + // .then(post => { + // $rootScope.title = post.title; + // this.posts = [post]; + // }); + } + }); +}; diff --git a/src/client/components/PageEvent.js b/src/client/components/PageEvent.js deleted file mode 100644 index a306bb99..00000000 --- a/src/client/components/PageEvent.js +++ /dev/null @@ -1,21 +0,0 @@ -import template from '../../templates/components/page-event.html'; - -export default function (app) { - app.component('pageEvent', { - template: template, - controllerAs: '$', - controller: function ($rootScope, $routeParams, api, menu) { - this.pageClass = 'page-event'; - menu.activate('/events'); - - let id = $routeParams.event; - if(!id) return; - - api.event.post(id) - .success(post => { - $rootScope.title = post.title; - this.posts = [post]; - }); - } - }); -}; \ No newline at end of file diff --git a/src/routes/articles.js b/src/routes/articles.js new file mode 100644 index 00000000..48880d8d --- /dev/null +++ b/src/routes/articles.js @@ -0,0 +1,38 @@ +import route from 'koa-route'; +import {c} from '../core/db'; +import {accepts, index} from './'; +import {sortBy} from '../utils/sort'; + +export default function (app) { + + function* exists(id) { + let i = yield c('events').findOne({id: id}); + if (!i) { + i = yield c('timeline').findOne({id: id}); + } + return i ? true : false; + } + + app.use(route.get('/article/:id', accepts({ + //'text/html': function* (id){ + // let test = yield exists(id); + // if(test){ + // return yield index(); + // }else{ + // this.status = 404; + // return yield index(); + // } + //}, + 'text/html': index(exists), + 'text/plain': index(exists), + 'application/json': function* (id) { + let i = yield c('events').findOne({id: id}); + if (!i) { + i = yield c('timeline').findOne({id: id}); + } + + if (!i) this.status = 404; + else this.body = i; + } + }))); +}; diff --git a/src/server.js b/src/server.js index d442cfa2..c4b822b5 100644 --- a/src/server.js +++ b/src/server.js @@ -24,6 +24,7 @@ import error404 from './routes/404'; import documents from './routes/documents'; import teachers from './routes/teachers'; import events from './routes/events'; +import articles from './routes/articles'; export const app = koa(); app.proxy = true; @@ -58,5 +59,6 @@ instagram(app); instagramIO(app); documents(app); events(app); +articles(app); teachers(app); -app.use(error404()); \ No newline at end of file +app.use(error404()); diff --git a/src/templates/components/page-article.html b/src/templates/components/page-article.html new file mode 100644 index 00000000..8d6a32e7 --- /dev/null +++ b/src/templates/components/page-article.html @@ -0,0 +1,4 @@ +
+ + +
diff --git a/src/templates/components/page-event.html b/src/templates/components/page-event.html deleted file mode 100644 index 2223e28b..00000000 --- a/src/templates/components/page-event.html +++ /dev/null @@ -1,3 +0,0 @@ -
- -