示例#1
0
        public async Task <ActionResult <ContactDTO> > CreateContact([FromBody] ContactCreateDTO contactCreateDTO)
        {
            CreateContactCommand command = mapper.Map <CreateContactCommand>(contactCreateDTO);

            ContactDTO contact = await mediator.Send(command);

            return(CreatedAtAction("GetContactById", new { id = contact.Id }, contact));
        }
示例#2
0
        public async Task <IActionResult> PutContact(int id, ContactCreateDTO contactCreateDTO)
        {
            if (!ContactExists(id))
            {
                return(BadRequest());
            }

            var contactType = ContactTypeExists(contactCreateDTO.ContactType);

            if (contactType == null)
            {
                return(BadRequest());
            }

            var contact = new Contact
            {
                Id          = id,
                Name        = contactCreateDTO.Name,
                Birthdate   = contactCreateDTO.Birthdate,
                ContactType = contactCreateDTO.ContactType,
                Phone       = contactCreateDTO.Phone,
                UpdatedAt   = DateTime.UtcNow
            };

            _context.Entry(contact).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ContactExists(id))
                {
                    return(NotFound());
                }
            }

            return(NoContent());
        }
示例#3
0
        public async Task <ActionResult <Contact> > PostContact(ContactCreateDTO contactCreateDTO)
        {
            var contactType = ContactTypeExists(contactCreateDTO.ContactType);

            if (contactType == null)
            {
                return(BadRequest());
            }

            var contact = new Contact
            {
                Name        = contactCreateDTO.Name,
                Birthdate   = contactCreateDTO.Birthdate,
                Phone       = contactCreateDTO.Phone,
                ContactType = contactType.Description,
            };

            _context.Contacts.Add(contact);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
示例#4
0
        public async Task <bool> ContactCreate(ContactCreateDTO DTO)
        {
            var user = await _authRepository.GetUserById(DTO.OwnerId);

            if (DTO.OwnerId != user.Id)
            {
                throw new Exception("Unauthorized"); //TODO: Adjustments
            }
            var rao = _mapper.Map <ContactCreateRAO>(DTO);

            rao.OwnerId = DTO.OwnerId;

            if (await _contactRepository.CreateContact(rao))
            {
                await _roleRepository.AddUserToRole(user, "user");

                return(true);
            }
            else
            {
                return(false);
            }
        }