public IActionResult PutCity([FromRoute] int id, [FromBody] CityDTOwithAttractions cityDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != cityDTO.Id) { return(BadRequest(id)); } City city = _mapper.Map <City>(cityDTO); try { _unitOfWork.Cities.Update(city); _unitOfWork.Complete(); } catch (DbUpdateConcurrencyException ex) { return(!_unitOfWork.Cities.Exists(id) ? NotFound(id) : StatusCode(412, ex.Message)); } return(NoContent()); }
public void PutCity() { var repo = new MockUnitOfWork(); var controller = new CitiesController(repo, _mapper); repo.Cities.Add(_testData.Cities.First()); var city = new CityDTOwithAttractions() { Id = 0, Name = "Changed", Description = "Hovedstad i FR", Attractions = new List <TouristAttraction>() { _testData.Attractions.SingleOrDefault(t => t.Id == 2) } }; var response = controller.PutCity(0, city); Assert.IsType <NoContentResult>(response); Assert.Equal(city.Name, repo.Cities.Get(0).Name); }