Skip to content

Commit cb3e62d

Browse files
committed
Presque terminé, manque la validation
1 parent 6e13f54 commit cb3e62d

File tree

9 files changed

+151
-13
lines changed

9 files changed

+151
-13
lines changed

Zenergy/Zenergy/Controllers/ApiControllers/CartContentsController.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task<IHttpActionResult> PostCartContent(int userId,CartContentModel
124124
return Created("api/users/basket", cartContent);
125125
}
126126

127-
127+
128128
/// <summary>
129129
/// Validate the basket, clear it and create a purchase.
130130
/// </summary>
@@ -153,14 +153,15 @@ public async Task<IHttpActionResult> ValidateBasket(int userId, CartContent cart
153153
return BadRequest("Your cart is empty!");
154154
}
155155
var purchaseContents = new List<purchaseContent>();
156-
156+
157157
foreach (CartContent item in basket)
158158
{
159159
var purchaseContent = new purchaseContent();
160160
purchaseContent.productId = item.productId;
161161
purchaseContent.product = item.product;
162162
purchaseContent.productQuantity = item.productQuantity;
163163
purchaseContents.Add(purchaseContent);
164+
}
164165

165166
var purchase = db.purchase.Add(new purchase() { userId = cartContent.userId, purchaseDate = DateTime.Today, user = cartContent.user, purchaseContent = purchaseContents });
166167

Zenergy/Zenergy/Pages/cart.html

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<div class="container ">
2+
<div class="row">
3+
<div class="col-md-12">
4+
<h2>Shopping cart</h2>
5+
<table class="table table-striped">
6+
<thead>
7+
<tr>
8+
<th>
9+
Name
10+
</th>
11+
<th>
12+
Description
13+
</th>
14+
<th>
15+
Price
16+
</th>
17+
<th>
18+
Reduction (only for members)
19+
</th>
20+
<th>
21+
Quantity
22+
</th>
23+
</tr>
24+
</thead>
25+
<tbody>
26+
<tr ng-repeat="c in cartContents" id="tr{{c.productId}}">
27+
<td>{{c.product.productName}}</td>
28+
<td>{{c.product.productDescr}}</td>
29+
<td>{{c.product.productPrice}} €</td>
30+
<td>{{c.product.memberReduction}} %</td>
31+
<td>
32+
<button type="button" class="btn btn-danger btn-xs" ng-click="setQuantity(c,-1)"><span class="glyphicon glyphicon glyphicon-minus" aria-hidden="true"></span></button>
33+
{{c.productQuantity}}
34+
<button type="button" class="btn btn-success btn-xs" ng-click="setQuantity(c,1)"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span></button>
35+
</td>
36+
<td width="10%"><button type="button" class="btn btn-warning btn-block" ng-click="deleteCC(c)"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span></button></td>
37+
</tr>
38+
39+
</tbody>
40+
</table>
41+
<div class="col-md-2 col-md-offset-9">
42+
<h3>Total : {{getTotal()}} €</h3>
43+
<input class="btn btn-primary btn-lg" value="Proceed" ng-click="proceed()" />
44+
</div>
45+
46+
</div>
47+
</div>
48+
</div>

Zenergy/Zenergy/Pages/products.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ <h3 class="modal-title">Edit product n° {{productToUpdate.productId}} ?</h3>
112112
<td>{{p.productName}}</td>
113113
<td>{{p.productDescr}}</td>
114114
<td>{{p.productPrice}} €</td>
115-
<td>{{p.memberReduction}} </td>
115+
<td>{{p.memberReduction}} %</td>
116116
<td>{{p.availableQty}}</td>
117117
<td width="100px"><input class="btn btn-primary btn-sm" value="Edit" ng-click="update(p)" /></td>
118118
<td width="100px"><input class="btn btn-danger btn-sm" value="Delete" ng-click="delete(p)" /></td>

