Esempio n. 1
0
        public async Task <ApiResult> Delete(DeleteAutomation request, CancellationToken cancellationToken)
        {
            int affected;

            using (var db = _dbContextFactory.Create())
            {
                await db.GetTable <DbAutomationAction>()
                .Where(x => request.Uids.Contains(x.AutomationUid))
                .DeleteAsync(cancellationToken);

                await db.GetTable <DbAutomationCondition>()
                .Where(x => request.Uids.Contains(x.AutomationUid))
                .DeleteAsync(cancellationToken);

                affected = await db.GetTable <DbAutomation>()
                           .Where(x => x.EntityTypeCode == request.EntityTypeCode &&
                                  x.EntityTypeUid == request.EntityTypeUid &&
                                  request.Uids.Contains(x.Uid))
                           .DeleteAsync(cancellationToken);
            }

            return(new ApiResult {
                AffectedRows = affected
            });
        }
Esempio n. 2
0
 public async Task <ApiResult> Delete(DeleteAutomation request)
 {
     return(await _mediator.Send(request));
 }
        public async Task Handle_NormalValues_DeleteAutomation()
        {
            // arrange
            var cancellationToken = new CancellationToken();
            var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory  = new DefaultDbContextFactory();
            var jsonSerializer    = new NewtonsoftJsonSerializer();
            var automationService = new DefaultAutomationService(dbContextFactory, jsonSerializer);
            var generator         = new AutomateDbGenerator(dbContextFactory);

            var handler = new DeleteAutomationHandler(unitOfWorkFactory, automationService);

            using (var _ = unitOfWorkFactory.Create())
            {
                // arrange
                var automation = new Automation
                {
                    Conditions = new List <AutomationCondition>
                    {
                        new FieldAutomationCondition
                        {
                            Props = new FieldAutomationCondition.Properties
                            {
                                Field    = "Status",
                                Operator = AutomationConditionOperator.Equal,
                                Value    = "Published"
                            }
                        }
                    },
                    Actions = new List <AutomationAction>
                    {
                        new NotifyByEmailAutomationAction
                        {
                            Props = new NotifyByEmailAutomationAction.Properties
                            {
                                Recipient = "operator",
                                Subject   = "Test message #1",
                                Body      = "Hello"
                            }
                        }
                    }
                };

                await generator.InsertAutomation(automation, cancellationToken);

                // act
                var command = new DeleteAutomation
                {
                    EntityTypeCode = generator.EntityTypeCode,
                    EntityTypeUid  = generator.EntityTypeUid,
                    Uids           = new [] { automation.Uid }
                };

                var result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Success);
                Assert.AreEqual(1, result.AffectedRows);

                await Assert.ThrowsExceptionAsync <InvalidOperationException>(
                    () => generator.GetAutomation(automation.Uid, cancellationToken));
            }
        }