public async Task <EntityResponse <ContactDto> > CreateContactAsync(AddContactDto newContactDto) { var result = ModelValidator.ValidateAsEntityValidation(newContactDto, "E0001").As <ContactDto>(); if (!result.Ok) { return(result); } bool alreadyExists = _contactRepository.AsQueryable().Any(e => e.Email == newContactDto.Email); if (alreadyExists) { return(EntityResponse.CreateError("The email already exists.", "E0004").As <ContactDto>()); } Contact contact = newContactDto.CreateEntity(); await _contactRepository.AddAsync(contact); await unitOfWork.SaveChangesAsync(); ContactDto dto = new ContactDto { Email = contact.Email, Id = contact.Id, Name = contact.Name }; return(EntityResponse.CreateOk().As(dto)); }
public async Task <EntityResponse <ContactListDto> > AddContactToListAsync(int listId, List <int> contactsId) { ContactList list = await _contactListRepository.AsQueryable().FirstOrDefaultAsync(e => e.Id == listId); if (list == null) { return(EntityResponse.CreateError("The list doesn't exists", "E0003").As <ContactListDto>()); } List <Contact> contacts = (from c in _contactRepository.AsQueryable() where contactsId.Contains(c.Id) && !c.Lists.Any(l => l.Id == listId) select c).ToList(); list.Contacts.AddRange(contacts); await unitOfWork.SaveChangesAsync(); var dto = await GetContactListAsync(listId); return(EntityResponse.CreateOk().As(dto)); }