Created
December 16, 2014 21:28
-
-
Save got5/dd643c17d612172389bf to your computer and use it in GitHub Desktop.
Slide 93 TP5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Service which handle all user logic. */ | |
var UserService = function ($http, $q, $log, $cookies, $cookieStore, isDebugMode) { | |
this.logUser = function (login, password) { | |
// We create a promise to offer the possibility to users to call some functions after the | |
// asynchronous call of $http.get. | |
var deferred = $q.defer(); | |
/** Gets all users... */ | |
$http.post('/api/login', {login: login, password: password}) | |
.success(function (user) { | |
isDebugMode && $log.info('Authentication successed !'); | |
$cookieStore.put('user', user); | |
deferred.resolve(user); | |
}) | |
.error(function (reason) { | |
isDebugMode && $log.error('unable to log ' + reason); | |
deferred.reject("Bad login and/or password"); | |
}); | |
// A promise is returned, so we can make: userService.logUser(u, p).then(success, error). | |
return deferred.promise; | |
}; | |
}; | |
/** Registers our service in a new sub module. */ | |
angular.module('sdcoServices').provider('UserService',function() { | |
var isDebugMode = false; | |
this.setDebugMode = function (pIsDebug) { | |
isDebugMode = pIsDebug; | |
}; | |
this.$get = [ '$http', '$q', '$log', '$cookies', '$cookieStore', function ($http, $q, $log, $cookies, $cookieStore, User) { | |
return new UserService($http, $q, $log, $cookies, $cookieStore, User, isDebugMode); | |
}]; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
angular.module('app') | |
.controller('LoginController', ['$scope', '$location', 'UserService', function ($scope, $location, userService) { | |
$scope.errorMsg = null; | |
$scope.logUser = function() { | |
userService.logUser($scope.login, $scope.password) | |
.then(function(currentUser) { | |
$location.path("/"); | |
}, function(reason) { | |
$scope.errorMsg = reason; | |
}); | |
}; | |
}]); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
"use strict"; | |
/** Products utility service */ | |
var ProductUtils = function() { | |
/** Returns rating for a given product. */ | |
var getProductRating = function(comments) { | |
if (comments) { | |
var sumRatings = 0; | |
for ( var index = 0; index < comments.length; index++) { | |
var comment = comments[index]; | |
sumRatings += comment.rate; | |
} | |
return Math.floor(sumRatings / comments.length); | |
} | |
return 0; | |
}; | |
/** Returns the CSS class for the average rating of a given product. */ | |
this.getRatingCss = function(pItem) { | |
var css = ['rating']; | |
if (pItem !== undefined) { | |
switch (getProductRating(pItem)) { | |
case 1: | |
css.push('one'); | |
break; | |
case 2: | |
css.push('two'); | |
break; | |
case 3: | |
css.push('three'); | |
break; | |
case 4: | |
css.push('four'); | |
break; | |
case 5: | |
css.push('five'); | |
break; | |
default : | |
css.push('zero'); | |
} | |
} | |
return css; | |
}; | |
}; | |
angular.module('app') | |
.value('ProductUtils', new ProductUtils()); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
/** Product detail view controller */ | |
angular.module('app') | |
.controller('DetailController', ['$scope', '$location', '$routeParams', 'catalogService', 'ProductUtils', | |
function ($scope, $location, $routeParams, catalogService, productUtils) { | |
$scope.product = {}; | |
catalogService.getProduct($routeParams.id).success(function (result) { | |
$scope.product = result; | |
}); | |
$scope.quantity = 1; | |
$scope.getImage = function (id) { | |
if (!id) { | |
return ""; | |
} else { | |
return "/img/catalog/" + id + ".jpg"; | |
} | |
}; | |
/** Returns the CSS class for the average rating of a given product. */ | |
$scope.getCSSRating = productUtils.getRatingCss; | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment