Example #1
0
        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 }));
        }