Skip to content

Instantly share code, notes, and snippets.

@joncodo
Created September 30, 2014 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncodo/ec2dac875997214d4fea to your computer and use it in GitHub Desktop.
Save joncodo/ec2dac875997214d4fea to your computer and use it in GitHub Desktop.
myApp.service('DealRetrievalService', ['$http', function($http) {
var _this = this;
return {
removeInactiveDeals: function (deals) {
var newDeals = [];
_.forEach(deals, function (deal) {
if(deal.status === 'Active'){
newDeals.push(deal);
}
});
return newDeals;
},
removeExpiredDeals: function (deals) {
var newDeals = [];
_.forEach(deals.data, function (deal) {
var validTo = Date.parse(deal.valid_to);
var today = new Date();
if(validTo > today){
newDeals.push(deal);
}
});
return _this.removeInactiveDeals(newDeals);
},
getAllDeals: function() {
return $http.get('/dealRecord')
.success(function(deals) {
var temp = _this.removeExpiredDeals(deals.data);
console.log('deals were:', temp);
return _this.removeExpiredDeals(deals.data);
})
.error(function(err) {
});
},
getRelatedDeals: function (category, retailer) {
var q = '/dealRecord?status=Active';
if(category && category !== 'All'){
q += '&category=' + category;
}
if(retailer && retailer !== 'All'){
q += '&retailer=' + retailer;
}
return $http.get(q)
.success(function(deals) {
return _this.removeExpiredDeals(deals.data);
})
.error(function(err) {
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment