public ZipCodeUpdateModel(ZipCode zipCode) { CountryCode = zipCode.CountyCode; StateId = zipCode.StateId; CityId = zipCode.CityId; Zip = zipCode.Zip; Longitude = zipCode.Longitude; Latitude = zipCode.Latitude; }
public ZipCode Update(ZipCode entity) { var errors = ValidationZipCodeUpdate(entity); if (!errors.Any()) { entity = ZipCodeRepository.Update(entity); return entity; } throw new ErrorException(errors); }
public void Delete(ZipCode entity) { try { ZipCodeRepository.Delete(entity); } catch (Exception ex) { throw new ErrorException(new[] { new Error(string.Empty, ex.Message) }); } }
public ActionResult Create(ZipCodeCreateModel model) { if (ModelState.IsValid) { try { var country = CountriesService.GetCountryByCountryCode(model.CountryCode); var state = StatesService.GetStateByStateId(model.StateId); var city = CitiesService.GetCityByCityId(model.CityId); if (country != null && state != null && city != null) { var zip = new ZipCode() { City = city, CityId = model.CityId, CountyCode = model.CountryCode, StateId = model.StateId, Zip = model.Zip, Latitude = model.Latitude, Longitude = model.Longitude }; zip = ZipCodesService.Create(zip); this.FlashInfo(string.Format("Zip {0} was created successfully", zip.Zip)); return RedirectToAction("Index"); } } catch (ErrorException errorException) { errorException.ToModelState(this); return View(model); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); return View(model); } } return View(model); }
public ZipCodeDeleteModel(ZipCode zipCode) { ZipCode = zipCode; }
IEnumerable<Error> BasicZipCodeValidation(ZipCode entity) { if (entity.City == null) yield return new Error("City"); if (string.IsNullOrEmpty(entity.Zip)) yield return new Error("Zip", "Zip is required."); }
IEnumerable<Error> ValidationZipCodeUpdate(ZipCode entity) { foreach (var error in BasicZipCodeValidation(entity)) yield return error; var oldZip = ZipCodeRepository.ByCountryCodeAndStateIdAndCityIdAndZipCode(entity.CountyCode, entity.StateId, entity.CityId, entity.Zip); if (!oldZip.Zip.Equals(entity.Zip)) foreach (var error in ValidateUniquenessOfZipCodeInCity(entity)) yield return error; }
IEnumerable<Error> ValidationZipCodeCreate(ZipCode entity) { foreach (var error in BasicZipCodeValidation(entity)) yield return error; foreach (var error in ValidateUniquenessOfZipCodeInCity(entity)) yield return error; }
IEnumerable<Error> ValidateUniquenessOfZipCodeInCity(ZipCode entity) { if (entity.City.ZipCodes.Any(z => z.Zip.Equals(entity.Zip, StringComparison.InvariantCultureIgnoreCase))) yield return new Error("Zip", string.Format("Zip {0} already exists. Please try again.", entity.Zip)); }