| | 5 | ==== Registering a new consumer |
| | 6 | |
| | 7 | {{{ |
| | 8 | [HttpPost] |
| | 9 | [ValidateAntiForgeryToken] |
| | 10 | public IActionResult Register(RegisterViewModel model) |
| | 11 | { |
| | 12 | if (!ModelState.IsValid) |
| | 13 | return View(model); |
| | 14 | |
| | 15 | if (_context.Users.Any(x => x.Username == model.Username)) |
| | 16 | { |
| | 17 | ModelState.AddModelError( |
| | 18 | "Username", |
| | 19 | "Username is already taken."); |
| | 20 | |
| | 21 | return View(model); |
| | 22 | } |
| | 23 | |
| | 24 | if (_context.Users.Any(x => x.Email == model.Email)) |
| | 25 | { |
| | 26 | ModelState.AddModelError( |
| | 27 | "Email", |
| | 28 | "Email is already registered."); |
| | 29 | |
| | 30 | return View(model); |
| | 31 | } |
| | 32 | |
| | 33 | using var transaction = |
| | 34 | _context.Database.BeginTransaction(); |
| | 35 | |
| | 36 | try |
| | 37 | { |
| | 38 | var user = new User |
| | 39 | { |
| | 40 | Email = model.Email, |
| | 41 | Username = model.Username, |
| | 42 | Password = model.Password, |
| | 43 | DateCreated = DateTime.Today, |
| | 44 | ShippingAddress = model.ShippingAddress, |
| | 45 | TelephoneNumber = model.TelephoneNumber |
| | 46 | }; |
| | 47 | |
| | 48 | _context.Users.Add(user); |
| | 49 | _context.SaveChanges(); |
| | 50 | |
| | 51 | var consumer = new Consumer |
| | 52 | { |
| | 53 | UserId = user.UserId, |
| | 54 | PointsCollected = 0 |
| | 55 | }; |
| | 56 | |
| | 57 | _context.Consumers.Add(consumer); |
| | 58 | _context.SaveChanges(); |
| | 59 | |
| | 60 | transaction.Commit(); |
| | 61 | |
| | 62 | return RedirectToAction(nameof(Login)); |
| | 63 | } |
| | 64 | catch |
| | 65 | { |
| | 66 | transaction.Rollback(); |
| | 67 | throw; |
| | 68 | } |
| | 69 | } |
| | 70 | }}} |
| | 71 | |
| | 72 | ==== Adding a product to order |
| | 73 | |
| | 74 | {{{ |
| | 75 | [HttpGet] |
| | 76 | public IActionResult AddProduct(long id) |
| | 77 | { |
| | 78 | var userId = GetUserId(); |
| | 79 | |
| | 80 | if (userId == null) |
| | 81 | return RedirectToAction("Login", "Account"); |
| | 82 | |
| | 83 | var product = _context.Products |
| | 84 | .Include(p => p.Release) |
| | 85 | .FirstOrDefault(p => p.ProductId == id); |
| | 86 | |
| | 87 | if (product == null) |
| | 88 | return NotFound(); |
| | 89 | |
| | 90 | if (product.Stock <= 0) |
| | 91 | { |
| | 92 | TempData["Error"] = |
| | 93 | "This product is currently out of stock."; |
| | 94 | |
| | 95 | return RedirectToAction( |
| | 96 | "Details", |
| | 97 | "Release", |
| | 98 | new { id = product.ReleaseId }); |
| | 99 | } |
| | 100 | |
| | 101 | using var transaction = |
| | 102 | _context.Database.BeginTransaction(); |
| | 103 | |
| | 104 | try |
| | 105 | { |
| | 106 | var order = _context.Orders |
| | 107 | .Include(o => o.OrderProducts) |
| | 108 | .FirstOrDefault(o => |
| | 109 | o.UserId == userId.Value && |
| | 110 | o.Status == OrderStatusType.PENDING); |
| | 111 | |
| | 112 | if (order == null) |
| | 113 | { |
| | 114 | order = new Order |
| | 115 | { |
| | 116 | UserId = userId.Value, |
| | 117 | PaymentMethod = PaymentMethodType.CARD, |
| | 118 | PurchaseDate = DateTime.Today, |
| | 119 | PointsEarned = 0, |
| | 120 | PointsUsed = null, |
| | 121 | Status = OrderStatusType.PENDING |
| | 122 | }; |
| | 123 | |
| | 124 | _context.Orders.Add(order); |
| | 125 | _context.SaveChanges(); |
| | 126 | } |
| | 127 | |
| | 128 | var existingItem = order.OrderProducts |
| | 129 | .FirstOrDefault(x => |
| | 130 | x.ProductId == product.ProductId); |
| | 131 | |
| | 132 | if (existingItem != null) |
| | 133 | { |
| | 134 | existingItem.Quantity++; |
| | 135 | } |
| | 136 | else |
| | 137 | { |
| | 138 | var orderProduct = new OrderProduct |
| | 139 | { |
| | 140 | OrderId = order.OrderId, |
| | 141 | ProductId = product.ProductId, |
| | 142 | PriceAtPurchase = product.Price, |
| | 143 | Quantity = 1 |
| | 144 | }; |
| | 145 | |
| | 146 | _context.OrderProducts.Add(orderProduct); |
| | 147 | } |
| | 148 | |
| | 149 | _context.SaveChanges(); |
| | 150 | |
| | 151 | transaction.Commit(); |
| | 152 | |
| | 153 | return RedirectToAction(nameof(Cart)); |
| | 154 | } |
| | 155 | catch |
| | 156 | { |
| | 157 | transaction.Rollback(); |
| | 158 | throw; |
| | 159 | } |
| | 160 | } |
| | 161 | }}} |