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 <CampaignDto> > CreateCampaignAsync(AddCampaignDto newCampaignDto) { var modelValidationResult = ModelValidator.ValidateAsEntityValidation(newCampaignDto, "ECACC-01"); if (!modelValidationResult.Ok) { return(modelValidationResult.As <CampaignDto>()); } Campaign campaign = newCampaignDto.CreateEntity(); List <ContactList> lists = _unitOfWork.GetRepository <ContactList>().AsQueryable().Where(c => newCampaignDto.ContactLists.Contains(c.Id)).ToList(); campaign.AddList(lists); await _unitOfWork.BeginTransactionAsync(); await _unitOfWork.GetRepository <Campaign>().AddAsync(campaign); await _unitOfWork.SaveChangesAsync(); campaign.Events.ForEach(e => { e.ScheduleJobId = _backgroundJobManager.Schedule <EmailCampaignSender>(s => s.SendCampaign(campaign.Id), e.Hour); }); await _unitOfWork.SaveChangesAsync(); _unitOfWork.Commit(); var dto = (from c in _unitOfWork.GetRepository <Campaign>().AsQueryable() where c.Id == campaign.Id select new CampaignDto { Id = c.Id, Name = c.Name, Events = (from e in c.Events select new EventDto { ScheduleJobId = e.ScheduleJobId, Date = e.Date, Hour = e.Hour }).ToList(), ContactLists = (from l in c.ContactLists select new ContactListDto { Id = l.Id, Name = l.Name, }).ToList() }).First(); return(EntityResponse.CreateOk().As(dto)); }
public async Task <EntityResponse <ContactListDto> > CreateContactListAsync(AddContactListDto newContactListDto) { var result = ModelValidator.ValidateAsEntityValidation(newContactListDto, "E0002").As <ContactListDto>(); if (!result.Ok) { return(result); } List <Contact> contacts = await _contactRepository.AsQueryable().Where(e => newContactListDto.ContactIds.Contains(e.Id)).ToListAsync(); ContactList list = new ContactList { Name = newContactListDto.Name, Contacts = contacts }; await unitOfWork.GetRepository <ContactList>().AddAsync(list); await unitOfWork.SaveChangesAsync(); return(EntityResponse.CreateOk().As(mapper.Map <ContactListDto>(list))); }