|
| 1 | +zenergyApp.controller("cartPageController", ["$scope", "$resource", "$uibModal", "$location", "tokenService", function ($scope, $resource, $uibModal, $location, tokenService) { |
| 2 | + |
| 3 | + if ($scope.isAuthanticated()) { |
| 4 | + |
| 5 | + |
| 6 | + var CartContents = $resource('api/users/:userId/basket/:productId', { userId: '@id', productId: '@id' }, { |
| 7 | + update: { |
| 8 | + method: 'PUT' // this method issues a PUT request |
| 9 | + }, |
| 10 | + removeFromBasket: { |
| 11 | + method: 'DELETE', isArray: true |
| 12 | + } |
| 13 | + }); |
| 14 | + |
| 15 | + $scope.cartContents = CartContents.query({userId : tokenService.getUserId()}, function () { |
| 16 | + console.log($scope.cartContents); |
| 17 | + }); |
| 18 | + |
| 19 | + $scope.getTotal = function () { |
| 20 | + var total = 0; |
| 21 | + for (var i = 0; i < $scope.cartContents.length; i++) { |
| 22 | + var c = $scope.cartContents[i]; |
| 23 | + if ($scope.isMember()) |
| 24 | + total += ((c.product.productPrice -((c.product.productPrice * c.product.memberReduction) / 100)) * c.productQuantity); |
| 25 | + else |
| 26 | + total += (c.product.productPrice * c.productQuantity); |
| 27 | + } |
| 28 | + return total; |
| 29 | + } |
| 30 | + |
| 31 | + $scope.deleteCC = function (c) { |
| 32 | + CartContents.removeFromBasket({ userId: c.userId, productId: c.productId }); |
| 33 | + $('#tr' + c.productId).fadeOut('slow', function () { |
| 34 | + var index = $scope.cartContents.indexOf(c); |
| 35 | + $scope.cartContents.splice(index, 1); |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + $scope.setQuantity = function(c,i) |
| 40 | + { |
| 41 | + c.productQuantity = c.productQuantity + i; |
| 42 | + c.$update({ userId: c.userId }, |
| 43 | + function (response) { |
| 44 | + }, |
| 45 | + function (response) { |
| 46 | + c.productQuantity = c.productQuantity - i; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + $scope.proceed = function () { |
| 51 | + |
| 52 | + /*var BasketValidation = $resource('api/users/1/basket/validate', { userId: '@id' }, { |
| 53 | + update: { |
| 54 | + method: 'PUT' // this method issues a PUT request |
| 55 | + } |
| 56 | + }); |
| 57 | +
|
| 58 | + BasketValidation.update({});*/ |
| 59 | + |
| 60 | + }; |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + } |
| 65 | + else |
| 66 | + $location.path("/") |
| 67 | + }]); |
0 commit comments