Пример #1
0
        public async Task <CityDTO> PatchAsync(CityUpdateDto city)
        {
            this.Logger.LogTrace($"{nameof(this.PutAsync)} called");
            var result = await this.CityService.UpdateAsync(this.Mapper.Map <CityUpdateModel>(city));

            return(this.Mapper.Map <CityDTO>(result));
        }
Пример #2
0
        public async Task <IActionResult> UpdateCityAsync(int id, [FromBody] CityUpdateDto cityUpdateDto)
        {
            try
            {
                if (cityUpdateDto == null)
                {
                    return(BadRequest());
                }

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

                await _cityService.UpdateCityAsync(id, cityUpdateDto);

                return(Ok());
            }
            catch (ObjectNotFoundException ex)
            {
                _logger.LogInformation(ex.Message);
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Пример #3
0
        public async Task <IActionResult> UpdateCity(int id, CityUpdateDto cityUpdateDto)
        {
            if (cityUpdateDto == null)
            {
                return(BadRequest());
            }

            var cityFromRepo = await _service.GetCityByIdAsync(id);

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

            string country = cityUpdateDto.Country;

            if (!InfraData.CountriesNames.Contains(country) ||
                !InfraData.CountriesCodes[country].Alpha2Code.Equals(cityUpdateDto.Alpha2Code) ||
                !InfraData.CountriesCodes[country].Alpha3Code.Equals(cityUpdateDto.Alpha3Code) ||
                !InfraData.CountriesCodes[country].CurrenciesCode.Equals(cityUpdateDto.CurrenciesCode)
                )
            {
                return(BadRequest("Country or its codes or currency is invalid!"));
            }

            _mapper.Map <CityUpdateDto, City>(cityUpdateDto, cityFromRepo);

            if (await _service.SaveChangesAsync())
            {
                return(NoContent());
            }

            return(StatusCode(StatusCodes.Status500InternalServerError));
        }
Пример #4
0
        public async Task <IActionResult> UpdateCity(int id, CityUpdateDto cityDto)
        {
            var cityfromdb = await uow.CityRepository.FindCity(id);

            mapper.Map(cityDto, cityfromdb);
            await uow.SaveAsync();

            return(StatusCode(200));
        }
Пример #5
0
        public async Task UpdateCityAsync(int id, CityUpdateDto cityUpdateDto)
        {
            var existingCity = await _citiesRepository.FindByIdAsync(id);

            if (existingCity == null)
            {
                throw new ObjectNotFoundException($"City with id {id} does not exist");
            }
            await _citiesRepository.UpdateAsync(id, _mapper.Map <City>(cityUpdateDto));
        }
Пример #6
0
        public async Task <IActionResult> UpdateCity(int id, CityUpdateDto cityDto)
        {
            var cityFromDb = await uow.CityRepository.FindCity(id);

            cityFromDb.LastUpdatedBy = 1;
            cityFromDb.LastUpdatedOn = DateTime.Now;
            mapper.Map(cityDto, cityFromDb);
            await uow.SaveAsync();

            return(StatusCode(200));
        }
Пример #7
0
        public async Task <IActionResult> UpdateCity(int id, CityUpdateDto cityDto)
        {
            var cityFromDB = await uow.CityRepository.FindCity(id);

            if (cityFromDB == null)
            {
                return(BadRequest("Update not allowed"));
            }
            cityFromDB.LastUpdatedBy = 1;
            cityFromDB.LastUpdatedOn = DateTime.Now;
            mapper.Map(cityDto, cityFromDB);
            await uow.SaveAsync();

            return(StatusCode(200));
        }
 public ActionResult <CityDto> UpdateCity([FromBody] CityUpdateDto cityUpdate, Guid cityId)
 {
     try
     {
         City cityWithId = _citiesService.GetCityByCityId(cityId);
         if (cityWithId == null)
         {
             return(NotFound());
         }
         City updatedCity = mapper.Map <City>(cityUpdate);
         updatedCity.CityId = cityId;
         mapper.Map(updatedCity, cityWithId);
         cityRepository.SaveChanges();
         return(Ok(mapper.Map <CityDto>(cityWithId)));
     }
     catch (Exception)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, "Update error"));
     }
 }
Пример #9
0
        public async Task UpdateCityAsync_Should_Throw_ObjectNotFoundException()
        {
            var city = new City()
            {
                CityId = 1, Name = "Stockholm"
            };
            var cityUpdateDto = new CityUpdateDto {
                Name = "Stockholm"
            };

            var mockedRepository = new Mock <ICitiesRepository>();

            var mapperConfiguration = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new DomainProfile());
            });
            var mockedMapper = mapperConfiguration.CreateMapper();

            var cityService = new CitiesService(mockedRepository.Object, mockedMapper);

            await Assert.ThrowsAsync <ObjectNotFoundException>(() => cityService.UpdateCityAsync(2, cityUpdateDto));
        }
 public async Task <CityDto> Update([FromRoute] int id, [FromBody] CityUpdateDto dto)
 {
     return(await CityAppService.UpdateAsync(id, dto));
 }