예제 #1
0
        /// <summary>
        /// Sync logic for sale table
        /// </summary>
        private void SyncTable_Sale()
        {
            var localTable = new SaleRepository<Sale>(_localSessionContext);
            var remoteTable = new SaleRepository<Sale>(_remoteSessionContext);

            OnSyncStart?.Invoke(this, new SyncStartedArgs("Sale"));

            #region SyncCode

            var count = 0;
            var total = localTable.Count();
            foreach (var row in localTable.GetAll())
            {
                // Find a sync record that matches our related column
                var recordProduct = _recordRepository.GetByLocalId(row.ProductId, "product_table");
                var recordStore = _recordRepository.GetByLocalId(row.StoreId, "store_table");

                // Update our related column with the remote ID to prevent reference errors
                // Update product relation
                if (recordProduct != null)
                    row.ProductId = recordProduct.Remote_Id;

                // Update store relation
                if (recordStore != null)
                    row.StoreId = recordStore.Remote_Id;

                // Copy object and save (this removes ID)
                var newObject = ObjectCopy.Copy(row);
                remoteTable.Save(newObject);

                // Save changes to remote
                remoteTable.Commit();

                OnUpdateStatus?.Invoke(this, new ProgressEventArgs("Synced entity " + ++count + "/" + total));

                // Remove after successful remove
                localTable.Remove(row);
                localTable.Commit();
            }

            #endregion
        }