Пример #1
0
        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)));
        }