Ejemplo n.º 1
0
        private bool ValidateAddress(string addressJson)
        {
            var address = JObject.Parse(addressJson);

            foreach (var component in address["address_components"])
            {
                //var name = component["long_name"].Value<string>();
                //var shortname = component["short_name"].Value<string>();
                var type     = component["types"].First.Value <string>();
                var unitType = AddressUnitParser.Get(type);

                if (unitType == AddressUnitType.PostalCode ||
                    unitType == AddressUnitType.Route)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        public override async Task <IHttpActionResult> Post(RegistrationViewModel regModel)
        {
            var addressUnits = new List <AddressUnitViewModel>();
            var details      = new AddressDetailsViewModel();

            regModel.Customer.Address = new AddressViewModel
            {
                ApiResult = regModel.Customer.AddressJson,
                UnitsAddressUnitViewModels = addressUnits,
                Details = details
            };

            if ((int)regModel.Customer.SearchType > 3 && !regModel.Customer.BirthDate.HasValue)
            {
                return(BadRequest("bithtdate_required"));
            }

            if (!ValidateAddress(regModel.Customer.AddressJson))
            {
                return(BadRequest("address_not_valid"));
            }

            // fill address
            var address = JObject.Parse(regModel.Customer.AddressJson);

            foreach (var component in address["address_components"])
            {
                var name      = component["long_name"].Value <string>();
                var shortname = component["short_name"].Value <string>();
                var type      = component["types"].First.Value <string>();
                var unitType  = AddressUnitParser.Get(type);

                if (unitType == AddressUnitType.Country)
                {
                    regModel.Customer.Address.Country = await _countryQuery.Execute(shortname);
                }

                var addressUnit = await _addressUnitByShortNameQuery.Execute(shortname, unitType);

                if (addressUnit != null)
                {
                    addressUnits.Add(_mapper.Map <AddressUnitViewModel>(addressUnit));
                }
                else
                {
                    addressUnits.Add(new AddressUnitViewModel
                    {
                        Name      = name,
                        ShortName = shortname,
                        Type      = unitType
                    });
                }
            }

            // fill details
            var location = address["geometry"]["location"];

            details.Lat        = location["lat"].Value <double>();
            details.Lng        = location["lng"].Value <double>();
            details.Components = address["address_components"].ToString();
            details.DetailsId  = address["id"].Value <string>();
            details.PlaceId    = address["place_id"].Value <string>();
            details.Reference  = address["reference"].Value <string>();
            details.Scope      = address["scope"].Value <string>();
            details.Types      = string.Join(";", address["types"].Values <string>());
            details.Url        = address["url"].Value <string>();
            details.UtcOffset  = address["utc_offset"].Value <string>();

            return(await base.Post(regModel));
        }