예제 #1
0
        public void Campaign_Create_ValidateEntity()
        {
            var newCampaignDto = new AddCampaignDto()
            {
                Name            = "New 123",
                EmailTemplateId = 1,
                Events          = new List <AddEventDto>
                {
                    new AddEventDto
                    {
                        Date = DateTime.Now.AddDays(5)
                    }
                },
                ContactLists = new List <int>
                {
                    1
                }
            };

            var result = newCampaignDto.CreateEntity();

            Assert.AreEqual(expected: newCampaignDto.Events.Count, result.Events.Count);
            Assert.AreEqual(expected: newCampaignDto.Name, result.Name);
            Assert.AreEqual(expected: newCampaignDto.EmailTemplateId, result.EmailTemplateId);
        }
        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));
        }