public async Task <IActionResult> UpdateCity(int id, [FromForm] CityToUpdate city) { var toUpdateCity = await _context.City.FirstOrDefaultAsync(c => c.Cityid == id); if (toUpdateCity == null) { return(NoContent()); } // map our form data to our updated model _mapper.Map(city, toUpdateCity); return(Ok(await _context.SaveChangesAsync())); }
public async Task <IActionResult> UpdateCity(Guid id, [FromBody] CityToUpdate cityToUpdate) { if (id == Guid.Empty) { return(BadRequest("Invalid City Id")); } if (cityToUpdate == null) { return(BadRequest("No update information supplied")); } // Return 422 Unprocessable Entity (you could argue for a 400 instead) if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var dbCity = await _cityRepository.GetCityAsync(id); if (dbCity == null) { return(NotFound()); } _mapper.Map(cityToUpdate, dbCity); _cityRepository.UpdateCity(dbCity); var result = await _cityRepository.SaveAsync(); // Return a 204 if (result) { return(NoContent()); } throw new ArgumentException("Unable to update a City"); }