public async Task <IActionResult> Edit(long id, [Bind("Address_Id,AddressLine1,AddressLine2,City,State,Country,ZipCode")] Address address) { if (id != address.Address_Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(address); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AddressExists(address.Address_Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(address)); }
public async Task <IActionResult> MoveToCart(int?id) { if (id == null) { return(NotFound()); } var cartItem = await _context.CartItem.FindAsync(id); if (cartItem == null) { return(NotFound()); } cartItem.WantToBuy = true; _context.Update(cartItem); await _context.SaveChangesAsync(); return(RedirectToAction("WishListIndex")); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } if (string.IsNullOrWhiteSpace(Input.FamilyName)) { Input.FamilyName = "default"; } if (string.IsNullOrWhiteSpace(Input.Address)) { Input.Address = "default"; } if (string.IsNullOrWhiteSpace(Input.Gender)) { Input.Gender = "default"; } if (string.IsNullOrWhiteSpace(Input.GivenName)) { Input.GivenName = "default"; } if (string.IsNullOrWhiteSpace(Input.BirthDate)) { Input.BirthDate = "0000-00-00"; } if (string.IsNullOrWhiteSpace(Input.PhoneNumber)) { Input.PhoneNumber = "+01234567890"; } if (string.IsNullOrWhiteSpace(Input.NickName)) { Input.NickName = "default"; } if (string.IsNullOrWhiteSpace(Input.AddressLine1)) { Input.AddressLine1 = "default"; } if (string.IsNullOrWhiteSpace(Input.AddressLine2)) { Input.AddressLine2 = "default"; } if (string.IsNullOrWhiteSpace(Input.City)) { Input.City = "default"; } if (string.IsNullOrWhiteSpace(Input.State)) { Input.State = "default"; } if (string.IsNullOrWhiteSpace(Input.Country)) { Input.Country = "default"; } if (string.IsNullOrWhiteSpace(Input.ZipCode)) { Input.ZipCode = "default"; } //update cognito user.Attributes[CognitoAttribute.Address.AttributeName] = Input.Address; user.Attributes[CognitoAttribute.BirthDate.AttributeName] = Input.BirthDate; user.Attributes[CognitoAttribute.Gender.AttributeName] = Input.Gender; user.Attributes[CognitoAttribute.NickName.AttributeName] = Input.NickName; user.Attributes[CognitoAttribute.PhoneNumber.AttributeName] = Input.PhoneNumber; user.Attributes[CognitoAttribute.FamilyName.AttributeName] = Input.FamilyName; user.Attributes[CognitoAttribute.GivenName.AttributeName] = Input.GivenName; user.Attributes["custom:AddressLine1"] = Input.AddressLine1; user.Attributes["custom:AddressLine2"] = Input.AddressLine2; user.Attributes["custom:City"] = Input.City; user.Attributes["custom:State"] = Input.State; user.Attributes["custom:Country"] = Input.Country; user.Attributes["custom:ZipCode"] = Input.ZipCode; var result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } //input and update customer address information into the DB else { var id = user.Attributes[CognitoAttribute.Sub.AttributeName]; //get customer information var recentCustomer = await _context.Customer.FindAsync(id); var address = from m in _context.Address where m.Customer == recentCustomer && m.IsPrimary == true select m; if (address == null) { var newAddress = new Address { IsPrimary = true, AddressLine1 = Input.AddressLine1, AddressLine2 = Input.AddressLine2, City = Input.City, Country = Input.Country, State = Input.State, Customer = recentCustomer, ZipCode = Convert.ToInt32(Input.ZipCode) }; _context.Add(newAddress); } else { Address recentAddress = new Address(); foreach (var add in address) { recentAddress = add; } //var recentAddress =await _context.Address.FindAsync(addressId); recentAddress.AddressLine1 = Input.AddressLine1; recentAddress.AddressLine2 = Input.AddressLine2; recentAddress.City = Input.City; recentAddress.Country = Input.Country; recentAddress.ZipCode = Convert.ToInt32(Input.ZipCode); recentAddress.State = Input.State; recentAddress.Customer = recentCustomer; recentAddress.IsPrimary = true; _context.Update(recentAddress); } await _context.SaveChangesAsync(); } return(RedirectToPage()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { //get user id var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value; var user = await _userManager.FindByIdAsync(userId); if (user == null) { return(NotFound($"Unable to load user with ID '{userId}'.")); } var result = await _userManager.ConfirmSignUpAsync(user, Input.Code, true); if (!result.Succeeded) { throw new InvalidOperationException($"Error confirming account for user with ID '{userId}':"); } else { //this part is to add customer information into the DB var userName = await _userManager.GetUserNameAsync(user); var firstName = user.Attributes[CognitoAttribute.GivenName.AttributeName]; var lastName = user.Attributes[CognitoAttribute.FamilyName.AttributeName]; var email = user.Attributes[CognitoAttribute.Email.AttributeName]; var dateOfBirth = user.Attributes[CognitoAttribute.BirthDate.AttributeName]; var phone = user.Attributes[CognitoAttribute.PhoneNumber.AttributeName]; var customer_ID = user.Attributes[CognitoAttribute.Sub.AttributeName]; var customer = new Customer() { Customer_Id = customer_ID, //Customer_Id = 1111, Username = userName, FirstName = firstName, LastName = lastName, Email = email, Phone = phone }; _context.Add(customer); _context.SaveChanges(); var currentCustomerID = _context.Customer.Find(user.Attributes[CognitoAttribute.Sub.AttributeName]); //get cardid var cartId = HttpContext.Request.Cookies["CartId"]; var recentCart = await _context.Cart.FindAsync(Convert.ToString(cartId)); recentCart.Customer = currentCustomerID; _context.Update(recentCart); await _context.SaveChangesAsync(); return(returnUrl != null?LocalRedirect(returnUrl) : Page() as IActionResult); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } if (string.IsNullOrWhiteSpace(Input.AddressLine1)) { Input.AddressLine1 = "default"; } if (string.IsNullOrWhiteSpace(Input.AddressLine2)) { Input.AddressLine2 = "default"; } if (string.IsNullOrWhiteSpace(Input.City)) { Input.City = "default"; } if (string.IsNullOrWhiteSpace(Input.State)) { Input.State = "default"; } if (string.IsNullOrWhiteSpace(Input.Country)) { Input.Country = "default"; } if (string.IsNullOrWhiteSpace(Input.ZipCode)) { Input.ZipCode = "default"; } user.Attributes["custom:AddressLine1"] = Input.AddressLine1; user.Attributes["custom:AddressLine2"] = Input.AddressLine2; user.Attributes["custom:City"] = Input.City; user.Attributes["custom:State"] = Input.State; user.Attributes["custom:Country"] = Input.Country; user.Attributes["custom:ZipCode"] = Input.ZipCode; var result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } //input customer information into the DB else { var id = user.Attributes[CognitoAttribute.Sub.AttributeName]; //get customer information var recentCustomer = await _context.Customer.FindAsync(id); var address = from m in _context.Address where m.Customer == recentCustomer && m.IsPrimary == true select m; Address recentAddress = new Address(); foreach (var add in address) { //addressId = add.Address_Id; recentAddress = add; } //var recentAddress =await _context.Address.FindAsync(addressId); recentAddress.AddressLine1 = Input.AddressLine1; recentAddress.AddressLine2 = Input.AddressLine2; recentAddress.City = Input.City; recentAddress.Country = Input.Country; //recentAddress.ZipCode = Convert.ToInt32(Input.ZipCode); recentAddress.State = Input.State; recentAddress.Customer = recentCustomer; recentAddress.IsPrimary = true; _context.Update(recentAddress); await _context.SaveChangesAsync(); } return(RedirectToPage()); }