コード例 #1
0
        public async Task <IActionResult> AddCitiesToCountry([FromForm] AddCitiesToCountryVM country)
        {
            if (country.CountryId == Guid.Empty)
            {
                ModelState.AddModelError(string.Empty, "Unexpected error occurred: The ID was blank.");

                return(BadRequest(ModelState));
            }

            var result = await _service.AddCitiesToCountry(country);

            if (result.Message == ActionMessages.Updated)
            {
                var cities = country.CitiesId.Count > 1 ? "cities" : "city";

                return(RedirectToAction(nameof(Details), new { id = result.Country.Id, message = $"The requested {cities} were successfully added to {result.Country.Name}." }));
            }
            else if (result.Message == ActionMessages.NotFound)
            {
                ModelState.AddModelError(string.Empty, $"Unexpected error occurred: Could not find any country with the ID: {country.CountryId}");

                return(NotFound(ModelState));
            }
            else
            {
                ModelState.AddModelError(string.Empty, $"Something unexpected happened. The requested action: {result.Message.ToString()}");

                return(BadRequest(ModelState));
            }
        }
コード例 #2
0
        public async Task <CountryWithMessage> AddCitiesToCountry(AddCitiesToCountryVM country)
        {
            try
            {
                if (country.CountryId == Guid.Empty)
                {
                    throw new Exception("Unexpected error occurred: The countryId was blank.");
                }

                if (country.CitiesId == null || country.CitiesId.Count == 0)
                {
                    throw new ArgumentNullException("CitiesId", "Unexpected error occurred: the list was null.");
                }

                var original = await _db.Countries.Include(x => x.Cities).FirstOrDefaultAsync(x => x.Id == country.CountryId);

                if (original == null)
                {
                    throw new ArgumentNullException("original", "Unexpected error occurred: country was null.");
                }

                foreach (var cityId in country.CitiesId)
                {
                    var city = await _db.Cities.FirstOrDefaultAsync(x => x.Id == cityId);

                    if (city == null)
                    {
                        throw new Exception($"Unexpected error occurred: No city was found with the given ID: {cityId}");
                    }
                    else if (original.Cities.Contains(city))
                    {
                        throw new ArgumentException($"Unexpected error occurred: {city.Name} already exists in {original.Name}.", "original");
                    }

                    original.Cities.Add(city);
                }

                await _db.SaveChangesAsync();

                return(new CountryWithMessage {
                    Country = original, Message = ActionMessages.Updated
                });
            }
            catch (ArgumentException ex)
            {
                throw new DuplicateWaitObjectException(ex.ParamName, ex.Message);
            }
            catch (Exception ex)
            {
                throw new NullReferenceException(ex.Message, ex.InnerException);
            }
        }