Exemplo n.º 1
0
        public async Task <IActionResult> AddContact(int id, ContactForAddDto contactForAddDto)
        {
            Contact contact = _mapper.Map <Contact>(contactForAddDto);
            Project project = await _projectService.GetProject(id);

            if (project.Contacts.Count() > 0)
            {
                project.Contacts.Add(new ProjectContact {
                    Contact = contact
                });
            }
            else
            {
                project.Contacts = new List <ProjectContact> {
                    new ProjectContact {
                        Contact = contact
                    }
                };
            }


            //project.Contacts.Add(contact);

            if (await _projectService.Update(project))
            {
                return(RedirectToAction("List", "Projects"));
            }

            throw new Exception($"Updating project {id} failed on save");
        }
        public ContactTest()
        {
            _contactService = new DataService <Contact>();
            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperProfiles());
            });

            _mapper = mockMapper.CreateMapper();



            _contactForAddDto = new ContactForAddDto
            {
                Name    = "Test Contact",
                Address = "1 See Stree MedowBank College",
                Details = "This is the testing details for ContactDto"
            };
            _contactForUpdateDto = new ContactForUpdateDto
            {
                Name    = "Updating The Contact for testing",
                Address = "Updated Address 1 See Stree MedowBank College",
                Details = "This is the testing checking for updating the details",
            };
        }
        public async Task <IActionResult> Add(ContactForAddDto contactForAddDto)
        {
            Contact contact = _mapper.Map <Contact>(contactForAddDto);

            var contactData = await _contactService.Add(contact);

            var contactView = _mapper.Map <ContactForViewDto>(contactData);

            return(Ok(contactView));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateContact(ContactForAddDto contactForAddDto)
        {
            var contact = _mapper.Map <Contact>(contactForAddDto);

            // check if phone number already exist
            var phoneExists = await _contactRepository.TelephoneNumberExists(contactForAddDto.TelephoneNumber);

            if (phoneExists)
            {
                return(BadRequest("Phone already exist"));
            }

            _contactRepository.Add(contact);

            if (await _contactRepository.SaveAll())
            {
                var contactToReturn = _mapper.Map <ContactForListDto>(contact);
                return(CreatedAtRoute("GetContact", new { id = contact.Id }, contactToReturn));
            }

            throw new Exception("Creating the contact failed on save");
        }