Ejemplo n.º 1
0
        public async Task Create_BadRequest_ListDoesntExist()
        {
            //Arrange
            int nonExistingList = 10;

            var contactDTO = new CreateContactDTO()
            {
                FirstName = "test",
                LastName  = "test",
                Email     = "*****@*****.**",
                Active    = true,
                ListIds   = new List <int>()
                {
                    nonExistingList
                }
            };

            //Act
            var controller   = new ContactController(_serviceBusMock.Object, _contactContext.Object);
            var actionResult = await controller.Create(contactDTO);

            //Assert
            var result = Assert.IsType <BadRequestResult>(actionResult);

            Assert.Equal((int)System.Net.HttpStatusCode.BadRequest, result.StatusCode);
        }
Ejemplo n.º 2
0
        public async Task Create_Created()
        {
            //Arrange
            var firstName = "Elizabeth";
            var lastName  = "Johnson";
            var email     = "*****@*****.**";
            var phone     = "0635555";
            var active    = true;

            var contactDTO = new CreateContactDTO()
            {
                FirstName = firstName,
                LastName  = lastName,
                Email     = email,
                Phone     = phone,
                Active    = active
            };

            //Act
            var contactController = new ContactController(_serviceBusMock.Object, _contactContext.Object);
            var actionResult      = await contactController.Create(contactDTO);

            //Assert
            var result = Assert.IsType <CreatedAtActionResult>(actionResult);

            Assert.Equal((int)System.Net.HttpStatusCode.Created, result.StatusCode);
            var returnValue = Assert.IsType <int>(result.Value);
        }
Ejemplo n.º 3
0
        public async Task <ContactDTO> CreateContactAsync(CreateContactDTO createContactDTO)
        {
            var contactEntity = _mapper.Map <ContactEntity>(createContactDTO);

            await _dBContext.Contacts.AddAsync(contactEntity);

            await _dBContext.SaveChangesAsync();

            var contactDTO = _mapper.Map <ContactDTO>(contactEntity);

            return(contactDTO);
        }
Ejemplo n.º 4
0
        public async Task <Guid> Create(CreateContactDTO contact)
        {
            Contact entity = contact.ToEntity();
            bool    res    = await _contactEntity.AddAsync(entity);

            if (contact.Address.Any())
            {
                res = await _addressEntity.AddRangeAsync(contact.GetAddress());
            }

            return(entity.ID);
        }
Ejemplo n.º 5
0
 public Contact?Convert(CreateContactDTO model, int personId)
 {
     return(model == null
         ? null
         : new Contact
     {
         Email = model.Email,
         Number = model.Number,
         ContactTypeId = model.ContactTypeId,
         PersonId = personId
     });
 }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create(CreateContactDTO model)
        {
            //if contact with given email already exists, then add modal error
            var contactEmail = await _contactContext.Contacts.FirstOrDefaultAsync(c => c.Email == model.Email);

            if (contactEmail != null)
            {
                ModelState.AddModelError("Email", "Contact with given e-mail already exists");
                return(BadRequest(ModelState));
            }

            var contact = new Models.Contact()
            {
                FirstName = model.FirstName,
                LastName  = model.LastName,
                Email     = model.Email,
                Phone     = model.Phone,
                Active    = model.Active
            };

            //add lists to contact
            if (model.ListIds != null)
            {
                //if list with passed id doesn't exist, then return BadRequest()
                var listsExist = _contactContext.Lists.Select(l => l.Id).ContainsAllItems(model.ListIds);
                if (!listsExist)
                {
                    return(BadRequest());
                }

                contact.ContactLists = new List <ContactList>();
                foreach (var listId in model.ListIds)
                {
                    contact.ContactLists.Add(new ContactList {
                        FContact = contact.Id, FList = listId
                    });
                }
            }

            try
            {
                await _contactContext.Contacts.AddAsync(contact);

                await _contactContext.SaveChangesAsync();

                return(CreatedAtAction(nameof(Create), contact.Id));
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> CreateContact([FromBody] CreateContactDTO contactDTO)
        {
            try
            {
                await _contactService.Create(contactDTO);

                return(NoContent());
            }
            catch (System.Exception ex)
            {
                return(BadRequest(ErrorDTO.Create(ex.Message)));
            }
        }
        public async Task <IActionResult> CreateStudent([FromBody] CreateContactDTO obj)
        {
            var response = await _contact.CreateContact(obj);

            if (response.statusCode == 400)
            {
                return(BadRequest(response));
            }
            else if (response.statusCode == 401)
            {
                return(Unauthorized(response));
            }

            return(Ok(response));
        }
Ejemplo n.º 9
0
        public async Task Create_BadRequest_InvalidModel()
        {
            //Arrange
            var emailThatAlreadyExists = "*****@*****.**";

            var model = new CreateContactDTO()
            {
                FirstName = "test",
                LastName  = "test",
                Email     = emailThatAlreadyExists,
                Active    = true
            };

            //Act
            var controller   = new ContactController(_serviceBusMock.Object, _contactContext.Object);
            var actionResult = await controller.Create(model);

            //Assert
            var result = Assert.IsType <BadRequestObjectResult>(actionResult);
        }
Ejemplo n.º 10
0
 public async Task Create(CreateContactDTO createContact)
 {
     _dbContext.Contacts.Add(createContact.ToContact());
     await _dbContext.SaveChangesAsync();
 }