public async Task <ApiResult> Insert(InsertAutomation request, CancellationToken cancellationToken) { var item = request.Item ?? throw new ArgumentNullException(nameof(request.Item)); item.Uid = Guid.NewGuid(); var dbConditions = CollectDbConditions(item); var dbActions = CollectDbActions(item); int affected; using (var db = _dbContextFactory.Create()) { affected = await db.GetTable <DbAutomation>() .Value(x => x.Uid, item.Uid) .Value(x => x.EntityTypeCode, request.EntityTypeCode) .Value(x => x.EntityTypeUid, request.EntityTypeUid) .Value(x => x.TypeCode, "trigger") // todo: ask user .Value(x => x.Name, item.Name) .Value(x => x.Description, item.Description) .Value(x => x.IsActive, true) .Value(x => x.IsSystem, item.System) .Value(x => x.DisplayOrder, item.DisplayOrder) .InsertAsync(cancellationToken); await db.GetTable <DbAutomationAction>().BulkCopyAsync(dbActions, cancellationToken); await db.GetTable <DbAutomationCondition>().BulkCopyAsync(dbConditions, cancellationToken); } return(new ApiResult { Uid = item.Uid, AffectedRows = affected }); }
public async Task Handle_NormalValues_InsertAutomation() { // 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 InsertAutomationHandler(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" } } } }; // act var command = new InsertAutomation { EntityTypeCode = generator.EntityTypeCode, EntityTypeUid = generator.EntityTypeUid, Item = automation }; var result = await handler.Handle(command, cancellationToken); // assert Assert.IsNotNull(result); Assert.IsTrue(result.Success); Assert.AreEqual(1, result.AffectedRows); Assert.IsNotNull(result.Uid); Assert.AreNotEqual(Guid.Empty, result.Uid); // ReSharper disable once PossibleInvalidOperationException var inserted = await generator.GetAutomation(result.Uid.Value, cancellationToken); Assert.AreEqual(1, inserted.Actions.Count); var actions = inserted.Actions.OfType <NotifyByEmailAutomationAction>().ToList(); Assert.IsNotNull(actions.FirstOrDefault(x => x.Props.Subject.Contains("#1"))); Assert.AreEqual(1, inserted.Conditions.Count); var conditions = inserted.Conditions.OfType <FieldAutomationCondition>().ToList(); Assert.IsNotNull(conditions.FirstOrDefault(x => x.Props.Field.Contains("Status"))); } }
public async Task <ApiResult> Insert(InsertAutomation request) { return(await _mediator.Send(request)); }