Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Juan.cook/assignment3 #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
9 changes: 6 additions & 3 deletions client/app/components/movies-connector/connector.module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

angular.module('movies.connector', [

]);
angular
.module('movies.connector', [])
.constant('cache_calls', {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using snake_case prefer camelCase instead

SEARCH_MOVIES: 100,
INFO_MOVIES: 100
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(function() {
'use strict';
angular
.module('movies.connector')
.factory('moviesConnectorCache', moviesConnectorCacheFactory);

/**
* @ngInject
*/
moviesConnectorCacheFactory.$inject = [
'cache_calls'
];

function moviesConnectorCacheFactory(
cache_calls
) {
var service = {
catchedInfo: [],
catchedSearch : [],
getCacheInfo: getCacheInfo,
addCacheInfo: addCacheInfo,
getCacheSearch: getCacheSearch,
addCacheSearch: addCacheSearch
};

function getCacheInfo(movieId) {
var result = {};
result.hasCache = false;
result.data = {};
angular.forEach(service.catchedInfo, function(obj){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use an object with the movie id as keys, and then access directly without iteration

if (movieId == obj.movieId){
result.hasCache = true;
result.data = obj;
console.log('cache INFO found!');
}
});
return result;
}

function addCacheInfo(movieId, result){
if (service.catchedInfo.length > cache_calls.INFO_MOVIES){
console.log('Cleaning INFO cache');
service.catchedInfo = [];
}
service.catchedInfo.push({ movieId: movieId, result: result});
}

function getCacheSearch(query) {
var result = {};
result.hasCache = false;
result.data = {};
angular.forEach(service.catchedSearch, function(obj){
if (query == obj.query){
result.hasCache = true;
result.data = obj;
console.log('cache MOVIES found!');
}
});
return result;
}

function addCacheSearch(query, result){
if (service.catchedSearch.length > cache_calls.SEARCH_MOVIES){
console.log('Cleaning MOVIES cache');
service.catchedSearch = [];
}
service.catchedSearch.push({ query: query, result: result});
}

return service;
}

})();
61 changes: 56 additions & 5 deletions client/app/components/movies-connector/moviesConnector.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,80 @@
*/
moviesConnectorFactory.$inject = [
'$q',
'$http'
'$http',
'moviesConnectorCache'
];

function moviesConnectorFactory(
$q,
$http
$http,
moviesConnectorCache
) {
var service = {
cachedConfiguration: null,
topRatedMovies: topRatedMovies,
configuration: configuration,
search: search,
movieInfo: movieInfo
movieInfo: movieInfo,
topRatedMoviesCompleteInfo: topRatedMoviesCompleteInfo
};

function topRatedMovies() {
return $http.get('/api/movies/');
}

function topRatedMoviesCompleteInfo(){
topRatedMovies()
.then(function(response){
angular.forEach(response.results, function(obj){
movieInfo(obj.id)
.then(function(response){
console.log(response)
});
});
});


}

function movieInfo(movieId) {
return $http.get('/api/movies/info/' + movieId);
var result = {};
var deferred = $q.defer();

var cacheResult = moviesConnectorCache.getCacheInfo(movieId);
if (cacheResult.hasCache === true){ //the query has been catched:
result.data = cacheResult.data.result;
deferred.resolve(result);
} else { //that query has NOT been catched:
$http.get('/api/movies/info/' + movieId)
.then(function(response){
result.data = response.data;
moviesConnectorCache.addCacheInfo(movieId, result.data);
deferred.resolve(result);
});
}

return deferred.promise;
}

function search(query) {
return $http.get('/api/movies/search/' + query);
var result = {};
var deferred = $q.defer();

var cacheResult = moviesConnectorCache.getCacheSearch(query);
if (cacheResult.hasCache === true){ //the query has been catched:
result.data = cacheResult.data.result;
deferred.resolve(result);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do return $q.when(result)

} else { //that query has NOT been catched:
$http.get('/api/movies/search/' + query)
.then(function(response){
result.data = response.data;
moviesConnectorCache.addCacheSearch(query, result.data);
deferred.resolve(result);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use $q.when above you could return result here

});
}

return deferred.promise;
}

function configuration() {
Expand Down
117 changes: 117 additions & 0 deletions client/demo/ex/assignment1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<html ng-app>
<head>
<script src="/bower_components/jquery/dist/jquery.js"></script>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="/bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.css">
<title>Expense App Assignment1</title>
</head>
<body>
<div class="container"
ng-init="
expenses = [];
types= ['food', 'transportation', 'lodging', 'financial', 'sales', 'other.'];
isEditing = false;
index = -1;">
<h1>Expenses</h1>
<div class="create_expense">
<!-- <form name="expenseForm" ng-init="expense = {};"> -->
<form ng-init="expense = {};">
<h4>Create Expense</h4>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="text" hidden ng-model="expense.id" />
<div class="form-group">
<input type="date" class="form-control" placeholder="Date" ng-model="expense.date" />
</div>
<div class="form-group">
<select id="type" name="type" class="form-control" ng-model="expense.type" ng-options="option for option in types track by option">
<option value="">-- Select type --</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Description" ng-model="expense.description" />
</div>
<div class="form-group">
<input type="number" class="form-control" placeholder="Amount" ng-model="expense.amount" />
</div>
<div>
<input type="button" id="create-button" value="Create" class="btn btn-primary"
ng-click="expense.id = (expenses.length)+1;
expenses.push(expense);
expense = {};"
ng-hide="isEditing">
<input type="button" id="update-button" value="Update" class="btn btn-primary"
ng-click="expenses[index] = expense;;
expense = {};
isEditing = false;"
ng-show="isEditing">
<input type="button" id="cancel-button" value="Cancel" class="btn btn-primary"
ng-click="expense = {};
isEditing = false;"
ng-show="isEditing">
</div>
</div>
</div>
</form>
</div>
<div class="display_expenses">
<table class="table">
<thead>
<th>Date</th>
<th>Type</th>
<th>Description</th>
<th>Amount</th>
</thead>
<tbody>
<tr ng-repeat="exp in expenses track by exp.id">
<td>{{ exp.date | date: 'dd/MM/yy'}}</td>
<td>{{ exp.type }}</td>
<td>{{ exp.description }}</td>
<td>{{ exp.amount }}</td>
<td>
<button class="btn btn-default"
ng-click="$parent.expense = exp;
$parent.isEditing = true;
$parent.index = expenses.indexOf(exp);">
<i class="glyphicon glyphicon-pencil"></i>
</button>
<button class="btn btn-default"
ng-click="expenses.splice(expenses.indexOf(exp),1)">
<i class="glyphicon glyphicon-trash"></i>
</button>
</td>
</tr>
</tbody>
</table>
<hr>
<div class="row">
<h3>Totals by Type</h3>
<div class="col-sm-6 col-sm-offset-3">
<table class="table" id="totals_table">
<thead>
<th>Type</th>
<th>Total</th>
</thead>
<tbody>
<tr ng-repeat="type in types">
<td>{{type}}</td>
<td ng-init="total=0;"
ng-repeat="exp in expenses | filter: type">{{ exp.amount + total }}
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h3>Total All Types</h3>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<p ng-init="total=0;" ng-repeat="exp in expenses">{{ exp.amount + total}}</p>
</div>
</div>
</div>
</div>
</body>
</html>
Loading