public async Task AddAsync(Rule rule, CancellationToken cancellationToken) { if (rule == null) { throw new ArgumentNullException(nameof(rule)); } using (var db = this.db.CreateDbContext()) { var entity = new EntityModel() { Id = rule.Id, CallbackId = rule.Callback?.Callback.Id, PropertyId = rule.Property.Value, AddressReservationId = rule.AddressReservation.Id, TargetAmount = rule.TargetAmount.Indivisible, TargetConfirmation = rule.TargetConfirmation, OriginalTimeout = rule.OriginalTimeout, CurrentTimeout = rule.OriginalTimeout, TimeoutStatus = rule.Callback?.TimeoutStatus, Status = Status.Uncompleted, }; await db.TokenReceivingWatcherRules.AddAsync(entity, cancellationToken); await db.SaveChangesAsync(); } }
async Task StoreRuleAsync(Rule rule) { var address = new Ztm.Data.Entity.Contexts.Main.ReceivingAddress() { Id = rule.AddressReservation.Address.Id, Address = rule.AddressReservation.Address.Address.ToString(), IsLocked = rule.AddressReservation.Address.IsLocked, }; var reservation = new Data.Entity.Contexts.Main.ReceivingAddressReservation() { Id = rule.AddressReservation.Id, AddressId = rule.AddressReservation.Address.Id, LockedAt = rule.AddressReservation.ReservedDate, ReleasedAt = rule.AddressReservation.ReleasedDate, }; var entity = new Ztm.Data.Entity.Contexts.Main.TokenReceivingWatcherRule() { Id = rule.Id, CallbackId = rule.Callback?.Callback.Id, PropertyId = rule.Property.Value, AddressReservationId = rule.AddressReservation.Id, TargetAmount = rule.TargetAmount.Indivisible, TargetConfirmation = rule.TargetConfirmation, OriginalTimeout = rule.OriginalTimeout, CurrentTimeout = rule.OriginalTimeout, TimeoutStatus = rule.Callback?.TimeoutStatus, Status = Ztm.Data.Entity.Contexts.Main.TokenReceivingWatcherRuleStatus.Uncompleted, }; using (var db = this.db.CreateDbContext()) { if (rule.Callback != null) { var callback = rule.Callback.Callback; await db.WebApiCallbacks.AddAsync(new Ztm.Data.Entity.Contexts.Main.WebApiCallback() { Id = callback.Id, RegisteredIp = callback.RegisteredIp, RegisteredTime = callback.RegisteredTime, Completed = callback.Completed, Url = callback.Url, }); } await db.ReceivingAddresses.AddAsync(address); await db.ReceivingAddressReservations.AddAsync(reservation); await db.TokenReceivingWatcherRules.AddAsync(entity); await db.SaveChangesAsync(); } }
async Task <Rule> ToDomainAsync(EntityModel entity, CancellationToken cancellationToken) { var reservation = await this.addresses.GetReservationAsync(entity.AddressReservationId, cancellationToken); var callback = entity.CallbackId != null ? await this.callbacks.GetAsync(entity.CallbackId.Value, cancellationToken) : null; return(new Rule( new PropertyId(entity.PropertyId), reservation, new PropertyAmount(entity.TargetAmount), entity.TargetConfirmation, entity.OriginalTimeout, callback != null ? new TokenReceivingCallback(callback, entity.TimeoutStatus) : null, entity.Id)); }
async Task <Rule> ToDomainAsync(EntityModel entity, CancellationToken cancellationToken) { var reservation = this.addresses.GetReservationAsync(entity.AddressReservationId, cancellationToken); var callback = this.callbacks.GetAsync(entity.CallbackId, cancellationToken); await Task.WhenAll(reservation, callback); return(new Rule( new PropertyId(entity.PropertyId), reservation.Result, new PropertyAmount(entity.TargetAmount), entity.TargetConfirmation, entity.OriginalTimeout, entity.TimeoutStatus, callback.Result, entity.Id)); }