Zenergy/Zenergy/Pages/shop.html

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
<!-- Collect the nav links, forms, and other content for toggling -->
44
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-8">
55
<ul class="nav navbar-nav">
6-
<li><a href="#Shop" ng-click="test()">All</a></li>
7-
<li ng-repeat="c in categories"><a href="#Shop" ng-click="test()">{{c.categoryName}}</a></li>
6+
<li><a href="#Shop" ng-click="changeFilter('')">All</a></li>
7+
<li ng-repeat="c in categories"><a href="#Shop" ng-click="changeFilter(c.categoryId)">{{c.categoryName}}</a></li>
88
</ul>
99
</div>
1010
</nav>
1111
<div class="container ">
1212
<div class="row">
13-
14-
<div ng-repeat="p in products" class="col-md-4">
13+
<div ng-repeat="p in products | filter:filter" class="col-md-4">
1514
<div class="panel panel-primary">
1615
<div class="panel-heading">
1716
<h3 class="panel-title">{{p.productName}} {{p.productPrice}}€ ( -{{p.memberReduction}}% for the members)</h3>
@@ -22,7 +21,7 @@ <h3 class="panel-title">{{p.productName}} {{p.productPrice}}€ ( -{{p.memberRed
2221
<br />
2322
</div>
2423
<div class="col-md-3">
25-
<button type="button" class="btn btn-success btn-xs" ng-click="">Add to cart</button>
24+
<button type="button" class="btn btn-success btn-xs" ng-click="addProduct(p)">Add to cart</button>
2625
</div>
2726
</div>
2827
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}]);

Zenergy/Zenergy/Scripts/Controllers/productsPageController.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212

1313
var Product = $resource('api/products/:productId', { productId: '@id' }, {
14-
});
14+
update: {
15+
method: 'PUT' // this method issues a PUT request
16+
}
17+
});
1518

1619
$scope.products = Product.query(function () {
1720
});

Zenergy/Zenergy/Scripts/Controllers/shopPageController.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
zenergyApp.controller("shopPageController", ["$scope", "$resource", "$uibModal", "$location", function ($scope, $resource, $uibModal, $location) {
1+
zenergyApp.controller("shopPageController", ["$scope", "$resource", "$uibModal", "$location","tokenService","$http", function ($scope, $resource, $uibModal, $location, tokenService, $http) {
22

33
if ($scope.isAuthanticated()) {
44

@@ -9,14 +9,32 @@
99
});
1010

1111
var Product = $resource('api/products/:productId', { productId: '@id' }, {
12-
update: {
13-
method: 'PUT' // this method issues a PUT request
14-
}
1512
});
1613

1714
$scope.products = Product.query(function () {
1815
});
1916

17+
var CartContents = $resource('api/users/:userId/basket', { userId: '@id' });
18+
19+
$scope.filter = { category: { categoryId: '' } };
20+
$scope.changeFilter = function(c)
21+
{
22+
$scope.filter.category.categoryId = c;
23+
}
24+
25+
/*$scope.cartContents = CartContents.query({userId : tokenService.getUserId()}, function () {
26+
console.log($scope.cartContents);
27+
});*/
28+
29+
$scope.addProduct = function (p) {
30+
var newCC = new CartContents();
31+
32+
newCC.userId = tokenService.getUserId();
33+
newCC.productId = p.productId;
34+
newCC.productQuantity = 1;
35+
newCC.$save({ userId: tokenService.getUserId() });
36+
}
37+
2038
}
2139
else
2240
$location.path("/")
126 Bytes
Binary file not shown.

Zenergy/Zenergy/Zenergy.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,14 @@
318318
<Content Include="fonts\glyphicons-halflings-regular.svg" />
319319
<Content Include="Global.asax" />
320320
<Content Include="Pages\accountManagement.html" />
321+
<Content Include="Pages\cart.html" />
321322
<Content Include="Pages\products.html" />
322323
<Content Include="Pages\profile.html" />
323324
<Content Include="Pages\rooms.html" />
324325
<Content Include="Pages\shop.html" />
325326
<Content Include="Pages\users.html" />
326327
<Content Include="Scripts\Controllers\accountManagementPageController.js" />
328+
<Content Include="Scripts\Controllers\cartPageController.js" />
327329
<Content Include="Scripts\Controllers\homePageController.js" />
328330
<Content Include="Scripts\Controllers\loginPageController.js" />
329331
<Content Include="Scripts\Controllers\mainController.js" />

0 commit comments

Comments
 (0)