public async Task <SyncedEntityIds> SyncEntitiesAsync(SyncEntities model) { (var conn, var uowTransaction) = _unitOfWork.StartTransaction(); var accountIds = new int[model.Accounts.Count]; var categoryIds = new int[model.Categories.Count]; var transactionIds = new int[model.Transactions.Count]; var upcomingExpenseIds = new int[model.UpcomingExpenses.Count]; var debtIds = new int[model.Debts.Count]; for (var i = 0; i < accountIds.Length; i++) { var account = _mapper.Map <Account>(model.Accounts[i]); int id = await _accountsRepository.CreateAsync(account, conn, uowTransaction); accountIds[i] = id; } for (var i = 0; i < categoryIds.Length; i++) { var category = _mapper.Map <Category>(model.Categories[i]); int id = await _categoriesRepository.CreateAsync(category, conn, uowTransaction); categoryIds[i] = id; } for (var i = 0; i < transactionIds.Length; i++) { var transaction = _mapper.Map <Transaction>(model.Transactions[i]); int id = await _transactionsRepository.CreateAsync(transaction, conn, uowTransaction); transactionIds[i] = id; } for (var i = 0; i < upcomingExpenseIds.Length; i++) { var upcomingExpense = _mapper.Map <UpcomingExpense>(model.UpcomingExpenses[i]); int id = await _upcomingExpensesRepository.CreateAsync(upcomingExpense, conn, uowTransaction); upcomingExpenseIds[i] = id; } for (var i = 0; i < debtIds.Length; i++) { var debt = _mapper.Map <Debt>(model.Debts[i]); int id = await _debtsRepository.CreateAsync(debt, conn, uowTransaction); debtIds[i] = id; } _unitOfWork.CommitTransaction(conn, uowTransaction); return(new SyncedEntityIds(accountIds, categoryIds, transactionIds, upcomingExpenseIds, debtIds)); }
private void RemoveEntity([NotNull] IEntityHandle ent) { if (ent == null) { throw new ArgumentNullException(nameof(ent)); } Debug.Assert(SyncEntities.Contains(ent)); Debug.Assert(!InitEntities.Contains(ent)); LeaveEntities.Add(ent); }
public async Task <IActionResult> CreateEntities([FromBody] SyncEntities dto) { if (dto == null) { return(BadRequest()); } int userId; try { userId = IdentityHelper.GetUserId(User); } catch (UnauthorizedAccessException) { return(Unauthorized()); } dto.Accounts.ForEach(x => { x.UserId = userId; }); dto.Categories.ForEach(x => { x.UserId = userId; }); dto.Transactions.ForEach(x => { x.UserId = userId; }); dto.UpcomingExpenses.ForEach(x => { x.UserId = userId; }); dto.Debts.ForEach(x => { x.UserId = userId; }); var syncedEntityIds = await _syncService.SyncEntitiesAsync(dto); var createdEntitiesVm = new CreatedEntityIdsVm(); for (var i = 0; i < syncedEntityIds.AccountIds.Length; i++) { createdEntitiesVm.AccountIdPairs.Add(new CreatedEntityIdPair(dto.Accounts[i].Id, syncedEntityIds.AccountIds[i])); } for (var i = 0; i < syncedEntityIds.CategoryIds.Length; i++) { createdEntitiesVm.CategoryIdPairs.Add(new CreatedEntityIdPair(dto.Categories[i].Id, syncedEntityIds.CategoryIds[i])); } for (var i = 0; i < syncedEntityIds.TransactionIds.Length; i++) { createdEntitiesVm.TransactionIdPairs.Add(new CreatedEntityIdPair(dto.Transactions[i].Id, syncedEntityIds.TransactionIds[i])); } for (var i = 0; i < syncedEntityIds.UpcomingExpenseIds.Length; i++) { createdEntitiesVm.UpcomingExpenseIdPairs.Add(new CreatedEntityIdPair(dto.UpcomingExpenses[i].Id, syncedEntityIds.UpcomingExpenseIds[i])); } for (var i = 0; i < syncedEntityIds.DebtIds.Length; i++) { createdEntitiesVm.DebtIdPairs.Add(new CreatedEntityIdPair(dto.Debts[i].Id, syncedEntityIds.DebtIds[i])); } return(StatusCode(201, createdEntitiesVm)); }
public static string ToString(SyncEntities entities) { var builder = new StringBuilder() .AppendLine("NetworkSyncDataEntityContainers: {"); foreach (var entity in entities.Entities) { builder.AppendLine(string.Format("{0}NetworkSyncEntity: ", new String(' ', tab1)) + "{") .AppendLine(string.Format("{0}NetworkId: {1}", new String(' ', tab2), entity.Id.NetworkId)) .AppendLine(string.Format("{0}ActorId: {1}", new String(' ', tab2), entity.Id.ActorId)) .AppendLine(string.Format("{0}", new String(' ', tab1)) + "}") .Append(string.Format("{0}AddedComponents: [ ", new String(' ', tab1))); if (entity.AddedComponents.Any()) { builder.AppendLine(); } foreach (var component in entity.AddedComponents) { builder.AppendLine(string.Format("{0}componentDataContainer: ", new String(' ', tab2)) + "{") .AppendLine(string.Format("{0}ComponentTypeId: {1}", new String(' ', tab3), component.TypeId)) .AppendLine(string.Format("{0}MemberData: [ {1} ]", new String(' ', tab3), string.Join(", ", component.Fields.Select(x => x.Value)))) .AppendLine(string.Format("{0}", new String(' ', tab2))); } if (entity.AddedComponents.Any()) { builder.Append(new String(' ', tab1)); } builder.AppendLine("]") .AppendLine(string.Format("{0}RemovedComponents: [ {1} ]", new String(' ', tab1), string.Join(", ", entity.RemovedComponents))) .Append(string.Format("{0}ComponentData: [ ", new String(' ', tab1))); if (entity.Components.Any()) { builder.AppendLine(); } foreach (var component in entity.Components) { builder.AppendLine(string.Format("{0}ComponentDataContainer: ", new String(' ', tab2)) + "{"); builder.AppendLine(string.Format("{0}ComponentTypeId: {1}", new String(' ', tab3), component.TypeId)); builder.AppendLine(string.Format("{0}MemberData: [ {1} ]", new String(' ', tab3), string.Join(", ", component.Fields.Select(x => x.Value)))); builder.AppendLine(string.Format("{0}", new String(' ', tab2)) + "}"); } if (entity.Components.Any()) { builder.Append(new String(' ', tab1)); } builder.AppendLine("]"); } builder.AppendLine("}") .AppendLine() .AppendLine("AddedNetworkSyncEntities: {"); foreach (var entity in entities.Added) { builder.AppendLine(string.Format("{0}NetworkEntityData: ", new String(' ', tab1)) + "{") .AppendLine(string.Format("{0}NetworkSyncEntity: ", new String(' ', tab2)) + "{") .AppendLine(string.Format("{0}NetworkId: {1}", new String(' ', tab3), entity.Id.NetworkId)) .AppendLine(string.Format("{0}ActorId: {1}", new String(' ', tab3), entity.Id.ActorId)) .AppendLine(string.Format("{0}", new String(' ', tab2)) + "}") .Append(string.Format("{0}ComponentData: [ ", new String(' ', tab1))); if (entity.Components.Any()) { builder.AppendLine(); } foreach (var component in entity.Components) { builder.AppendLine(string.Format("{0}ComponentDataContainer: ", new String(' ', tab2)) + "{") .AppendLine(string.Format("{0}ComponentTypeId: {1}", new String(' ', tab3), component.TypeId)) .AppendLine(string.Format("{0}MemberData: [ {1} ]", new String(' ', tab3), string.Join(", ", component.Fields.Select(x => x.Value)))) .AppendLine(string.Format("{0}", new String(' ', tab2)) + "}"); } if (entity.Components.Any()) { builder.Append(new String(' ', tab1)); } builder.AppendLine("]") .AppendLine(new String(' ', tab1) + "}"); } builder.AppendLine("}") .AppendLine() .AppendLine("AddedNetworkSyncEntities: {"); foreach (var id in entities.Removed) { builder.AppendLine(string.Format("{0}NetworkSyncEntity: ", new String(' ', tab1)) + "{") .AppendLine(string.Format("{0}NetworkId: {1}", new String(' ', tab2), id.NetworkId)) .AppendLine(string.Format("{0}ActorId: {1}", new String(' ', tab2), id.ActorId)) .AppendLine(string.Format("{0}", new String(' ', tab1)) + "}"); } builder.AppendLine("}"); return(builder.ToString()); }
protected IEnumerable <IUpdateSegment> GetSyncSegments( [NotNull] IList <IUpdateWriter> updateSegments, [NotNull] Func <FlagAccumulatorComponent, IUpdateWriter> updateWriterFactory) { if (updateSegments == null) { throw new ArgumentNullException(nameof(updateSegments)); } if (updateWriterFactory == null) { throw new ArgumentNullException(nameof(updateWriterFactory)); } var removeList = new List <IEntityHandle>(); var syncSegments = new List <IUpdateSegment>(); foreach (var handle in SyncEntities) { // remove entity if // it's dead // or its in the leave list if (handle.IsDead() || LeaveEntities.Contains(handle)) { syncSegments.Add(RemoveEntitySegment.Instance); removeList.Add(handle); } else { var entity = handle.Get(); var flags = entity.Components.AssertGet <FlagAccumulatorComponent>(); if (flags.Reinitialize) { InitEntities.Add(handle); removeList.Add(handle); } else { var updater = updateWriterFactory(flags); var needsUpdate = updater.NeedsUpdate(); syncSegments.Add(CommonSegmentResolve(flags, needsUpdate)); if (needsUpdate) { updateSegments.Add(updater); } } } } // cleanup LeaveEntities.Clear(); foreach (var handle in removeList) { SyncEntities.Remove(handle); } return(syncSegments); }