diff --git a/bower.json b/bower.json index 9ebfc71..eb6d9ed 100644 --- a/bower.json +++ b/bower.json @@ -12,7 +12,8 @@ "angular-bootstrap": "~0.14.0", "font-awesome": ">=4.1.0", "lodash": "~2.4.1", - "angular-ui-router": "~0.2.15" + "angular-ui-router": "~0.2.15", + "angular-ui-date": "~0.0.8" }, "devDependencies": { "angular-mocks": ">=1.2.*", diff --git a/client/app/components/movies-connector/moviesConnector.factory.js b/client/app/components/movies-connector/moviesConnector.factory.js index 0d2186b..d0d8586 100644 --- a/client/app/components/movies-connector/moviesConnector.factory.js +++ b/client/app/components/movies-connector/moviesConnector.factory.js @@ -16,6 +16,9 @@ $q, $http ) { + + var cachedMovies = []; + var service = { cachedConfiguration: null, topRatedMovies: topRatedMovies, @@ -27,16 +30,54 @@ return $http.get('/api/movies/'); } - function search(query) { - return $http.get('/api/movies/search/' + query); + function search(query) { + + var index = queryIsCached(query); + + if(index == -1){ + var httpPromise = $http.get('/api/movies/search/' + query); + httpPromise + .then(function (response) { + addResponseToCache(query,response); + }); + + return httpPromise; + } + else{ + return $q.when(cachedMovies[index].value); + } } + + function addResponseToCache(query, response){ + + if(cachedMovies.length > 100){ + cachedMovies.splice(0,cachedMovies.length); + } + + cachedMovies.push({key:query, value: response}); + } + + function queryIsCached(query) + { + var index = -1; + + for(var i = 0, len = cachedMovies.length; i < len; i++){ + + if( cachedMovies[i].key == query){ + index = i; + break; + } + } + + return index; + } function configuration() { if (service.cachedConfiguration) { return $q.when(service.cachedConfiguration); } - return $http.get('/api/movies/configuration').then(function(response) { - service.cachedConfiguration = response; + return $http.get('/api/movies/configuration').then(function sucessCallback(response) { + service.cachedConfiguration = response; return response; }); } diff --git a/client/app/pages/search/search-page.controller.js b/client/app/pages/search/search-page.controller.js index a54aa54..13ca8a0 100644 --- a/client/app/pages/search/search-page.controller.js +++ b/client/app/pages/search/search-page.controller.js @@ -25,7 +25,8 @@ function updatedSearch(query) { - moviesConnector.search(query).then(function (response) { + var searchPromise = moviesConnector.search(query); + searchPromise.then(function (response) { vm.movies = response.data.results; angular.forEach(vm.movies, function (each) { if (vm.topMovieNames.indexOf(each) === -1){ diff --git a/client/demo/expenses/expenses-controller.js b/client/demo/expenses/expenses-controller.js new file mode 100644 index 0000000..5bec622 --- /dev/null +++ b/client/demo/expenses/expenses-controller.js @@ -0,0 +1,115 @@ +(function() { + 'use strict'; + + // Create module and controller + angular + .module('expensesApp',['ui.date']) + .controller('expensesController', expensesController); + + + expensesController.$inject = [ + ]; + + function expensesController() { + + // Controller as viewModel + var vm = this; + + // Initialization + vm.types = ["food","transportation","lodging","financial","other"]; + vm.list = []; + vm.listTotal = []; + + vm.currentAmount = 0; + + + // Controller methods + vm.add = add; + vm.remove = remove; + vm.edit = edit; + vm.save = save; + vm.cancel = cancel; + + /* Adds an item to the todo list */ + function add(descTxt,dateTxt,typeDdl,amountTxt) { + vm.list.push({desc:descTxt,date:dateTxt, typeExpense:typeDdl, amount:amountTxt, editing:false}); + calculateTotals(); + cleanForm(); + } + + /* Remove and item to the expenses list */ + function remove(indexList) { + vm.list.splice(indexList,1); + calculateTotals(); + } + + /* Save changes when we edit a item*/ + function save(indexList){ + vm.list[indexList].editing = false; + calculateTotals(); + } + + + /* Edit a item from expenses list */ + function edit(indexList) { + vm.currentAmount = vm.list[indexList].amount; + + disableAllEditingItem(); + vm.list[indexList].editing = true; + } + + function cancel(indexList) { + vm.list[indexList].editing = false; + vm.list[indexList].amount = vm.currentAmount; + calculateTotals(); + } + + function disableAllEditingItem(){ + for(var i = 0, len = vm.list.length; i < len; i++){ + vm.list[i].editing = false; + } + } + + /* Clean form test */ + function cleanForm() + { + vm.descTxt = ''; + vm.amountTxt = ''; + vm.dateTxt = ''; + } + + + function calculateTotals() + { + vm.listTotal.splice(0,vm.listTotal.length); + + for(var i = 0, len = vm.list.length; i < len; i++){ + + var type = vm.list[i].typeExpense; + + var indextTotal = getIndexFromTotalListByType(type) + + if(indextTotal == -1){ + vm.listTotal.push({type:vm.list[i].typeExpense, total:vm.list[i].amount}); + } + else{ + vm.listTotal[indextTotal].total = parseInt(vm.listTotal[indextTotal].total) + parseInt(vm.list[i].amount); + } + } + } + + /* Get the index from total list */ + function getIndexFromTotalListByType(searchTerm) + { + var index = -1; + + for(var i = 0, len = vm.listTotal.length; i < len; i++) { + if (vm.listTotal[i].type === searchTerm) { + index = i; + break; + } + } + return index; + } + } +})(); diff --git a/client/demo/expenses/index.html b/client/demo/expenses/index.html new file mode 100644 index 0000000..edd7987 --- /dev/null +++ b/client/demo/expenses/index.html @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + +
+

Add Expense:

+
+ + + + + + + + + + + + + +
+ Desc + + Date + + Amount + + Type + +
+ + + + + + + + + +
+
+
+

Items (#{{expVM.list.length}}):

+ + + + + + + + +
+ {{ item.desc }} + + {{ item.date | date:'shortDate' }} + + {{ item.typeExpense }} + + {{ item.amount | currency }} + + +
+ + +
+
+ + +
+
+
+
+

Totals:

+ + + + +
+ {{item.type}} : {{item.total | currency}} +
+
+
+ + \ No newline at end of file diff --git a/client/demo/expenses/style.css b/client/demo/expenses/style.css new file mode 100644 index 0000000..5daced8 --- /dev/null +++ b/client/demo/expenses/style.css @@ -0,0 +1,15 @@ +.transp { + background-color: green; +} +.food { + background-color: gray; +} +.lodging { + background-color: red; +} +.financial { + background-color: blue; +} +.other { + background-color: yellow; +} \ No newline at end of file diff --git a/client/index.html b/client/index.html index 687cba4..1471519 100644 --- a/client/index.html +++ b/client/index.html @@ -53,12 +53,15 @@ + + +