Ejemplo n.º 1
0
        public BankListResponse Sync(SyncBankRequest request)
        {
            BankListResponse response = new BankListResponse();

            try
            {
                response.Banks = new List <BankViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Banks.AddRange(unitOfWork.GetBankRepository()
                                            .GetBanksNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                            ?.ConvertToBankViewModelList() ?? new List <BankViewModel>());
                }
                else
                {
                    response.Banks.AddRange(unitOfWork.GetBankRepository()
                                            .GetBanks(request.CompanyId)
                                            ?.ConvertToBankViewModelList() ?? new List <BankViewModel>());
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Banks   = new List <BankViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
Ejemplo n.º 2
0
        public BankListResponse Sync(SyncBankRequest request)
        {
            BankListResponse response = new BankListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncBankRequest, BankViewModel, BankListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Banks   = new List <BankViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
        public JsonResult Sync([FromBody] SyncBankRequest request)
        {
            BankListResponse response = new BankListResponse();

            try
            {
                response = this.bankService.Sync(request);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
        public void Sync(IBankService bankService, Action <int, int> callback = null)
        {
            try
            {
                SyncBankRequest request = new SyncBankRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                BankListResponse response = bankService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Banks?.Count ?? 0;
                    List <BankViewModel> banksFromDB = response.Banks;

                    using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM Banks WHERE Identifier = @Identifier";

                            SqliteCommand insertCommand = db.CreateCommand();
                            insertCommand.CommandText = SqlCommandInsertPart;

                            foreach (var bank in banksFromDB)
                            {
                                deleteCommand.Parameters.AddWithValue("@Identifier", bank.Identifier);
                                deleteCommand.ExecuteNonQuery();
                                deleteCommand.Parameters.Clear();

                                if (bank.IsActive)
                                {
                                    bank.IsSynced = true;

                                    insertCommand = AddCreateParameters(insertCommand, bank);
                                    insertCommand.ExecuteNonQuery();
                                    insertCommand.Parameters.Clear();

                                    syncedItems++;
                                    callback?.Invoke(syncedItems, toSync);
                                }
                            }

                            transaction.Commit();
                        }
                        db.Close();
                    }
                }
                else
                {
                    throw new Exception(response.Message);
                }
            }
            catch (Exception ex)
            {
                MainWindow.ErrorMessage = ex.Message;
            }
        }