示例#1
0
        public async Task <IActionResult> Get([FromQuery] int pageNr)
        {
            if (pageNr < 1)
            {
                return(BadRequest("Invalid data on input"));
            }
            IEnumerable <ContactDto> contactsDto = null;

            try
            {
                contactsDto = (await _service
                               .GetAddressBookAsync(pageNr))
                              .Contacts
                              .Select(c => ContactDto.Create(c));
            }
            catch
            {
                return(BadRequest());
            }
            return(Ok(contactsDto));
        }
示例#2
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 }));
        }
示例#3
0
        public async Task <IActionResult> Get([FromRoute] string id)
        {
            if (!Guid.TryParse(id, out var contactId))
            {
                return(BadRequest("Id not in proper format"));
            }
            ContactDto contactDto = null;

            try
            {
                contactDto = (await _service
                              .GetAddressBookForContactAsync(contactId))
                             .Contacts
                             .Select(c => ContactDto.Create(c))
                             .SingleOrDefault();
            }
            catch
            {
                return(BadRequest());
            }
            return(contactDto == null ? (IActionResult)NotFound() : Ok(contactDto));
        }
示例#4
0
        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());
        }
        private void PublishContactPersistedEvent(Contact contact, bool isNew)
        {
            ContactDto dto = ContactDto.Create(contact.Id, contact.Name);

            DomainEvents.Raise(new ContactUpdatedEvent(dto, isNew));
        }