Beispiel #1
0
        public ActionResult Edit([Bind(Include = "SiteName,JobSiteID,Address,Town,State,Zip,Lat,Long")] JobSite jobSite)
        {
            if (ModelState.IsValid)
            {
                db.Entry(jobSite).State = EntityState.Modified;

                //Query the API for new coordinates upon changes to any properties that actually change the address.
                var entry   = db.Entry(jobSite);
                var changed = entry.CurrentValues.PropertyNames.Where(x => entry.Property(x).IsModified);
                if (changed.Contains("Address") || changed.Contains("Town") || changed.Contains("State") || changed.Contains("Zip"))
                {
                    var validateZipCode = FormAPIZipCodeService.ValidateZipCode(jobSite.Zip, jobSite.State.GetDisplayName(), jobSite.Town);

                    if (validateZipCode.Successful)
                    {
                        var geoCode = OpenGeocodingService.GetGeocode(jobSite.Address, jobSite.Town, jobSite.State.GetDisplayName(), jobSite.Zip);

                        jobSite.Lat  = geoCode.Return.Geocode.DecimalLatitude;
                        jobSite.Long = geoCode.Return.Geocode.DecimalLongitude;

                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError("Zip", validateZipCode.Return.ErrorMessage);
                    }
                }
            }
            return(View(jobSite));
        }
Beispiel #2
0
        public ActionResult Create([Bind(Include = "SiteName,JobSiteID,Address,Town,State,Zip,Lat,Long")] JobSite jobSite)
        {
            if (ModelState.IsValid)
            {
                var validateZipCode = FormAPIZipCodeService.ValidateZipCode(jobSite.Zip, jobSite.State.GetDisplayName(), jobSite.Town);

                if (validateZipCode.Successful)
                {
                    var geoCode = OpenGeocodingService.GetGeocode(jobSite.Address, jobSite.Town, jobSite.State.GetDisplayName(), jobSite.Zip);

                    jobSite.Lat  = geoCode.Return.Geocode.DecimalLatitude;
                    jobSite.Long = geoCode.Return.Geocode.DecimalLongitude;

                    db.JobSites.Add(jobSite);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("Zip", validateZipCode.Return.ErrorMessage);
                }
            }

            return(View(jobSite));
        }
Beispiel #3
0
        public ActionResult CreateAjax([Bind(Include = "SiteName,JobSiteID,Address,Town,State,Zip,Lat,Long")] JobSite jobSite)
        {
            if (ModelState.IsValid)
            {
                var validateZipCode = FormAPIZipCodeService.ValidateZipCode(jobSite.Zip, jobSite.State.GetDisplayName(), jobSite.Town);

                // the following logic is setup to bypass the 509 error code from the FormAPIZipCodeService.
                // It may seem counter intutative, but it first checks if it was unsucessful, and if so, if it was a 509
                // error. if it's a 509, then it adds to the database anyway, if not, it reports the error to the form,
                // and it if it was successful, then it jumps to the add database label using the goto/label.

                if (!validateZipCode.Successful)
                {
                    if (validateZipCode.Return.ErrorMessage.Contains("509"))
                    {
                        //509 error is bandwidth limit exceeded or rather, that the api server itself is busy.
                        var geoCode = OpenGeocodingService.GetGeocode(jobSite.Address, jobSite.Town, jobSite.State.GetDisplayName(), jobSite.Zip);

                        jobSite.Lat  = geoCode.Return.Geocode.DecimalLatitude;
                        jobSite.Long = geoCode.Return.Geocode.DecimalLongitude;

                        db.JobSites.Add(jobSite);
                        db.SaveChanges();
                        return(Json(new { success = "true", jobSite_Text = jobSite.SiteName, jobSite_Value = jobSite.JobSiteID }));
                    }
                    else
                    {
                        // invalid zip code
                        // add error to zip field
                        ModelState.AddModelError("Zip", validateZipCode.Return.ErrorMessage);

                        // return json with false success, and pass in the field name and errors
                        return(Json(new { success = "false", errors = ExtractErrors(ViewData.ModelState) }));
                    }
                }
                else
                {
                    var geoCode = OpenGeocodingService.GetGeocode(jobSite.Address, jobSite.Town, jobSite.State.GetDisplayName(), jobSite.Zip);

                    jobSite.Lat  = geoCode.Return.Geocode.DecimalLatitude;
                    jobSite.Long = geoCode.Return.Geocode.DecimalLongitude;

                    db.JobSites.Add(jobSite);
                    db.SaveChanges();
                    return(Json(new { success = "true", jobSite_Text = jobSite.SiteName, jobSite_Value = jobSite.JobSiteID }));
                }
            }
            else
            {
                // invalid form
                // return json with false success, and pass in the field name and errors
                return(Json(new { success = "false", errors = ExtractErrors(ViewData.ModelState) }));
            }
        }