source: KernelRecordsMVC.Web/Controllers/OrderController.cs@ 08aefc6

main
Last change on this file since 08aefc6 was 08aefc6, checked in by mmilevski <markomilevski3@…>, 4 weeks ago

Initial commit

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[08aefc6]1using Microsoft.AspNetCore.Http;
2
3namespace KernelRecordsMVC.Controllers;
4
5using Microsoft.AspNetCore.Mvc;
6using Microsoft.EntityFrameworkCore;
7using KernelRecordsMVC.Data;
8using KernelRecordsMVC.Models;
9using KernelRecordsMVC.Domain.Enums;
10using KernelRecordsMVC.Application.ViewModels;
11
12public class OrderController : Controller
13{
14 private readonly KernelRecordsContext _context;
15
16 public OrderController(KernelRecordsContext context)
17 {
18 _context = context;
19 }
20
21
22 // ==========================================
23 // ADD PRODUCT TO ORDER
24 // UC007
25 // ==========================================
26
27 [HttpGet]
28 public IActionResult AddProduct(long id)
29 {
30 var userId = GetUserId();
31
32 if (userId == null)
33 return RedirectToAction(
34 "Login",
35 "Account");
36
37 var product = _context.Products
38 .Include(p => p.Release)
39 .FirstOrDefault(p => p.ProductId == id);
40
41 if (product == null)
42 return NotFound();
43
44 if (product.Stock <= 0)
45 {
46 TempData["Error"] =
47 "This product is currently out of stock.";
48
49 return RedirectToAction(
50 "Details",
51 "Release",
52 new { id = product.ReleaseId });
53 }
54
55
56 // Find an existing pending order.
57 var order = _context.Orders
58 .Include(o => o.OrderProducts)
59 .FirstOrDefault(o =>
60 o.UserId == userId.Value &&
61 o.Status == OrderStatusType.PENDING);
62
63
64 // If there is no pending order, create one.
65 if (order == null)
66 {
67 order = new Order
68 {
69 OrderId = GetNextOrderId(),
70 UserId = userId.Value,
71
72 // The actual payment method will be
73 // selected during checkout.
74 PaymentMethod = PaymentMethodType.CARD,
75
76 PurchaseDate = DateTime.Today,
77
78 PointsEarned = 0,
79
80 PointsUsed = null,
81
82 Status = OrderStatusType.PENDING
83 };
84
85 _context.Orders.Add(order);
86
87 _context.SaveChanges();
88 }
89
90
91 // Check whether product is already in cart.
92 var existingItem = order.OrderProducts
93 .FirstOrDefault(x =>
94 x.ProductId == product.ProductId);
95
96
97 if (existingItem != null)
98 {
99 if (existingItem.Quantity + 1 >
100 product.Stock)
101 {
102 TempData["Error"] =
103 "There is not enough stock available.";
104
105 return RedirectToAction(
106 "Details",
107 "Release",
108 new { id = product.ReleaseId });
109 }
110
111 existingItem.Quantity++;
112 }
113 else
114 {
115 var orderProduct = new OrderProduct
116 {
117 OrderId = order.OrderId,
118
119 ProductId = product.ProductId,
120
121 PriceAtPurchase = product.Price,
122
123 Quantity = 1
124 };
125
126 _context.OrderProducts.Add(orderProduct);
127 }
128
129 _context.SaveChanges();
130
131
132 return RedirectToAction(nameof(Cart));
133 }
134
135
136 // ==========================================
137 // CART
138 // ==========================================
139
140 [HttpGet]
141 public IActionResult Cart()
142 {
143 var userId = GetUserId();
144
145 if (userId == null)
146 {
147 return RedirectToAction(
148 "Login",
149 "Account");
150 }
151
152
153 var order = _context.Orders
154 .Include(o => o.OrderProducts)
155 .ThenInclude(op => op.Product)
156 .ThenInclude(p => p.Release)
157 .FirstOrDefault(o =>
158 o.UserId == userId.Value &&
159 o.Status == OrderStatusType.PENDING);
160
161
162 var viewModel = new CartViewModel();
163
164
165 if (order != null)
166 {
167 viewModel.Items = order.OrderProducts
168 .Select(op => new CartItemViewModel
169 {
170 ProductId = op.ProductId,
171
172 ReleaseId =
173 op.Product.ReleaseId,
174
175 ReleaseTitle =
176 op.Product.Release.Title,
177
178 Format =
179 op.Product.Format.ToString(),
180
181 Price =
182 op.PriceAtPurchase,
183
184 Quantity =
185 op.Quantity,
186
187 Stock =
188 op.Product.Stock
189 })
190 .ToList();
191 }
192
193
194 return View(viewModel);
195 }
196
197
198 // ==========================================
199 // REMOVE PRODUCT
200 // ==========================================
201
202 [HttpPost]
203 [ValidateAntiForgeryToken]
204 public IActionResult RemoveProduct(long id)
205 {
206 var userId = GetUserId();
207
208 if (userId == null)
209 return RedirectToAction(
210 "Login",
211 "Account");
212
213
214 var item = _context.OrderProducts
215 .Include(x => x.Order)
216 .FirstOrDefault(x =>
217 x.ProductId == id &&
218 x.Order.UserId == userId.Value &&
219 x.Order.Status ==
220 OrderStatusType.PENDING);
221
222
223 if (item != null)
224 {
225 _context.OrderProducts.Remove(item);
226 _context.SaveChanges();
227 }
228
229
230 return RedirectToAction(nameof(Cart));
231 }
232
233
234 // ==========================================
235 // UPDATE QUANTITY
236 // ==========================================
237
238 [HttpPost]
239 [ValidateAntiForgeryToken]
240 public IActionResult UpdateQuantity(
241 long id,
242 long quantity)
243 {
244 var userId = GetUserId();
245
246 if (userId == null)
247 return RedirectToAction(
248 "Login",
249 "Account");
250
251
252 if (quantity <= 0)
253 {
254 return RemoveProduct(id);
255 }
256
257
258 var item = _context.OrderProducts
259 .Include(x => x.Order)
260 .Include(x => x.Product)
261 .FirstOrDefault(x =>
262 x.ProductId == id &&
263 x.Order.UserId == userId.Value &&
264 x.Order.Status ==
265 OrderStatusType.PENDING);
266
267
268 if (item == null)
269 return NotFound();
270
271
272 if (quantity > item.Product.Stock)
273 {
274 TempData["Error"] =
275 "The requested quantity exceeds available stock.";
276
277 return RedirectToAction(nameof(Cart));
278 }
279
280
281 item.Quantity = quantity;
282
283 _context.SaveChanges();
284
285
286 return RedirectToAction(nameof(Cart));
287 }
288
289
290 // ==========================================
291 // USER ID
292 // ==========================================
293
294 private long? GetUserId()
295 {
296 var value =
297 HttpContext.Session.GetString("UserId");
298
299 if (long.TryParse(value, out var userId))
300 return userId;
301
302 return null;
303 }
304
305
306 // ==========================================
307 // ORDER ID
308 // ==========================================
309
310 private long GetNextOrderId()
311 {
312 var maxId = _context.Orders
313 .Select(x => (long?)x.OrderId)
314 .Max();
315
316 return (maxId ?? 0) + 1;
317 }
318}
Note: See TracBrowser for help on using the repository browser.