public IActionResult UpdateCustomer(int id, [FromBody] CustomerToUpdateDto customerToUpdate) { if (customerToUpdate == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var customerFromRepo = _repo.GetCustomer(id); if (customerFromRepo == null) { return(NotFound()); } try { Mapper.Map(customerToUpdate, customerFromRepo); _repo.UpdateCustomer(customerFromRepo); _repo.Save(); } catch (Exception ex) { throw new Exception(ex.Message); } return(NoContent()); }
public async Task <IActionResult> UpdateCustomer(int id, [FromBody] CustomerToUpdateDto customerToUpdate) { if (customerToUpdate == null) { return(BadRequest()); } //basically dont wanna have to worry about deserializing 2 possible types of response object, so just gonna do the validation here if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } UriBuilder uriBuilder = new UriBuilder(_client.BaseAddress); uriBuilder.Path += "/" + id; MemoryStream customerProtoStream = new MemoryStream(); Serializer.Serialize(customerProtoStream, customerToUpdate); ByteArrayContent bArray = new ByteArrayContent(customerProtoStream.ToArray()); var customerResponse = await _client.PutAsync(uriBuilder.Uri, bArray); return(StatusCode((int)customerResponse.StatusCode)); }
public async Task <IActionResult> EditCustomer(CustomerToUpdateDto customerToUpdateDto, int id) { var customerFromRepo = await _repo.GetCustomer(id); var customerToReturn = _mapper.Map(customerToUpdateDto, customerFromRepo); await _context.SaveChangesAsync(); return(Ok(customerToReturn)); }