public async Task<ActionResult> NewShippingInfo(AddressViewModel model, bool saveAddress,
            bool returnToConfirm = false)
        {
            Guid memberId = GetUserId();

            RedirectToRouteResult invalidStateResult = await EnsureCartNotEmptyAsync(memberId);
            if (invalidStateResult != null)
            {
                return invalidStateResult;
            }

            if (!ModelState.IsValid)
            {
                model.UpdatePostalCodeModelError(ModelState);

                this.AddAlert(AlertType.Error, "Some address information was invalid.");

                await model.SetupAddressesAndCountries(db, memberId);

                return View("ShippingInfo", model);
            }

            bool validCountry = await db.Countries.AnyAsync(c => c.CountryCode == model.CountryCode);
            bool validProvince = true;

            if (!validCountry)
            {
                this.AddAlert(AlertType.Error, "The Country you selected isn't valid.");
            }
            else
            {
                validProvince = await db.Provinces.
                AnyAsync(p => p.CountryCode == model.CountryCode &&
                        p.ProvinceCode == model.ProvinceCode);

                if (!validProvince)
                {
                    this.AddAlert(AlertType.Error,
                        "The Province/State you selected isn't in the Country you selected.");
                }
            }

            if (!validCountry || !validProvince)
            {
                await model.SetupAddressesAndCountries(db, memberId);

                return View("ShippingInfo", model);
            }

            var orderCheckoutDetails =  Session[OrderCheckoutDetailsKey] as WebOrderCheckoutDetails ??
                new WebOrderCheckoutDetails();

            model.FormatPostalCode();

            if (saveAddress)
            {
                var newAddress = new MemberAddress
                {
                    MemberId = memberId,
                    Address = model.MapToNewAddress(),
                    CountryCode = model.CountryCode,
                    ProvinceCode = model.ProvinceCode
                };

                db.MemberAddresses.Add(newAddress);

                await db.SaveChangesAsync();

                this.AddAlert(AlertType.Success, "Successfully add the new address.");

                orderCheckoutDetails.MemberAddressId = newAddress.Id;
            }
            else
            {
                orderCheckoutDetails.Address = model.MapToNewAddress();
                orderCheckoutDetails.CountryCode = model.CountryCode;
                orderCheckoutDetails.ProvinceCode = model.ProvinceCode;
            }

            Session[OrderCheckoutDetailsKey] = orderCheckoutDetails;

            if (returnToConfirm)
            {
                return RedirectToAction("ConfirmOrder");
            }

            return RedirectToAction("BillingInfo");
        }
Esempio n. 2
0
        public async Task<ActionResult> EditAddress(Guid id, AddressViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.UpdatePostalCodeModelError(ModelState);

                this.AddAlert(AlertType.Error, "Some address information was invalid.");

                await model.SetupCountries(db);

                return View("ManageAddresses", model);
            }

            MemberAddress editedAddress = await db.MemberAddresses.FindAsync(id);

            if (editedAddress == null)
            {
                throw new HttpException(NotFound, "Address");
            }

            model.FormatPostalCode();

            editedAddress.Address = model.MapToNewAddress();
            editedAddress.ProvinceCode = model.ProvinceCode;
            editedAddress.CountryCode = model.CountryCode;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                // Get the exception which states if a foreign key constraint was violated
                SqlException innermostException = ex.GetBaseException() as SqlException;

                bool errorWasProvinceForeignKeyConstraint = false;

                if (innermostException != null)
                {
                    string exMessage = innermostException.Message;

                    errorWasProvinceForeignKeyConstraint =
                        innermostException.Number == (int) SqlErrorNumbers.ConstraintViolation &&
                            exMessage.Contains(nameof(Province.ProvinceCode)) &&
                            exMessage.Contains(nameof(Province.CountryCode));
                }

                this.AddAlert(
                    AlertType.Error,
                    errorWasProvinceForeignKeyConstraint
                        ? "The Province/State you selected isn't in the Country you selected."
                        : "An unknown error occured while adding the address.");

                await model.SetupCountries(db);

                return View(model);
            }

            this.AddAlert(AlertType.Success, "Successfully updated the address.");
            return RedirectToAction("ManageAddresses");
        }