public async Task <ApiResult> Update(UpdateAutomation request, CancellationToken cancellationToken) { var item = request.Item ?? throw new ArgumentNullException(nameof(request.Item)); var dbConditions = CollectDbConditions(item); var dbActions = CollectDbActions(item); int affected; using (var db = _dbContextFactory.Create()) { affected = await db.GetTable <DbAutomation>() .Where(x => x.EntityTypeCode == request.EntityTypeCode && x.EntityTypeUid == request.EntityTypeUid && x.Uid == item.Uid) .Set(x => x.Name, item.Name) .Set(x => x.Description, item.Description) .Set(x => x.DisplayOrder, item.DisplayOrder) .UpdateAsync(cancellationToken); await db.GetTable <DbAutomationAction>() .Where(x => x.AutomationUid == item.Uid).DeleteAsync(cancellationToken); await db.GetTable <DbAutomationAction>().BulkCopyAsync(dbActions, cancellationToken); await db.GetTable <DbAutomationCondition>() .Where(x => x.AutomationUid == item.Uid).DeleteAsync(cancellationToken); await db.GetTable <DbAutomationCondition>().BulkCopyAsync(dbConditions, cancellationToken); } return(new ApiResult { AffectedRows = affected }); }
public async Task Handle_NormalValues_UpdateAutomation() { // 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 UpdateAutomationHandler(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" } } } }; var inserted = await generator.InsertAutomation(automation, cancellationToken); // act // ReSharper disable once PossibleInvalidOperationException automation.Uid = inserted.Uid.Value; automation.Actions.Add(new NotifyByEmailAutomationAction { Props = new NotifyByEmailAutomationAction.Properties { Recipient = "requester", Subject = "Test message #2", Body = "Hello" } }); var command = new UpdateAutomation { 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); var updated = await generator.GetAutomation(inserted.Uid.Value, cancellationToken); Assert.AreEqual(2, updated.Actions.Count); var actions = updated.Actions.OfType <NotifyByEmailAutomationAction>().ToList(); Assert.IsNotNull(actions.FirstOrDefault(x => x.Props.Subject.Contains("#1"))); Assert.IsNotNull(actions.FirstOrDefault(x => x.Props.Subject.Contains("#2"))); Assert.AreEqual(1, updated.Conditions.Count); var conditions = updated.Conditions.OfType <FieldAutomationCondition>().ToList(); Assert.IsNotNull(conditions.FirstOrDefault(x => x.Props.Field.Contains("Status"))); } }
public async Task <ApiResult> Update(UpdateAutomation request) { return(await _mediator.Send(request)); }