public AgencyListResponse Sync(SyncAgencyRequest request)
        {
            AgencyListResponse response = new AgencyListResponse();

            try
            {
                response.Agencies = new List <AgencyViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Agencies.AddRange(unitOfWork.GetAgencyRepository()
                                               .GetAgenciesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                               ?.ConvertToAgencyViewModelList() ?? new List <AgencyViewModel>());
                }
                else
                {
                    response.Agencies.AddRange(unitOfWork.GetAgencyRepository()
                                               .GetAgencies(request.CompanyId)
                                               ?.ConvertToAgencyViewModelList() ?? new List <AgencyViewModel>());
                }

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

            return(response);
        }
        public AgencyListResponse Sync(SyncAgencyRequest request)
        {
            AgencyListResponse response = new AgencyListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncAgencyRequest, AgencyViewModel, AgencyListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Agencies = new List <AgencyViewModel>();
                response.Success  = false;
                response.Message  = ex.Message;
            }

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

            try
            {
                response = this.AgencyService.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(IAgencyService AgencyService, Action <int, int> callback = null)
        {
            try
            {
                SyncAgencyRequest request = new SyncAgencyRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                AgencyListResponse response = AgencyService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Agencies?.Count ?? 0;
                    List <AgencyViewModel> agenciesFromDB = response.Agencies;

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

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

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

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

                                    insertCommand = AddCreateParameters(insertCommand, agency);
                                    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;
            }
        }