public ActionResult Create(CountryCreateModel model) { if (ModelState.IsValid) { try { var country = new Country() { CountryCode = model.CountryCode, CountryOrder = model.CountryOrder, CountryName = model.CountryName, Displayable = model.Displayable ? "1" : "0" }; country = CountriesService.Create(country); this.FlashInfo(string.Format("Country {0} was created succcessfully", country.CountryName)); 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 CountryUpdateModel(Country entity) { CountryCode = entity.CountryCode; CountryName = entity.CountryName; CountryOrder = entity.CountryOrder; Displayable = entity.Displayable.Equals("1"); }
public Country Create(Country entity) { var errors = ValidateCountryCreate(entity); if (!errors.Any()) { entity = CountryRepository.Add(entity); return entity; } throw new ErrorException(errors); }
public void Delete(Country entity) { try { CountryRepository.Delete(entity); } catch (Exception ex) { throw new ErrorException(new[] { new Error(string.Empty, ex.Message) }); } }
IEnumerable<Error> BasicCountryValidation(Country entity) { if (string.IsNullOrEmpty(entity.CountryCode)) yield return new Error("CountryCode", "Country Code is required."); if (string.IsNullOrEmpty(entity.CountryName)) yield return new Error("CountryName", "Country Name is required."); if (string.IsNullOrEmpty(entity.Displayable)) yield return new Error("Displayable", "Displayable is required."); }
IEnumerable<Error> ValidateUniquenessOfCountryName(Country entity) { if (GetAll().Any(c => c.CountryName.Equals(entity.CountryName, StringComparison.InvariantCultureIgnoreCase))) yield return new Error("CountryName", string.Format("There is a country with the name {0} already. Please choose another name.", entity.CountryName)); }
IEnumerable<Error> ValidateCountryUpdate(Country entity) { var originalEntity = GetCountryByCountryCode(entity.CountryCode); foreach (var error in BasicCountryValidation(entity)) yield return error; // We changed the country code. if (!originalEntity.CountryCode.Equals(entity.CountryCode)) foreach (var error in ValidateUniquenessOfCountryCode(entity)) yield return error; // We changed the country name. if (!originalEntity.CountryName.Equals(entity.CountryName)) foreach (var error in ValidateUniquenessOfCountryName(entity)) yield return error; }
IEnumerable<Error> ValidateCountryCreate(Country entity) { foreach (var error in BasicCountryValidation(entity)) yield return error; foreach (var error in ValidateUniquenessOfCountryName(entity)) yield return error; foreach (var error in ValidateUniquenessOfCountryCode(entity)) yield return error; }
public CountryDeleteModel(Country country) { Country = country; }