Skip to content

Commit 5473c88

Browse files
committed
fixing conflicts
2 parents fd92491 + 0af22fd commit 5473c88

15 files changed

+469
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Entity;
5+
using System.Data.Entity.Infrastructure;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Threading.Tasks;
10+
using System.Web.Http;
11+
using System.Web.Http.Description;
12+
using Zenergy.Models;
13+
14+
namespace Zenergy.Controllers.ApiControllers
15+
{
16+
public class CartContentsController : ApiController
17+
{
18+
private ZenergyContext db = new ZenergyContext();
19+
20+
21+
// GET: api/users/{userId}/basket
22+
[HttpGet]
23+
[Route("api/users/{userId}/basket")]
24+
[Authorize]
25+
[ResponseType(typeof(IQueryable<CartContent>))]
26+
public IHttpActionResult GetCartContent(int userId)
27+
{
28+
//verify the identity of the user
29+
var currentUserId = db.user.Where(u => u.mail.Equals(this.User.Identity.Name)).FirstAsync().Result.userId;
30+
if (!(currentUserId == userId))
31+
{
32+
return BadRequest("You are not authorized to access this user's cart content!");
33+
}
34+
IQueryable<CartContent> cartContent = db.CartContent.Where(cc => cc.userId == (userId));
35+
if (!cartContent.Any())
36+
{
37+
return NotFound();
38+
}
39+
40+
41+
return Ok(cartContent);
42+
}
43+
44+
// PUT: api/CartContents/5
45+
[HttpPut]
46+
[Route("api/users/{userId}/basket")]
47+
[Authorize]
48+
[ResponseType(typeof(void))]
49+
public async Task<IHttpActionResult> PutCartContent(int userId, CartContentModel cartContent)
50+
{
51+
if (!ModelState.IsValid)
52+
{
53+
return BadRequest(ModelState);
54+
}
55+
56+
if (userId != cartContent.userId)
57+
{
58+
return BadRequest("You are not authorized to access to this user's basket");
59+
}
60+
61+
if (!CartContentExists(userId, cartContent.productId))
62+
{
63+
return NotFound();
64+
}
65+
if (cartContent.productQuantity < 1) return BadRequest("Please choose a positive product quantity!");
66+
//Verify if the quantity in the cartContent if inferior to the available quantity for the product
67+
int enoughProductInStock = db.product.Where(p => p.productId == cartContent.productId).FirstAsync().Result.availableQty.Value;
68+
if (cartContent.productQuantity > enoughProductInStock)
69+
{
70+
return BadRequest(string.Format("There are only {0} products left!", enoughProductInStock));
71+
}
72+
db.Entry(new CartContent() { userId = cartContent.userId, productId = cartContent.productId, productQuantity = cartContent.productQuantity }).State = EntityState.Modified;
73+
74+
try
75+
{
76+
await db.SaveChangesAsync();
77+
}
78+
catch (DbUpdateConcurrencyException)
79+
{
80+
throw;
81+
82+
}
83+
84+
return StatusCode(HttpStatusCode.NoContent);
85+
}
86+
87+
// POST: api/users/basket
88+
[HttpPost]
89+
[Route("api/users/{userId}/basket")]
90+
[Authorize]
91+
[ResponseType(typeof(CartContentModel))]
92+
public async Task<IHttpActionResult> PostCartContent(int userId,CartContentModel cartContent)
93+
{
94+
if (!ModelState.IsValid)
95+
{
96+
return BadRequest(ModelState);
97+
}
98+
99+
if (userId != cartContent.userId) //verify that we are creating a cartcontent into the rigth user's basket
100+
{
101+
return BadRequest();
102+
}
103+
if (cartContent.productQuantity < 0) return BadRequest("Please choose a positive product quantity!");
104+
if (!CartContentExists(userId, cartContent.productId))
105+
{
106+
//Verify if the quantity in the cartContent if inferior to the available quantity for the product
107+
int enoughProductInStock = db.product.Where(p => p.productId == cartContent.productId).FirstAsync().Result.availableQty.Value;
108+
if (cartContent.productQuantity > enoughProductInStock)
109+
{
110+
return BadRequest(string.Format("There are only {0} products left!", enoughProductInStock));
111+
}
112+
db.CartContent.Add(new CartContent() { userId = cartContent.userId, productId = cartContent.productId, productQuantity = cartContent.productQuantity });
113+
}
114+
else return BadRequest("This product is already in your cart");
115+
try
116+
{
117+
await db.SaveChangesAsync();
118+
}
119+
catch (DbUpdateException)
120+
{
121+
122+
throw;
123+
}
124+
125+
return Created("api/users/basket", cartContent);
126+
}
127+
128+
129+
/// <summary>
130+
/// Validate the basket, clear it and create a purchase.
131+
/// </summary>
132+
/// <param name="userId"></param>
133+
/// <param name="cartContent"></param>
134+
/// <returns></returns>
135+
[HttpPut]
136+
[Route("api/users/{userId}/basket/validate")]
137+
[Authorize]
138+
[ResponseType(typeof(purchase))]
139+
public async Task<IHttpActionResult> ValidateBasket(int userId)
140+
{
141+
if (!ModelState.IsValid)
142+
{
143+
return BadRequest(ModelState);
144+
}
145+
146+
if (!VerifyIdentity(userId)) //verify that we are creating a cartcontent into the rigth user's basket
147+
{
148+
return BadRequest("You are not authorized to access to this user's cart!");
149+
}
150+
151+
var basket = db.CartContent.Where(cc => cc.userId == userId);
152+
if (!basket.Any())
153+
{
154+
return BadRequest("Your cart is empty!");
155+
}
156+
var purchaseContents = new List<purchaseContent>();
157+
158+
foreach (CartContent item in basket)
159+
{
160+
var purchaseContent = new purchaseContent();
161+
purchaseContent.productId = item.productId;
162+
purchaseContent.product = item.product;
163+
purchaseContent.productQuantity = item.productQuantity;
164+
purchaseContents.Add(purchaseContent);
165+
}
166+
167+
var purchase = db.purchase.Add(new purchase() { userId = userId, purchaseDate = DateTime.Today, purchaseContent = purchaseContents });
168+
169+
try
170+
{
171+
//Clearing the basket
172+
await ClearBasket(basket.ToListAsync().Result);
173+
}
174+
catch (DbUpdateException)
175+
{
176+
throw;
177+
}
178+
179+
return Created("api/users/{userId}/basket/validate", purchase);
180+
}
181+
182+
// DELETE: api/users/{userId}/basket
183+
[HttpDelete]
184+
[Route("api/users/{userId}/basket/{productId}")]
185+
[Authorize]
186+
[ResponseType(typeof(CartContentModel))]
187+
public async Task<IHttpActionResult> DeleteCartContent(int userId, int productId)
188+
{
189+
if (!VerifyIdentity(userId))
190+
{
191+
return BadRequest("You are not authorized to access to this user's basket");
192+
}
193+
var cartContent = db.CartContent.Where(cc => cc.userId == userId && cc.productId == productId);
194+
if (!cartContent.Any())
195+
{
196+
return NotFound();
197+
}
198+
199+
if (userId != cartContent.FirstAsync().Result.userId)
200+
{
201+
return BadRequest("You are not authorized to access to this user's basket");
202+
}
203+
204+
db.CartContent.Remove(cartContent.FirstAsync().Result);
205+
await db.SaveChangesAsync();
206+
return Ok(cartContent);
207+
}
208+
209+
protected override void Dispose(bool disposing)
210+
{
211+
if (disposing)
212+
{
213+
db.Dispose();
214+
}
215+
base.Dispose(disposing);
216+
}
217+
218+
private bool CartContentExists(int userId, int productId)
219+
{
220+
return db.CartContent.Count(e => e.userId == userId && e.productId == productId) > 0;
221+
}
222+
223+
private async Task<int> ClearBasket(List<CartContent> basket)
224+
{
225+
db.CartContent.RemoveRange(basket);
226+
return await db.SaveChangesAsync();
227+
}
228+
229+
public bool VerifyIdentity(int userId)
230+
{
231+
return db.user.Where(cc => cc.mail.Equals(this.User.Identity.Name)).FirstAsync().Result.userId == userId;
232+
}
233+
}
234+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace Zenergy.Models
7+
{
8+
public class CartContentModel
9+
{
10+
public int userId { get; set; }
11+
public int productId { get; set; }
12+
public int productQuantity { get; set; }
13+
}
14+
}

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

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<nav class="navbar navbar-default navbar-static-top">
2+
3+
<!-- Collect the nav links, forms, and other content for toggling -->
4+
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-8">
5+
<ul class="nav navbar-nav">
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>
8+
</ul>
9+
</div>
10+
</nav>
11+
<div class="container ">
12+
<div class="row">
13+
<div ng-repeat="p in products | filter:filter" class="col-md-4">
14+
<div class="panel panel-primary">
15+
<div class="panel-heading">
16+
<h3 class="panel-title">{{p.productName}} {{p.productPrice}}€ ( -{{p.memberReduction}}% for the members)</h3>
17+
</div>
18+
<div class="panel-body">
19+
<div class="col-md-9">
20+
{{p.productDescr}}
21+
<br />
22+
</div>
23+
<div class="col-md-3">
24+
<button type="button" class="btn btn-success btn-xs" ng-click="addProduct(p)">Add to cart</button>
25+
</div>
26+
</div>
27+
</div>
28+
</div>
29+
30+
</div>
31+
</div>

Zenergy/Zenergy/Providers/ApplicationOAuthProvider.cs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwner
5252
identity.AddClaim(new Claim("role", "user"));
5353
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
5454
identity.AddClaim(new Claim("UserId", user.userId.ToString()));
55+
identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
5556

5657
if (user.member != null)
5758
{

0 commit comments

Comments
 (0)