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)); }
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()); }
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()); }
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); } }