public async Task <IActionResult> RegisterAsync([FromBody] RegisterVM model) { if (model == null) { throw new NullReferenceException("Register model is null"); } if (ModelState.IsValid) { var role = _roleManager.FindByIdAsync("4").Result; var user = new Account { UserName = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, Email = model.Email, }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var roleResult = await _userManager.AddToRoleAsync(user, role.Name); if (!roleResult.Succeeded) { throw new NullReferenceException("Role is not good"); } Customer customer = new Customer(); customer.Account = user; _customerService.AddCustomer(customer); var confirmEmailToken = await _userManager.GenerateEmailConfirmationTokenAsync(user); var encodedEmailToken = Encoding.UTF8.GetBytes(confirmEmailToken); var validEmailToken = WebEncoders.Base64UrlEncode(encodedEmailToken); string url = $"https://www.api.customer.app.fit.ba/account/confirmemail?userid={user.Id}&token={validEmailToken}"; await _emailService.SendEmailAsync(user.Email, "Confirm your email", $"<h1>Hi,</h1>" + $"<p>Please confirm your email by <a href='{url}'>Clicking here</a></p>"); if (_friendService.CheckFriend(user.Email)) { var CustomerID = _friendService.GetUserID(user.Email); var customer1 = await _userManager.FindByIdAsync(CustomerID); Coupon coupon = new Coupon() { Code = _couponService.GenerateChode(), IsValid = true, Value = 0.10 }; _couponService.AddCoupon(coupon); await _emailService.SendEmailAsync(customer1.Email, "E-commerce", "<h1>Your friend accepted your invitation</h1>" + $"<p>Use the following discount code for your next purchase " + coupon.Code + "</p>"); } return(Ok(new { message = "User created successfully!" })); // Status Code: 200 } return(BadRequest(result)); } return(BadRequest("Some properties are not valid")); // Status code: 400 }
public async Task ShoppingCartAsync(ShoppingCartVM cart) { Check check = new Check() { Price = cart.totalprice, TotalPrice = cart.totalprice, DateTime = DateTime.Now, PaymentMethodID = cart.paymentmethodId, FirstName = cart.firstname, LastName = cart.lastname }; if (cart.coupon != null) { var coupon = _context.Coupon.First(a => a.Code == cart.coupon); coupon.IsValid = false; _context.SaveChanges(); check.CouponID = coupon.ID; check.Discount = coupon.Value; } _context.Add(check); _context.SaveChanges(); ShoppingCart ShoppingCart = new ShoppingCart() { TotalPrice = cart.totalprice, Date = DateTime.Now, Address = cart.adress, City = cart.city, CustomerID = cart.customerId, BranchID = cart.branchId, CheckID = check.ID }; _context.Add(ShoppingCart); _context.SaveChanges(); foreach (var x in cart.Orders) { var itemsizeID = _context.Inventory.First(a => a.ID == x.inventoryId && a.BranchID == cart.branchId).ItemSizeID; var order = new Order() { Date = DateTime.Now, UnitCost = x.unitcost, TotalPrice = x.totalpriceitem, Quantity = x.quantity, ItemSizeID = itemsizeID, ShoppingCartID = ShoppingCart.ID }; var inventory = _context.Inventory.First(a => a.ID == x.inventoryId && a.ItemSizeID == itemsizeID && a.BranchID == cart.branchId); inventory.Quantity = inventory.Quantity - x.quantity; if (inventory.Quantity <= 0) { inventory.IsAvailable = false; } _context.SaveChanges(); _context.Add(order); _context.SaveChanges(); } var customer = _context.Customer.First(a => a.ID == ShoppingCart.CustomerID); customer.TotalSpent += ShoppingCart.TotalPrice; if (customer.TotalSpent >= 1000) { Coupon coupon = new Coupon() { Code = _couponService.GenerateChode(), IsValid = true, Value = 0.10 }; _couponService.AddCoupon(coupon); var user = await _userManager.FindByIdAsync(cart.customerId); await _emailService.SendEmailAsync(user.Email, "E-commerce", "<h1>Congratulations, you have won a gift bonus</h1>" + $"<p>Use the following discount code for your next purchase " + coupon.Code + "</p>"); customer.TotalSpent = 0; } _context.SaveChanges(); var vm = new PaymentVM() { CardNumber = cart.cardnumber, Month = cart.month, Year = cart.year, Cvc = cart.cvc, TotalPrice = cart.totalprice }; if (cart.paymentmethodId == 1) { await _paymentService.PayAsync(vm); if (_context.CreditCard.FirstOrDefault(a => a.CardNumber == vm.CardNumber && a.CustomerID == cart.customerId) == null) { var card = new CreditCard() { CardNumber = vm.CardNumber, ExpMonth = vm.Month, ExpYear = vm.Year, Cvc = vm.Cvc, CustomerID = cart.customerId }; _context.Add(card); _context.SaveChanges(); ShoppingCart.CreditCardID = card.ID; _context.SaveChanges(); } else { ShoppingCart.CreditCardID = _context.CreditCard.FirstOrDefault(a => a.CardNumber == vm.CardNumber && a.CustomerID == cart.customerId).ID; _context.SaveChanges(); } } var history = new HistoryOfPayments() { CustomerID = cart.customerId, CheckID = check.ID, Date = DateTime.Now, Price = cart.totalprice }; _context.Add(history); _context.SaveChanges(); }
public IActionResult Get() { return(Ok(_couponService.GenerateChode())); }