public async Task <IActionResult> PostAsync([FromBody] ContactDto contactDto) { Guid newId = new Guid(); contactDto.AddressBookId = (await _service.GetAddressBookIdAsync()).ToString(); Contact contact = null; contactDto.Sanitize(_sanitizer); try { contact = contactDto.Create(); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch { return(BadRequest("Invalid data on input.")); } try { newId = await _service.AddContactAsync(contact); } catch (ArgumentException ex) { return(BadRequest($"An error happened while saving new contact: {ex.Message}")); } catch { return(BadRequest("An error happened while saving new contact.")); } return(CreatedAtAction(nameof(Get), new { id = newId })); }
public async Task <IActionResult> Put([FromRoute] string id, [FromBody] ContactDto contactDto) { // contactDto.Id = id; Contact contact = null; contactDto.Sanitize(_sanitizer); try { contact = contactDto.Create(); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch { return(BadRequest("Invalid data on input.")); } try { await _service.UpdateContactAsync(contact); } catch (ArgumentException ex) { return(BadRequest($"An error happened while saving new contact: {ex.Message}.")); } catch { return(BadRequest("An error happened while saving new contact.")); } await _addressBookHub.Clients.All.SendAsync("updateContact", ContactDto.Create(contact)); return(NoContent()); }