From b1ad87ebdfb11c9041d12d4eb96ddf535593e1a4 Mon Sep 17 00:00:00 2001 From: "siarhei.mikhailau" Date: Wed, 11 Sep 2013 11:53:27 +0300 Subject: [PATCH] rest initial commit, data.json has been changed, data provider was re-factored, edit view fixes --- data.json | 107 +++++++-------------- scripts/app.js | 6 +- scripts/controllers/calendar-controller.js | 49 +++++----- scripts/controllers/details-controller.js | 10 +- scripts/controllers/edit-controller.js | 10 +- scripts/services/data-provider.js | 36 ++----- server.js | 79 +-------------- server_rest.js | 93 ++++++++++++++++++ views/calendar-page.html | 6 +- views/details-page.html | 11 ++- views/edit-page.html | 21 +++- views/index.html | 1 + 12 files changed, 204 insertions(+), 225 deletions(-) create mode 100644 server_rest.js diff --git a/data.json b/data.json index cc2475d..1257917 100644 --- a/data.json +++ b/data.json @@ -1,7 +1,6 @@ { - "talks": [ - { - "_id": "1", + "talks": { + "1": { "date": "2\/22\/2013", "title": "CSS via JS", "lector": ["siarhei_mikhailau"], @@ -27,8 +26,7 @@ - { - "_id": "2", + "2": { "date": "2\/27\/2013", "title": "CSS via JS", "lector": ["siarhei_mikhailau"], @@ -58,8 +56,7 @@ }, - { - "_id": "3", + "3": { "date": "2\/27\/2013", "title": "CSS via JS", "lector": ["siarhei_mikhailau"], @@ -90,8 +87,7 @@ }, - { - "_id": "4", + "4": { "date": "2\/25\/2013", "title": "Tetris", "lector": ["siarhei_mikhailau"], @@ -116,8 +112,7 @@ - { - "_id": "7", + "7": { "date": "4\/12\/2013", "title": "noJS framework or \":checked\"", "lector": ["siarhei_mikhailau"], @@ -139,8 +134,7 @@ }, - { - "_id": "5", + "5": { "date": "3\/6\/2013", "title": "Tetris", "lector": ["siarhei_mikhailau"], @@ -174,8 +168,7 @@ }, - { - "_id": "6", + "6": { "date": "3\/5\/2013", "title": "Tetris", "lector": ["siarhei_mikhailau"], @@ -205,8 +198,7 @@ - { - "_id": "8", + "8": { "date": "4\/16\/2013", "title": "Tetris", "lector": ["siarhei_mikhailau"], @@ -229,8 +221,7 @@ }, - { - "_id": "9", + "9": { "date": "3\/12\/2013", "title": "Backbone", "lector": ["aliaksandr_arlou"], @@ -274,8 +265,7 @@ }, - { - "_id": "10", + "10": { "date": "3\/20\/2013", "title": "NodeJS", "lector": ["andrei_barysiuk"], @@ -322,8 +312,7 @@ - { - "_id": "11", + "11": { "date": "3\/19\/2013", "title": "Web Inspector", "lector": ["siarhei_mikhailau"], @@ -356,8 +345,7 @@ }, - { - "_id": "12", + "12": { "date": "3\/27\/2013", "title": "Web Inspector", "lector": ["siarhei_mikhailau"], @@ -395,8 +383,7 @@ ] }, - { - "_id": "13", + "13": { "date": "4\/10\/2013", "title": "Web Inspector", "lector": ["siarhei_mikhailau"], @@ -427,8 +414,7 @@ ] }, - { - "_id": "14", + "14": { "date": "4\/19\/2013", "title": "Web Inspector", "lector": ["siarhei_mikhailau"], @@ -455,8 +441,7 @@ }, - { - "_id": "15", + "15": { "date": "3\/28\/2013", "title": "CSS Flexbox", "lector": ["maria_putyrskaya"], @@ -482,8 +467,7 @@ - { - "_id": "16", + "16": { "date": "4\/3\/2013", "title": "Javascript Modularity", "lector": ["andrei_barysiuk"], @@ -539,8 +523,7 @@ }, - { - "_id": "17", + "17": { "date": "4\/24\/2013", "title": "Chrome Extensions", "lector": ["yauhen_karmyzau"], @@ -577,8 +560,7 @@ ] }, - { - "_id": "18", + "18": { "date": "4\/4\/2013", "title": "Chrome Extensions", "lector": ["yauhen_karmyzau"], @@ -602,8 +584,7 @@ ] }, - { - "_id": "19", + "19": { "date": "4\/26\/2013", "title": "Chrome Extensions", "lector": ["yauhen_karmyzau"], @@ -624,8 +605,7 @@ }, - { - "_id": "20", + "20": { "date": "4\/10\/2013", "title": "Javascript Animation", "lector": ["mikita_khatsimtsou"], @@ -671,8 +651,7 @@ }, - { - "_id": "21", + "21": { "date": "4\/5\/2013", "title": "Javascript Animation", "lector": ["mikita_khatsimtsou"], @@ -699,8 +678,7 @@ - { - "_id": "22", + "22": { "date": "4\/11\/2013", "title": "Sencha Intro", "lector": ["uladzimir_hartsau"], @@ -730,8 +708,7 @@ - { - "_id": "23", + "23": { "date": "4\/17\/2013", "title": "10 CSS Secrets", "lector": ["siarhei_mikhailau"], @@ -775,8 +752,7 @@ ] }, - { - "_id": "24", + "24": { "date": "4\/18\/2013", "title": "10 CSS Secrets", "lector": ["siarhei_mikhailau"], @@ -807,8 +783,7 @@ - { - "_id": "25", + "25": { "date": "4\/23\/2013", "title": "Widgets for Windows", "lector": ["andrei_dzeuhuts"], @@ -835,8 +810,7 @@ - { - "_id": "26", + "26": { "date": "4\/25\/2013", "title": "Knockout.js", "lector": ["khrystsina_tsikhanovich", "nadzeya_shedava"], @@ -864,8 +838,7 @@ - { - "_id": "27", + "27": { "date": "4\/30\/2013", "title": "Hackaton", "lector": ["siarhei_mikhailau"], @@ -893,8 +866,7 @@ }, - { - "_id": "28", + "28": { "date": "5\/2\/2013", "title": "Semantic", "lector": ["siarhei_mikhailau", "aliaksei_patsiomkin"], @@ -931,8 +903,7 @@ }, - { - "_id": "29", + "29": { "date": "5\/22\/2013", "title": "MVC Introduction", "lector": ["dzianis_ziankovich"], @@ -962,8 +933,7 @@ ] }, - { - "_id": "30", + "30": { "date": "5\/23\/2013", "title": "Dev Process", "lector": ["siarhei_mikhailau"], @@ -992,8 +962,7 @@ }, - { - "_id": "31", + "31": { "date": "5\/29\/2013", "title": "CoffeeScript - Easy Access to the Good Parts of JavaScript", "lector": ["ivan_straltsou"], @@ -1030,8 +999,7 @@ ] }, - { - "_id": "32", + "34": { "date": "6\/20\/2013", "title": "From Non-Intuitive JS Parts to JS OOP", "lector": ["raman_vysotski"], @@ -1068,8 +1036,7 @@ ] }, - { - "_id": "32", + "32": { "date": "6\/20\/2013", "title": "MongoDB + NodeJS", "lector": ["dzianis_kryvashei"], @@ -1096,8 +1063,7 @@ }, - { - "_id": "33", + "33": { "date": "7\/24\/2013", "title": "NodeJS, npm, express", "lector": ["andrei_volkau1"], @@ -1136,7 +1102,7 @@ "express" ] } - ], + }, "users": { "siarhei_mikhailau": { @@ -1594,6 +1560,7 @@ "email": ["katsiaryna_utlik@epam.com"], "photo_url": "photo-placeholder.png" } + }, "tags": [ diff --git a/scripts/app.js b/scripts/app.js index 4c4cafd..3209dd3 100644 --- a/scripts/app.js +++ b/scripts/app.js @@ -42,13 +42,13 @@ $rootScope.global = { isAuthN: authService.isAuthN(), authService: authService, - users: {} + data: {} }; $scope.auth = {}; - dataProvider.getUser().then(function(data) { - $rootScope.global.users = data; + dataProvider.getAll().then(function(data) { + $rootScope.global.data = data.data; }); $scope.signin = function() { diff --git a/scripts/controllers/calendar-controller.js b/scripts/controllers/calendar-controller.js index 7671b88..26413dd 100644 --- a/scripts/controllers/calendar-controller.js +++ b/scripts/controllers/calendar-controller.js @@ -23,12 +23,7 @@ }; $scope.getUserDescription = function(id) { - var user; - dataProvider.getUser().then(function(data) { - user = data[id]; - }); - - return user; + return $scope.global.data.users[id]; }; _update(); @@ -48,29 +43,31 @@ } function _initPeriod(year, month) { + var i = 1; + + while(i <= daysNumber) { + periodDescription.dates.push({ + date: i, + data: [] + }); + i += 1; + } - dataProvider - .getSchedule(year, month) - .success(function(data) { - var i = 1; - console.log(data); - while(i <= daysNumber) { - var techtalkData = data[i], - dayDescription = { - date: i - }; + for (var key in $scope.global.data.talks) { + var talk = $scope.global.data.talks[key]; + var talkDate = new Date(talk.date), + date = talkDate.getDate(); - techtalkData && (dayDescription.data = techtalkData); - periodDescription.dates.push(dayDescription); - i += 1; - } + if(talkDate.getFullYear() == year && talkDate.getMonth() == month) { + periodDescription.dates[date].data.push({ + _id: key, + description: talk + }); + } + } - //FETCH - $scope.schedule = periodDescription; - }) - .error(function() { - console.log('ERROR: ', arguments); - }); + //FETCH + $scope.schedule = periodDescription; } }]); diff --git a/scripts/controllers/details-controller.js b/scripts/controllers/details-controller.js index 4b0b961..24f9f37 100644 --- a/scripts/controllers/details-controller.js +++ b/scripts/controllers/details-controller.js @@ -7,16 +7,10 @@ var currentTalkId = $routeParams.talkId; $scope.global.pageTitle = 'talk: ' + currentTalkId; + $scope._id = currentTalkId; if (ng.isDefined(currentTalkId)) { - dataProvider - .getTechtalkData(currentTalkId) - .success(function(data) { - $scope.details = data; - }) - .error(function() { - - }); + $scope.details = $scope.global.data.talks[currentTalkId]; } }]); })(angular); \ No newline at end of file diff --git a/scripts/controllers/edit-controller.js b/scripts/controllers/edit-controller.js index 5a662ef..053b27e 100644 --- a/scripts/controllers/edit-controller.js +++ b/scripts/controllers/edit-controller.js @@ -6,8 +6,14 @@ function($scope, $routeParams, dataProvider) { var currentTalkId = $routeParams.talkId; - console.log(currentTalkId); - editor.init(); + $scope.global.pageTitle = 'edit talk: ' + currentTalkId; + $scope._id = currentTalkId; + + if (ng.isDefined(currentTalkId)) { + $scope.details = $scope.global.data.talks[currentTalkId]; + } + + //editor.init(); //ui.init(); }]); })(angular); \ No newline at end of file diff --git a/scripts/services/data-provider.js b/scripts/services/data-provider.js index 31ae7f0..4b4b75b 100644 --- a/scripts/services/data-provider.js +++ b/scripts/services/data-provider.js @@ -6,38 +6,14 @@ this.$get = ['$http', '$q', function($http, $q) { - var users; - return { - getSchedule: function(year, month) { - return $http({ - method: 'GET', - url: 'talks/' + year + '/' + month - }); - }, - getTechtalkData: function(id) { - return $http({ - method: 'GET', - url: '/details/' + id - }); - }, - getUser: function() { - var defer = $q.defer(); - - if (!users) { - $http({ - method: 'GET', - url: '/user' - }) - .success(function(data) { - users = data; - defer.resolve(users); - }); + getAll: function() { + return $http({ + method: 'GET', + url: '/data/all' + }); } - - return defer.promise; - } - }; + }; }]; }); diff --git a/server.js b/server.js index 82361c0..d4cdf66 100644 --- a/server.js +++ b/server.js @@ -3,66 +3,9 @@ require('colors'); var express = require('express'), - app = express(), - fs = require('fs'), - data = JSON.parse(fs.readFileSync('data.json', { - encoding: 'utf-8' - })), - talks = data.talks, - users = data.users, - tags = data.tags; + app = express(); -//helper -var getTalkById = (function() { - var memory = {}; - return function(id) { - var memorized = memory[id], - talk; - - if (memorized) { - return memorized; - } - else { - talks.forEach(function(resource, i) { - if (resource._id === id) { - talk = resource; - memory[id] = resource; - return false; - } - }); - - return talk; - } - }; -})(); - -var getSchedule = (function() { - var memory = {}; - - return function(year, month) { - var dateStack = {}, - periodId = year + '/' + month, - memorized = memory[periodId]; - - if (memorized) { - return memorized; - } - else { - talks.forEach(function(talk, i) { - var talkDate = new Date(talk.date), - date = talkDate.getDate(); - - if(talkDate.getFullYear() == year && talkDate.getMonth() == month) { - var current = dateStack[date] = dateStack[date] || []; - current.push(talk); - } - }); - - return (memory[periodId] = dateStack); - } - }; -})(); //config app @@ -91,23 +34,6 @@ app.get('/views/:templateName', function(req, res) { res.render(req.params.templateName); }); -app.get('/talks/:year/:month', function(req, res) { - var year = req.params.year, - month = req.params.month; - - res.send(getSchedule(year, month) || talks); -}); - -app.get('/details/:talkId', function(req, res) { - var id = req.params.talkId; - - res.send(getTalkById(id) || {}); -}); - -app.get('/user', function(req, res) { - res.send(users); -}); - app.post('/auth', function(req, res) { console.log(req.body.green); var login = req.body.login, @@ -126,6 +52,9 @@ app.post('/auth', function(req, res) { } }) +// REST API +require('./server_rest.js')(app); + //server starts here app.listen(app.get('port')); console.log(('start web-server on port ' + app.get('port')).green); \ No newline at end of file diff --git a/server_rest.js b/server_rest.js new file mode 100644 index 0000000..5ea54b1 --- /dev/null +++ b/server_rest.js @@ -0,0 +1,93 @@ +'use strict'; + +var fs = require('fs'); + +// constant data from file +// could be used for reset +var RAW_DATA = fs.readFileSync('./data.json', {encoding: 'UTF-8'}); + +// parse raw data +function parse() { + return JSON.parse(RAW_DATA); +} + +var data = parse();// parsed data + + + +// GETTERS +function getAll() {return data;} // get all data (talks+users+tags) + +function getTalks() {return data.talks;} // get talks +function getTalk(id) {return data.talks[id];} // get talk + +function getUsers() {return data.users;} // get users +function getUser(id) {return data.users[id];} // get user + +function getTags() {return data.tags;} // get tags + + + +// DELETE + +// delete talk +function deleteTalk(id) { + delete data.talks[id]; + return getTalks(); +} + + + +// UPDATERS +function updateTalk(id, talk) {data.talks[id] = talk;} // update talk +function updateUser(id, user) {data.users[id] = user;} // update user + + + +// SETTERS + +// create new talk +function createTalk(talk) { + var i = 1; + while (data.talks[i.toString()]) i++; + + data.talks[i] = talk; + return getTalks(); +} +// create user +function createUser(user) { + data.users[id] = user; + return getUsers(); +} + + + + + + +module.exports = function(app) { + // get + app.get('/data/all', function(req, res) {res.send(getAll() || {});}); + app.get('/data/reset', function(req, res) {res.send(data = parse() || {});}); + + app.get('/data/talks', function(req, res) {res.send(getTalks() || {});}); + app.get('/data/talk/:id', function(req, res) {res.send(getTalk(req.params.id) || {});}); + + app.get('/data/users', function(req, res) {res.send(getUsers() || {});}); + app.get('/data/user/:id', function(req, res) {res.send(getUser(req.params.id) || {});}); + + app.get('/data/tags', function(req, res) {res.send(getTags() || {});}); + + + // post + /*could be buggy*/app.post('/data/talk/', function(req, res) {createTalk(req.body); res.send('ok');}); + /*could be buggy*/app.post('/data/user/', function(req, res) {createUser(req.body); res.send('ok');}); + + // put + /*could be buggy*/app.put('/data/talk/:id', function(req, res) {updateTalk(req.params.id, req.body); res.send('ok');}); + /*could be buggy*/app.put('/data/user/:id', function(req, res) {updateUser(req.params.id, req.body); res.send('ok');}); + + // delete + /*could be buggy*/app.delete('/data/talk/:id', function(req, res) {deleteTalk(req.params.id); res.send('ok');}); + +} diff --git a/views/calendar-page.html b/views/calendar-page.html index 886c51f..4ec9c9d 100644 --- a/views/calendar-page.html +++ b/views/calendar-page.html @@ -8,12 +8,12 @@

  • -
    +
    Location:
    -
    +
    Author:
    -
    +
  • diff --git a/views/details-page.html b/views/details-page.html index d7ffca2..f167537 100644 --- a/views/details-page.html +++ b/views/details-page.html @@ -1,10 +1,13 @@ -Edit + +Edit +

    < Back to Calendar

    -

    -

    +

    ,

    +

    ,

    \ No newline at end of file diff --git a/views/edit-page.html b/views/edit-page.html index c1ae820..b370cb5 100644 --- a/views/edit-page.html +++ b/views/edit-page.html @@ -1,10 +1,17 @@
    -
    - This is ZenPen +
    + +

    -
    \ No newline at end of file diff --git a/views/index.html b/views/index.html index db70776..ae0e835 100644 --- a/views/index.html +++ b/views/index.html @@ -15,6 +15,7 @@