Beispiel #1
0
        public async Task <ActionResult <ContactReadDto> > GetContactAndContactDetails(int contactId, int contactDetailId)
        {
            try
            {
                bool contactExists = await _contactBookService.ContactExists(contactId);

                if (!contactExists)
                {
                    return(NotFound());
                }

                ContactDetail contactDetailFromService = await _contactBookService.GetContactDetail(contactId, contactDetailId);

                if (contactDetailFromService == null)
                {
                    return(NotFound());
                }

                ContactReadDto contactReadDto = _mapper.Map <ContactReadDto>(contactDetailFromService.Contact);

                return(Ok(contactReadDto));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        public async Task UpdateContact_InConsistentContactId_BadRequestResult()
        {
            ContactReadDto contactForUpdate = new ContactReadDto()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetailReadDto>()
                {
                    new ContactDetailReadDto()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            var mapperMock    = new Mock <IMapper>();
            var serviceMocker = new Mock <IContactBookService>();

            var controller   = new ContactsController(contactBookService: serviceMocker.Object, mapper: mapperMock.Object);
            var actionResult = await controller.UpdateContact(2, contactForUpdate);

            Assert.IsAssignableFrom <BadRequestResult>(actionResult.Result);
        }
Beispiel #3
0
        public async Task <ActionResult <ContactReadDto> > UpdateContact(int id, ContactReadDto contactReadDto)
        {
            try
            {
                if (id != contactReadDto.ContactId)
                {
                    return(BadRequest());
                }

                bool contactExists = await _contactBookService.ContactExists(id);

                if (!contactExists)
                {
                    return(BadRequest());
                }
                contactReadDto.UpdatedDate = DateTime.UtcNow;
                Contact contact = _mapper.Map <Contact>(contactReadDto);

                _contactBookService.UpdateContact(contact);
                await _contactBookService.SaveAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Beispiel #4
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 _contactBookService.CreateContact(contact);

                await _contactBookService.SaveAsync();

                Contact recentlyAddedContact = await _contactBookService.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));
            }
        }
Beispiel #5
0
        public async Task <ActionResult <ContactReadDto> > GetContactById(int id)
        {
            try
            {
                Contact contactFromService = await _contactBookService.GetContactById(id);

                if (contactFromService == null)
                {
                    return(NotFound());
                }

                ContactReadDto contactReadDto = _mapper.Map <ContactReadDto>(contactFromService);
                return(Ok(contactReadDto));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        public async Task UpdateContact_Successful_NoContentResult()
        {
            Contact updatedContact = new Contact()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetail>()
                {
                    new ContactDetail()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            ContactReadDto contactForUpdate = new ContactReadDto()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetailReadDto>()
                {
                    new ContactDetailReadDto()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            var mapperMock = new Mock <IMapper>();

            mapperMock
            .Setup(mapper => mapper.Map <Contact>(It.IsAny <ContactReadDto>()))
            .Returns(updatedContact);

            var serviceMocker = new Mock <IContactBookService>();

            serviceMocker
            .Setup(service => service.ContactExists(It.IsAny <int>()))
            .ReturnsAsync(true);
            serviceMocker
            .Setup(service => service.UpdateContact(It.IsAny <Contact>()));
            serviceMocker
            .Setup(service => service.SaveAsync());

            var controller   = new ContactsController(contactBookService: serviceMocker.Object, mapper: mapperMock.Object);
            var actionResult = await controller.UpdateContact(30, contactForUpdate);

            Assert.IsAssignableFrom <NoContentResult>(actionResult.Result);
        }
        public async Task CreateContact_Successful_NotNull()
        {
            ContactCreateDto newContact = new ContactCreateDto()
            {
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetailCreateDto>()
                {
                    new ContactDetailCreateDto()
                    {
                        Address     = "302 Lebea",
                        Cell        = "0782347832",
                        Description = "Description",
                        Email       = "*****@*****.**",
                        Telephone   = "0156728912"
                    }
                }
            };

            Contact contact = new Contact()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetail>()
                {
                    new ContactDetail()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            ContactReadDto returnedContact = new ContactReadDto()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetailReadDto>()
                {
                    new ContactDetailReadDto()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            var mapperMock = new Mock <IMapper>();

            mapperMock
            .Setup(mapper => mapper.Map <Contact>(It.IsAny <ContactCreateDto>()))
            .Returns(contact);

            mapperMock
            .Setup(mapper => mapper.Map <ContactReadDto>(It.IsAny <Contact>()))
            .Returns(returnedContact);

            var serviceMocker = new Mock <IContactBookService>();

            serviceMocker
            .Setup(service => service.CreateContact(contact));

            serviceMocker
            .Setup(service => service.SaveAsync());

            var controller   = new ContactsController(contactBookService: serviceMocker.Object, mapper: mapperMock.Object);
            var actionResult = await controller.CreateContact(newContact);

            var result = actionResult.Result as CreatedAtActionResult;

            Assert.NotNull(result.Value);
            Assert.Equal(returnedContact, result.Value);
        }
        public async Task GetContactAndContactDetail_FoundMatchingContactId_NotNull()
        {
            //What's in memory
            Contact contactFromService = new Contact()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetail>()
                {
                    new ContactDetail()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            //What is expected
            ContactReadDto returnedContact = new ContactReadDto()
            {
                ContactId   = 30,
                FirstName   = "Selby",
                Surname     = "Seroka",
                BirthDate   = new DateTime(1978, 7, 23),
                UpdatedDate = new DateTime(2020, 11, 2),

                ContactDetails = new List <ContactDetailReadDto>()
                {
                    new ContactDetailReadDto()
                    {
                        ContactDetailId = 40,
                        ContactId       = 30,
                        Address         = "302 Lebea",
                        Cell            = "0782347832",
                        Description     = "Description",
                        Email           = "*****@*****.**",
                        Telephone       = "0156728912"
                    }
                }
            };

            var serviceMocker = new Mock <IContactBookService>();

            serviceMocker
            .Setup(service => service.ContactExists(30))
            .ReturnsAsync(true);

            serviceMocker
            .Setup(service => service.GetContactDetail(30, 40))
            .ReturnsAsync(contactFromService.ContactDetails.First());

            var mapperMock = new Mock <IMapper>();

            mapperMock
            .Setup(mapper => mapper.Map <ContactReadDto>(It.IsAny <ContactReadDto>()))
            .Returns(returnedContact);

            var controller   = new ContactsController(contactBookService: serviceMocker.Object, mapper: mapperMock.Object);
            var actionResult = await controller.GetContactAndContactDetails(30, 40);

            var result = actionResult.Result as OkObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(returnedContact, result.Value);
        }