public StateUpdateModel(State state)
 {
     StateName = state.StateName;
     StateCode = state.StateCode;
     IsTerritory = state.IsTerritory == "1";
     ContiguousLand = state.ContiguousLand == "1";
     CountryCode = state.CountryCode;
 }
        public State Update(State entity)
        {
            var errors = ValidateStateUpdate(entity);
            if (!errors.Any())
            {
                entity = StateRepository.Update(entity);
                return entity;
            }

            throw new ErrorException(errors);
        }
 public void Delete(State entity)
 {
     try
     {
         StateRepository.Delete(entity);
     }
     catch (Exception ex)
     {
         throw new ErrorException(new[] { new Error(string.Empty, ex.Message) });
     }
 }
 public ActionResult Create(StateCreateModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             var country = CountriesService.GetCountryByCountryCode(model.CountryCode);
             if (country != null)
             {
                 var state = new State()
                                 {
                                     ContiguousLand = model.ContiguousLand ? "1" : "0",
                                     CountryCode = country.CountryCode,
                                     Country = country,
                                     IsTerritory = model.IsTerritory ? "1" : "0",
                                     StateCode = model.StateCode,
                                     StateName = model.StateName
                                 };
                 state = StatesService.Create(state);
                 this.FlashInfo(string.Format("State {0} was created successfully", state.StateName));
                 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 StateDeleteModel(State state)
 {
     State = state;
 }
        IEnumerable<Error> BasicStateValidation(State entity)
        {
            if (entity.Country == null)
                yield return new Error("Country", "Country is required.");

            if (string.IsNullOrEmpty(entity.StateCode))
                yield return new Error("StateCode", "State Code is required.");

            if (string.IsNullOrEmpty(entity.StateName))
                yield return new Error("StateName", "State Name is required.");

            if (string.IsNullOrEmpty(entity.ContiguousLand))
                yield return new Error("ContiguousLand", "Contiguous Land is required.");

            if (string.IsNullOrEmpty(entity.IsTerritory))
                yield return new Error("IsTerritory", "Is State Territory is required.");
        }
 IEnumerable<Error> ValidateUniquenessOfStateName(State entity)
 {
     if (entity.Country.States.Any(s => s.StateName.Equals(entity.StateName, StringComparison.InvariantCultureIgnoreCase)))
         yield return new Error("StateName", string.Format("There is already a state with the name {0}, please try again.", entity.StateName));
 }
        IEnumerable<Error> ValidateStateUpdate(State entity)
        {
            foreach (var error in BasicStateValidation(entity)) yield return error;

            var oldState = GetStateByStateId(entity.StateId);

            if (entity.Country == null)
            {
                if (!oldState.StateName.Equals(entity.StateName, StringComparison.InvariantCultureIgnoreCase))
                    foreach (var error in ValidateUniquenessOfStateName(entity)) yield return error;

                if (!oldState.StateCode.Equals(entity.StateCode, StringComparison.InvariantCultureIgnoreCase))
                    foreach (var error in ValidateUniquenessOfStateCode(entity)) yield return error;
            }
        }
        IEnumerable<Error> ValidateStateCreate(State entity)
        {
            foreach (var error in BasicStateValidation(entity)) yield return error;

            if (entity.Country != null)
            {
                foreach (var error in ValidateUniquenessOfStateName(entity)) yield return error;

                foreach (var error in ValidateUniquenessOfStateCode(entity)) yield return error;
            }
        }