public async Task <IActionResult> UpdateCountry(Guid id, [FromBody] CountryUpdateDto country) { if (country == null) { return(BadRequest()); } if (country.Description == country.Name) { ModelState.AddModelError(nameof(CountryUpdateDto), "The provided description should be different from the country name"); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var retrievedCountry = await _countryService.FindCountryById(id); if (retrievedCountry == null) { return(NotFound()); } _mapper.Map(country, retrievedCountry); _countryService.UpdateCountry(retrievedCountry); if (!await _countryService.Save()) { throw new Exception($"Updating country {id} failed on save."); } return(NoContent()); }
public bool UpdateCountry(CountryUpdateDto countryToUpdateDto) { var countryToUpdate = MapConfig.Mapper.Map <Country>(countryToUpdateDto); _countryContext.Update(countryToUpdate); return(Save()); }
public async Task <IActionResult> UpdateCountry(int id, [FromBody] CountryUpdateDto countryDto) { if (countryDto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var country = await _countryRepository.GetCountryByIdAsync(id, includeCities : true); if (country == null) { return(NotFound()); } _mapper.Map(countryDto, country); if (!await _unitOfWork.SaveAsync()) { throw new Exception($"Updating country {id} failed when saving."); } return(NoContent()); }
public IActionResult UpdateCountry(int countryId, [FromBody] CountryUpdateDto updatedCountry) { if (updatedCountry == null) { return(BadRequest(ModelState)); } if (countryId != updatedCountry.Id) { return(BadRequest(ModelState)); } if (!_unitOfWork.CountryRepository.CountryExists(countryId)) { ModelState.AddModelError("", "Country doesn't exist!"); } if (!ModelState.IsValid) { return(StatusCode(404, ModelState)); } if (!_unitOfWork.CountryRepository.UpdateCountry(updatedCountry)) { ModelState.AddModelError("", $"Something went wrong updating the country " + $"{updatedCountry.CountryName}"); } _unitOfWork.Commit(); return(NoContent()); }
public CountryDto Update(CountryUpdateDto dto) { CountryDto countryDto = null; try { var country = _unitOfWork.GenericRepository <Country>().GetById(dto.Id); Mapper.Map <CountryUpdateDto, Country>(dto, country); country.ModifiedBy = _appSession.GetUserName(); _unitOfWork.CreateTransaction(); _unitOfWork.GenericRepository <Country>().Update(country); _unitOfWork.Save(); _unitOfWork.Commit(); CheckForDelete(dto.Provinces, country.Provinces); CheckForAdd(dto.Provinces, country.Id); countryDto = Mapper.Map <Country, CountryDto>(country); } catch (Exception ex) { Tracing.SaveException(ex); } return(countryDto); }
public async Task Update(int id, CountryUpdateDto model) { var entry = await _context.Countries.SingleAsync(x => x.CountryId == id); entry.Name = model.Name; await _context.SaveChangesAsync(); }
public async Task UpdateCountry(int countryId, CountryUpdateDto country) { var countryAsJson = new StringContent(JsonSerializer.Serialize(country), Encoding.UTF8, "application/json"); var response = await _httpClient.PutAsync($"api/countries/{ countryId }", countryAsJson); if (!response.IsSuccessStatusCode) { throw new Exception("Error in server - Please contact your administrator"); } }
public async Task <CountryDtoResult> Put(CountryUpdateDto dto) { var model = _mapper.Map <CountryModel>(dto); var entity = _mapper.Map <CountryEntity>(model); var oldRecord = await _repository.SelectAsync(entity.Id); entity.Name = oldRecord.Name; entity.SvgFile = oldRecord.SvgFile; entity.ObjectJson = oldRecord.ObjectJson; entity.OfficialLanguage = oldRecord.OfficialLanguage; entity.OriginalInfo = false; var result = await _repository.UpdateAsync(entity); return(_mapper.Map <CountryDtoResult>(result)); }
public ActionResult Put(int id, CountryUpdateDto country) { var countryFromRepository = _repository.Get(id); if (countryFromRepository == null) { return(NotFound()); } _mapper.Map(country, countryFromRepository); _repository.Update(countryFromRepository); _repository.SaveChanges(); return(Ok(countryFromRepository)); }
public async Task <IActionResult> UpdateCountry([FromBody] CountryUpdateDto country) { if (country == null) { return(BadRequest("CountryUpdateDto object is null")); } var countryEntity = await _repository.Country.GetCountryAsync(country.Id, trackChanges : true); if (countryEntity == null) { return(NotFound()); } _mapper.Map(country, countryEntity); await _repository.SaveAsync(); return(NoContent()); }
public CountryUpdateDto GetForEdit(int id) { CountryUpdateDto countryDto = null; try { var country = _unitOfWork.GenericRepository <Country>().GetById(id); if (country != null) { countryDto = Mapper.Map <Country, CountryUpdateDto>(country); } } catch (Exception ex) { Tracing.SaveException(ex); } return(countryDto); }
public ActionResult <CountryDto> UpdateCountry([FromBody] CountryUpdateDto countryUpdate, Guid countryId) { try { Country countryWithId = _countriesService.GetCountryByCountryId(countryId); if (countryWithId == null) { return(NotFound()); } Country updatedCountry = mapper.Map <Country>(countryUpdate); updatedCountry.CountryId = countryId; mapper.Map(updatedCountry, countryWithId); countryRepository.SaveChanges(); return(Ok(mapper.Map <CountryDto>(countryWithId))); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Update error")); } }
public async Task E_Possivel_Realizar_Crud_Usuario() { await AdicionarToken(); _name = Faker.Name.First(); //Get All response = await client.GetAsync($"{hostApi}v1/Countries"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var jsonResult = await response.Content.ReadAsStringAsync(); var listaFromJson = JsonConvert.DeserializeObject <IEnumerable <CountryDtoResult> >(jsonResult); Assert.NotNull(listaFromJson); Assert.True(listaFromJson.Count() > 0); Assert.True(listaFromJson.Where(r => r.Name == "Brazil").Count() == 1); var updateCountryDto = new CountryUpdateDto() { Id = listaFromJson.Where(r => r.Name == "Brazil").FirstOrDefault().Id, Area = 15, Capital = "São Paulo", Population = 123213213, PopulationDensity = 321 }; //PUT var stringContent = new StringContent(JsonConvert.SerializeObject(updateCountryDto), Encoding.UTF8, "application/json"); response = await client.PutAsync($"{hostApi}v1/Countries", stringContent); jsonResult = await response.Content.ReadAsStringAsync(); var registroAtualizado = JsonConvert.DeserializeObject <CountryDtoResult>(jsonResult); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("Brazil", registroAtualizado.Name); Assert.Equal("São Paulo", registroAtualizado.Capital); }
public CountryTestes() { _id = Guid.NewGuid(); for (int i = 0; i < 10; i++) { var dto = new CountryDtoResult() { Id = Guid.NewGuid(), Name = Faker.Name.FullName(), Area = 100, Capital = Faker.Name.First(), OfficialLanguage = "Lingua", Population = 1111, PopulationDensity = 50 }; listaCountryDto.Add(dto); } countryDtoResult = new CountryDtoResult { Id = _id, Name = "Brasil", Area = 100, Capital = Faker.Name.First(), OfficialLanguage = "Lingua", Population = 1111, PopulationDensity = 50, }; countryUpdateDto = new CountryUpdateDto { Id = _id, Area = 100, Capital = Faker.Name.First(), Population = 1111, PopulationDensity = 50, }; }
public async Task <ActionResult> Put([FromBody] CountryUpdateDto dto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var result = await _service.Put(dto); if (result != null) { return(Ok(result)); } else { return(BadRequest()); } } catch (ArgumentException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task <IActionResult> ModifyCountry(int id, [FromBody] CountryUpdateDto country) { var countryDb = await _context.Countries.FirstOrDefaultAsync(c => c.Id == id); if (countryDb == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } countryDb.Name = country.Name; countryDb.IsCool = country.IsCool; countryDb.Description = country.Description; countryDb.BestDayToVisit = country.BestDayToVisit; countryDb.CurrencyId = country.CurrencyId; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> Update(int id, CountryUpdateDto model) { await _countryService.Update(id, model); return(NoContent()); }