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
+ }
0 commit comments