Exemple #1
0
        public JsonResult GetProvinces(ConfigurationRequestViewModel viewModel)
        {
            var country = _addressConfigurationService.GetCountry(viewModel.CountryId);
            var city    = string.IsNullOrWhiteSpace(viewModel.CityName)
                ? _addressConfigurationService.GetCity(viewModel.CityId)
                : _addressConfigurationService.GetCity(viewModel.CityName);

            if (country == null)
            {
                // this is an error
            }
            else
            {
                // city may be null: that is handled in the service
                var provinces = _addressConfigurationService.GetAllProvinces(
                    viewModel.IsBillingAddress
                        ? AddressRecordType.BillingAddress
                        : AddressRecordType.ShippingAddress,
                    country, city);
                return(Json(new {
                    Success = true,
                    Provinces = provinces
                                .Select(tp =>
                                        new {
                        Value = tp.Record.TerritoryInternalRecord.Id,
                        Text = _contentManager.GetItemMetadata(tp).DisplayText
                    })
                                .OrderBy(obj => obj.Text)
                }));
            }
            // TODO
            return(Json(new List <string>()));
        }
        /// <summary>
        /// validation of the vm coming from a create/edit action
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        /// <remarks>
        /// It would be cleaner to do this in its own validation classes,
        /// but we need a bunch of IDependencies, so putting this code
        /// here is less of an hassle.
        /// </remarks>
        public bool Validate(AddressEditViewModel vm)
        {
            var validCountries = _addressConfigurationService
                                 .GetAllCountries(vm.AddressType);
            var countryTP = _addressConfigurationService
                            .GetCountry(vm.CountryId);

            if (!SubValidation(validCountries, countryTP))
            {
                return(false);
            }
            var validProvinces = _addressConfigurationService
                                 .GetAllProvinces(vm.AddressType, countryTP);

            if (validProvinces == null)
            {
                // error condition
                return(false);
            }
            var provinceTP = GetTerritory(vm.Province);

            if (validProvinces.Any())
            {
                // there may not be any configured province, and that is ok,
                // but if any is configured, we check that the one selected is valid
                if (!SubValidation(validProvinces, provinceTP))
                {
                    return(false);
                }
            }
            var validCities = _addressConfigurationService
                              .GetAllCities(vm.AddressType,
                                            // use province if it exists, otherwise country
                                            provinceTP == null ? countryTP : provinceTP);

            if (validCities == null)
            {
                // error condition
                return(false);
            }
            if (validCities.Any())
            {
                // there may not be any configured city, and that is ok,
                // but if any is configured, we check that the one selected is valid
                var cityTP = GetTerritory(vm.City);
                if (!SubValidation(validCities, cityTP))
                {
                    return(false);
                }
            }
            return(true);
        }