public async Task <IActionResult> ManageDeliveryAddresses()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            var model = new ManageDeliveryAddressesViewModel {
                StatusMessage = StatusMessage
            };

            var savedAddresses = await _addressManager.QueryAllSavedDeliveryAddresses(user);

            model.savedAddresses = new List <SelectListItem>();

            foreach (DeliveryAddress da in savedAddresses)
            {
                model.savedAddresses.Add(new SelectListItem
                {
                    Text  = da.Zipcode,
                    Value = da.Zipcode
                });
            }

            return(View(model));
        }
        public async Task <IActionResult> RemoveSavedAddress(ManageDeliveryAddressesViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            DeliveryAddress addressOnDeathrow = await _addressManager.FindAddressByZipcodeAsync(model.RemovalZipcode);

            int resultCode = await _addressManager.RemoveDeliveryAddressAsync(user, addressOnDeathrow);

            Debug.WriteLine("Code is: " + addressOnDeathrow.Zipcode);
            StatusMessage = "Delivery address removed";

            return(RedirectToAction(nameof(ManageDeliveryAddresses)));
        }
        public async Task <IActionResult> ManageDeliveryAddresses(ManageDeliveryAddressesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
                }

                DeliveryAddress newAddress = await _addressManager.FindAddressByZipcodeAsync(model.Zipcode);

                if (newAddress == null)
                {
                    newAddress         = new DeliveryAddress();
                    newAddress.Country = model.Country;
                    newAddress.County  = model.County;
                    newAddress.City    = model.City;
                    newAddress.Address = model.Address;
                    newAddress.Zipcode = model.Zipcode;
                    newAddress.User    = user;
                    await _addressManager.CreateDeliveryAddress(newAddress);
                }

                StatusMessage = "New delivery address added";
            }
            catch (Exception)
            {
                StatusMessage = "Failed to add new delivery address";
            }


            return(RedirectToAction(nameof(ManageDeliveryAddresses)));
        }