Changeset 1dcdb2c for KernelRecordsMVC.Web/Controllers/OrderController.cs
- Timestamp:
- 09/09/26 03:30:35 (3 weeks ago)
- Branches:
- main
- Children:
- 6886d95
- Parents:
- 595d2e5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
KernelRecordsMVC.Web/Controllers/OrderController.cs
r595d2e5 r1dcdb2c 52 52 53 53 54 // Find an existing pending order.55 var order = _context.Orders56 .Include(o => o.OrderProducts) 57 .FirstOrDefault(o =>58 o.UserId == userId.Value &&59 o.Status == OrderStatusType.PENDING);60 61 62 // If there is no pending order, create one.63 if (order == null)64 {65 order = new Order54 using var transaction = 55 _context.Database.BeginTransaction(); 56 57 try 58 { 59 var order = _context.Orders 60 .Include(o => o.OrderProducts) 61 .FirstOrDefault(o => 62 o.UserId == userId.Value && 63 o.Status == OrderStatusType.PENDING); 64 65 if (order == null) 66 66 { 67 UserId = userId.Value, 68 PaymentMethod = PaymentMethodType.CARD, 69 PurchaseDate = DateTime.Today, 70 PointsEarned = 0, 71 PointsUsed = null, 72 Status = OrderStatusType.PENDING 73 }; 74 75 _context.Orders.Add(order); 67 order = new Order 68 { 69 UserId = userId.Value, 70 PaymentMethod = PaymentMethodType.CARD, 71 PurchaseDate = DateTime.Today, 72 PointsEarned = 0, 73 PointsUsed = null, 74 Status = OrderStatusType.PENDING 75 }; 76 77 _context.Orders.Add(order); 78 79 _context.SaveChanges(); 80 } 81 82 83 var existingItem = order.OrderProducts 84 .FirstOrDefault(x => 85 x.ProductId == product.ProductId); 86 87 88 if (existingItem != null) 89 { 90 if (existingItem.Quantity + 1 > 91 product.Stock) 92 { 93 TempData["Error"] = 94 "There is not enough stock available."; 95 96 transaction.Rollback(); 97 98 return RedirectToAction( 99 "Details", 100 "Release", 101 new { id = product.ReleaseId }); 102 } 103 104 existingItem.Quantity++; 105 } 106 else 107 { 108 var discount = _context.ModificationProducts 109 .Include(mp => mp.Modification) 110 .Where(mp => 111 mp.ProductId == product.ProductId && 112 mp.Modification.TypeOfModification == 113 ModificationType.DISCOUNT && 114 mp.Modification.Discount.HasValue && 115 mp.Modification.Discount.Value > 0) 116 .Select(mp => mp.Modification) 117 .OrderByDescending(m => m.DateModified) 118 .FirstOrDefault(); 119 120 121 var priceAtPurchase = product.Price; 122 123 if (discount != null) 124 { 125 priceAtPurchase = 126 product.Price - 127 ( 128 product.Price * 129 discount.Discount!.Value / 130 100m 131 ); 132 133 priceAtPurchase = 134 Math.Round( 135 priceAtPurchase, 136 2); 137 } 138 139 140 var orderProduct = new OrderProduct 141 { 142 OrderId = order.OrderId, 143 144 ProductId = 145 product.ProductId, 146 147 PriceAtPurchase = 148 priceAtPurchase, 149 150 Quantity = 1 151 }; 152 153 _context.OrderProducts.Add(orderProduct); 154 } 155 76 156 77 157 _context.SaveChanges(); 78 } 79 80 81 // Check whether product is already in cart. 82 var existingItem = order.OrderProducts 83 .FirstOrDefault(x => 84 x.ProductId == product.ProductId); 85 86 87 if (existingItem != null) 88 { 89 if (existingItem.Quantity + 1 > 90 product.Stock) 91 { 92 TempData["Error"] = 93 "There is not enough stock available."; 94 95 return RedirectToAction( 96 "Details", 97 "Release", 98 new { id = product.ReleaseId }); 99 } 100 101 existingItem.Quantity++; 102 } 103 else 104 { 105 var discount = _context.ModificationProducts 106 .Include(mp => mp.Modification) 107 .Where(mp => 108 mp.ProductId == product.ProductId && 109 mp.Modification.TypeOfModification == 110 ModificationType.DISCOUNT && 111 mp.Modification.Discount.HasValue && 112 mp.Modification.Discount.Value > 0) 113 .Select(mp => mp.Modification) 114 .OrderByDescending(m => m.DateModified) 115 .FirstOrDefault(); 116 117 var priceAtPurchase = product.Price; 118 119 if (discount != null) 120 { 121 priceAtPurchase = 122 product.Price - 123 (product.Price * discount.Discount!.Value / 100m); 124 125 priceAtPurchase = Math.Round( 126 priceAtPurchase, 127 2); 128 } 129 130 var orderProduct = new OrderProduct 131 { 132 OrderId = order.OrderId, 133 134 ProductId = product.ProductId, 135 136 PriceAtPurchase = product.Price, 137 138 Quantity = 1 139 }; 140 141 _context.OrderProducts.Add(orderProduct); 142 } 143 144 _context.SaveChanges(); 145 146 147 return RedirectToAction(nameof(Cart)); 158 159 transaction.Commit(); 160 161 162 return RedirectToAction(nameof(Cart)); 163 } 164 catch 165 { 166 transaction.Rollback(); 167 throw; 168 } 148 169 } 149 170 … … 202 223 Stock = 203 224 op.Product.Stock, 204 205 CoverPhoto = op.Product.Release.CoverPhoto 225 226 CoverPhoto = 227 op.Product.Release.CoverPhoto 206 228 }) 207 229 .ToList(); … … 241 263 { 242 264 _context.OrderProducts.Remove(item); 265 243 266 _context.SaveChanges(); 244 267 } … … 311 334 private long? GetUserId() 312 335 { 313 var userId = HttpContext.Session.GetInt32("UserId"); 336 var userId = 337 HttpContext.Session.GetInt32("UserId"); 314 338 315 339 if (userId.HasValue)
Note:
See TracChangeset
for help on using the changeset viewer.
