Example #1
0
        public async Task <ActionResult <ContactReadDto> > CreateContact(ContactCreateDto createContactDto)
        {
            try
            {
                if (createContactDto == null)
                {
                    return(BadRequest());
                }
                createContactDto.UpdatedDate = DateTime.UtcNow;

                Contact contact = _mapper.Map <Contact>(createContactDto);
                await _addressBookService.CreateContact(contact);

                await _addressBookService.SaveAsync();

                Contact recentlyAddedContact = await _addressBookService.FindAsync(contact.ContactDetails.First().ContactId);

                ContactReadDto contactReadDto = _mapper.Map <ContactReadDto>(recentlyAddedContact);

                return(CreatedAtAction(nameof(GetContactById), new { id = contactReadDto.ContactId }, contactReadDto));
            }
            catch (DbUpdateException ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #2
0
        public async Task Post([FromBody] Contact contact)
        {
            //using filter to validate model state

            addressBookService.CreateContact(contact);

            addressBookDbContext.SaveChanges();

            await addressBookBroadcaster.BroadcastContactsChanged(contact);
        }