public ActionResult ArchiveDeliveryAddress(DeliveryAddressViewModel item) { try { string userId = User.Identity.GetUserId(); if (userId == null) { return RedirectToAction("Login", "Account"); } if (User.IsInRole("Customer")) { var itemId = item.DeliveryAddressId; if (itemId < 1) { RedirectToAction("CustomerRetrieveDeliveryAddresse"); } if (item == null) { RedirectToAction("CustomerRetrieveDeliveryAddress"); } var record = db.DeliveryAddresses.Where(d => d.DeliveryAddressId == itemId && d.UserId == userId).FirstOrDefault(); if (record != null) { record.Status = "Archived"; db.SaveChanges(); } GetCustomerReturnNotification(); GetCustomerNotification(); // return RedirectToAction("Retrieve"); return RedirectToAction("CustomerRetrieveDeliveryAddress"); } return RedirectToAction("Login", "Account"); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()); } }
public async Task<ActionResult> Register(RegisterCustomerViewModel model) { //Verify if the email address is in use on the system var emailValid = (from u in db.Users where u.Email == model.Email select u).Count(); //If the email is already in use the user receives an error message if (emailValid > 0) { ViewBag.StatusMessage = "Email already exists."; } //Verify if the model is valid //if the model is valid then proceed if (ModelState.IsValid) { //Generate activation code and store it in the variable activationCode var activationCode = CodeGenerator(); //Create a new instance of CustomerViewModel and store it in object customer var customer = new CustomerViewModel() { UserName = model.Email, Email = model.Email, //Assign value of activationCode to the property ActivationCode of customer ActivationCode = activationCode, FirstName = model.FirstName, LastName = model.LastName, Name = model.FirstName + " " + model.LastName, Status = "Pending", DtCreated = DateTime.UtcNow, PhoneNumber = model.PhoneNumber }; //Async method to create a new customer var result = await UserManager.CreateAsync(customer, model.Password); //If the method property succeeds if (result.Succeeded) { //Signin the new customer in the application await SignInManager.SignInAsync(customer, isPersistent: false, rememberBrowser: false); //Create an instance of DeliveryAddressViewModel and store it variable customerAddress var customerAddress = new DeliveryAddressViewModel() { Street = model.Street, City = model.City, UserId = customer.Id, CustomerId = customer.CustomerId, Zipcode = model.ZipCode, Status = "Default" }; db.DeliveryAddresses.Add(customerAddress); //Save changes into the database db.SaveChanges(); //It the body of the email that will be sent to the user after the registration process var body = "<p>Dear Valued Customer,</p><p>This is the activation code that has been sent to you in order to validate your registration on BontoBuy</p>" + "<p>Your activation code: {0}</p>"; var message = new MailMessage(); //It contains the recipient of the email message.To.Add(new MailAddress(model.Email)); //It contains the email address of BontoBuy message.From = new MailAddress("*****@*****.**"); //Subject of the mail message.Subject = "Register on BontoBuy"; //Using formatted string the activation code is then added to the body of the email message.Body = string.Format(body, activationCode); message.IsBodyHtml = true; var smtp = new SmtpClient(); //Use credential of BontoBuy email var credential = new NetworkCredential() { UserName = "******", Password = "******" }; smtp.Credentials = credential; smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; await smtp.SendMailAsync(message); ApplicationUser currentUser = db.Users.Where(u => u.Email.Equals(model.Email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); if (customer != null) UserManager.AddToRole(customer.Id, "Customer"); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); return RedirectToAction("RegistrationLogin", "Account"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult CustomerCreateDeliveryAddress(DeliveryAddressActionViewModel item) { var userId = User.Identity.GetUserId(); if (userId == null || !User.IsInRole("Customer")) { return RedirectToAction("Login", "Account"); } if (ModelState.IsValid) { ViewBag.DeliveryAddressStatusId = new SelectList(db.DeliveryAddressStatuses, "DeliveryAddressStatusId", "Status", item.DeliveryAddressStatusId); var deliveryAddress = new DeliveryAddressViewModel() { UserId = userId, CustomerId = (from c in db.Customers where c.Id == userId select c.CustomerId).FirstOrDefault(), Street = item.Street, City = item.City, Zipcode = item.Zipcode, Status = (from ds in db.DeliveryAddressStatuses where ds.DeliveryAddressStatusId == item.DeliveryAddressStatusId select ds.Status).FirstOrDefault() }; if (deliveryAddress.Status == "Default") { var otherAddresses = db.DeliveryAddresses.Where(x => x.UserId == userId).ToList(); if (otherAddresses != null) { foreach (var obj in otherAddresses) { obj.Status = "Other"; } } } db.DeliveryAddresses.Add(deliveryAddress); db.SaveChanges(); GetCustomerReturnNotification(); GetCustomerNotification(); return RedirectToAction("CustomerRetrieveDeliveryAddress", "Customer", new { message = ManageMessageId.AddDeliveryAddressSuccess }); } return RedirectToAction("Login", "Account"); }