示例#1
0
        // signs the user up based on the information filled in the entries
        // returns a SignUpResponse if successful and null if unsuccessful
        private async Task <SignUpResponse> signUp()
        {
            var addressLine2 = "";

            if (Address2.Text == null)
            {
                addressLine2 = "";
            }
            SignUpPost signUpContent = new SignUpPost
            { // SignUpPost object to send to database
                FirstName      = this.firstNameEntry.Text,
                LastName       = this.lastNameEntry.Text,
                Address1       = this.Address1.Text,
                Address2       = addressLine2,
                City           = this.City.Text,
                State          = this.State.Text,
                Zipcode        = this.Zipcode.Text,
                PhoneNumber    = this.phoneNumberEntry.Text,
                Email          = this.emailEntry.Text,
                Password       = this.passwordEntry.Text,
                UserIsCustomer = 1,
                UserIsDonor    = 1,
                UserIsAdmin    = 1,
                UserIsFoodbank = 1
            };

            string signUpContentJson = JsonConvert.SerializeObject(signUpContent);                              // convert to json
            var    httpContent       = new StringContent(signUpContentJson, Encoding.UTF8, "application/json"); // convert to string content

            try
            {
                var response = await client.PostAsync(signUpApi, httpContent); // send to database

                if (response.Content != null)
                { // successful post
                    var responseContent = await response.Content.ReadAsStringAsync();

                    var signUpResponse = JsonConvert.DeserializeObject <SignUpResponse>(responseContent);
                    return(signUpResponse); // return the response from the api
                }
                return(null);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
                return(null);
            }
        }
        async void ValidateAddressClick(object sender, System.EventArgs e)
        {
            if (userEmailAddress.Text != null && !IsValidEmail(userEmailAddress.Text))
            {
                await DisplayAlert("Error", "Please enter a valid email address.", "OK");
            }
            if (userEmailAddress.Text == null || userConfirmEmailAddress.Text == null || userPassword.Text == null || usertAddress.Text == null || userConfirmPassword.Text == null || userFirstName.Text == null || userLastName.Text == null || userLastName.Text.Length == 0 || userPhoneNumber.Text.Length == 0 || usertAddress.Text.Length == 0 || userCity.Text.Length == 0 || userState.Text.Length == 0 || userZipcode.Text.Length == 0)
            {
                await DisplayAlert("Error", "Please enter all information within the boxes provided.", "OK");
            }

            if (userFirstName.Text != null)
            {
                Application.Current.Properties["userFirstName"] = userFirstName.Text;
            }

            if (userLastName.Text != null)
            {
                Application.Current.Properties["userLastName"] = userLastName.Text;
            }

            if (userEmailAddress.Text != null)
            {
                Application.Current.Properties["userEmailAddress"] = userEmailAddress.Text.ToLower();
            }

            if (userPhoneNumber.Text != null)
            {
                Application.Current.Properties["userPhoneNumber"] = userPhoneNumber.Text;
            }
            if (usertAddress.Text != null)
            {
                usertAddress.Text = usertAddress.Text.Trim();
                Application.Current.Properties["userAddress"] = usertAddress.Text;
            }
            if (userCity.Text != null)
            {
                userCity.Text = userCity.Text.Trim();
                Application.Current.Properties["userCity"] = userCity.Text;
            }
            if (userState.Text != null)
            {
                userState.Text = userState.Text.Trim();
                Application.Current.Properties["userState"] = userState.Text;
            }
            if (userUnitNumber.Text != null)
            {
                if (userUnitNumber.Text.Length != 0)
                {
                    userUnitNumber.Text = userUnitNumber.Text.Trim();
                    Application.Current.Properties["userAddressUnit"] = userUnitNumber.Text;
                }
                else
                {
                    Application.Current.Properties["userAddressUnit"] = "";
                }
            }
            if (userZipcode.Text != null)
            {
                userZipcode.Text = userZipcode.Text.Trim();
                Application.Current.Properties["userZipCode"] = userZipcode.Text;
            }

            // Setting request for USPS API
            XDocument requestDoc = new XDocument(
                new XElement("AddressValidateRequest",
                             new XAttribute("USERID", "400INFIN1745"),
                             new XElement("Revision", "1"),
                             new XElement("Address",
                                          new XAttribute("ID", "0"),
                                          new XElement("Address1", usertAddress.Text),
                                          new XElement("Address2", userUnitNumber.Text),
                                          new XElement("City", userCity.Text),
                                          new XElement("State", userState.Text),
                                          new XElement("Zip5", userZipcode.Text),
                                          new XElement("Zip4", "")
                                          )
                             )
                );
            var url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=" + requestDoc;

            Console.WriteLine(url);
            var client   = new WebClient();
            var response = client.DownloadString(url);

            var xdoc = XDocument.Parse(response.ToString());

            Console.WriteLine(xdoc);
            string latitude  = "0";
            string longitude = "0";

            foreach (XElement element in xdoc.Descendants("Address"))
            {
                if (GetXMLElement(element, "Error").Equals(""))
                {
                    if (GetXMLElement(element, "DPVConfirmation").Equals("Y") && GetXMLElement(element, "Zip5").Equals(userZipcode.Text) && GetXMLElement(element, "City").Equals(userCity.Text.ToUpper())) // Best case
                    {
                        // Get longitude and latitide because we can make a deliver here. Move on to next page.
                        // Console.WriteLine("The address you entered is valid and deliverable by USPS. We are going to get its latitude & longitude");
                        //GetAddressLatitudeLongitude();
                        Geocoder geoCoder = new Geocoder();

                        IEnumerable <Position> approximateLocations = await geoCoder.GetPositionsForAddressAsync(usertAddress.Text + "," + userCity.Text + "," + userState.Text);

                        Position position = approximateLocations.FirstOrDefault();

                        latitude    = $"{position.Latitude}";
                        longitude   = $"{position.Longitude}";
                        map.MapType = MapType.Street;
                        var mapSpan = new MapSpan(position, 0.001, 0.001);

                        Pin address = new Pin();
                        address.Label    = "Delivery Address";
                        address.Type     = PinType.SearchResult;
                        address.Position = position;

                        map.MoveToRegion(mapSpan);
                        map.Pins.Add(address);

                        break;
                    }
                    else if (GetXMLElement(element, "DPVConfirmation").Equals("D"))
                    {
                        //await DisplayAlert("Alert!", "Address is missing information like 'Apartment number'.", "Ok");
                        //return;
                    }
                    else
                    {
                        //await DisplayAlert("Alert!", "Seems like your address is invalid.", "Ok");
                        //return;
                    }
                }
                else
                {   // USPS sents an error saying address not found in there records. In other words, this address is not valid because it does not exits.
                    //Console.WriteLine("Seems like your address is invalid.");
                    //await DisplayAlert("Alert!", "Error from USPS. The address you entered was not found.", "Ok");
                    //return;
                }
            }
            if (latitude == "0" || longitude == "0")
            {
                await DisplayAlert("We couldn't find your address", "Please check for errors.", "Ok");
            }

            SignUpPost newUser = new SignUpPost();

            newUser.email           = userEmailAddress.Text;
            newUser.first_name      = userFirstName.Text;
            newUser.last_name       = userLastName.Text;
            newUser.phone_number    = userPhoneNumber.Text;
            newUser.address         = usertAddress.Text;
            newUser.unit            = userUnitNumber.Text;
            newUser.city            = userCity.Text;
            newUser.state           = userState.Text;
            newUser.zip_code        = userZipcode.Text;
            newUser.latitude        = latitude;
            newUser.longitude       = longitude;
            newUser.referral_source = "WEB";
            newUser.role            = "CUSTOMER";
            newUser.access_token    = "";
            newUser.refresh_token   = "";
            newUser.social          = "FALSE";
            newUser.password        = userPassword.Text;

            Application.Current.Properties["latitude"]  = latitude;
            Application.Current.Properties["longitude"] = longitude;

            var content = new StringContent(JsonConvert.SerializeObject(newUser), Encoding.UTF8, "application/json");

            using (var httpClient = new HttpClient())
            {
                var request = new HttpRequestMessage();
                request.Method  = HttpMethod.Post;
                request.Content = new StringContent(JsonConvert.SerializeObject(newUser), Encoding.UTF8, "application/json");;
                var httpResponse = await httpClient.PostAsync("https://tsx3rnuidi.execute-api.us-west-1.amazonaws.com/dev/api/v2/SignUp", content);

                Console.WriteLine("This is the order's response " + httpResponse.Content.ReadAsStringAsync());
            }

            await Application.Current.SavePropertiesAsync();

            await tagUser(userEmailAddress.Text, userZipcode.Text);
        }