public IActionResult Update(Guid id, [FromBody] CityUpdateViewModel info)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = _storage.Get(id);

            if (city == null)
            {
                return(NotFound());
            }

            var updatedCity = new City(
                city.Id,
                city.Title,
                info.Description.Trim().Capitalize(),
                info.Population);

            if (city.Title == updatedCity.Description)
            {
                ModelState.AddModelError("Title", "The value duplicated with field Description");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _storage.Remove(city);
            _storage.Add(updatedCity);
            return(Ok(updatedCity));
        }
Exemple #2
0
        public async Task <IActionResult> UpdateCity(Guid countryId, Guid cityId,
                                                     [FromBody] CityUpdateViewModel city)
        {
            if (city == null)
            {
                return(BadRequest());
            }
            if (!await countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }
            var cityModel = await cityRepository.GetCityForCountry(countryId, cityId);

            if (cityModel == null)
            {
                cityModel    = mapper.Map <City>(city);
                cityModel.Id = cityId;
                await cityRepository.AddCityForCountry(countryId, cityModel);

                if (!await unitOfWork.SaveAsync())
                {
                    return(StatusCode(500));
                }
                //return NotFound();
                return(CreatedAtAction(nameof(GetCity), new { countryId, cityId }, cityModel));
            }
            mapper.Map(city, cityModel);
            await cityRepository.UpdateCity(cityModel);

            if (!await unitOfWork.SaveAsync())
            {
                return(StatusCode(500));
            }
            return(NoContent());
        }
Exemple #3
0
        public ActionResult UpdateCity(CityUpdateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid Request"));
            }

            var updateResult = _cityService.UpdateCity(model);

            if (!string.IsNullOrEmpty(updateResult))
            {
                return(StatusCode((int)HttpStatusCode.NotAcceptable, updateResult));
            }
            return(Ok());
        }
Exemple #4
0
        public string UpdateCity(CityUpdateViewModel model)
        {
            var existedCity = _cityRepository.Get(x => x.Deleted == false && x.Id == model.Id);

            if (existedCity == null)
            {
                return("Not found city");
            }

            existedCity.Name       = model.Name;
            existedCity.TimeZoneId = model.TimeZoneId;

            _cityRepository.Update(existedCity);
            try
            {
                _unitOfWork.CommitChanges();
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            return(string.Empty);
        }