public CountryListResponse Sync(SyncCountryRequest request) { CountryListResponse response = new CountryListResponse(); try { response.Countries = new List <CountryViewModel>(); if (request.LastUpdatedAt != null) { response.Countries.AddRange(unitOfWork.GetCountryRepository() .GetCountriesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToCountryViewModelList() ?? new List <CountryViewModel>()); } else { response.Countries.AddRange(unitOfWork.GetCountryRepository() .GetCountries(request.CompanyId) ?.ConvertToCountryViewModelList() ?? new List <CountryViewModel>()); } response.Success = true; } catch (Exception ex) { response.Countries = new List <CountryViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public CountryListResponse Sync(SyncCountryRequest request) { CountryListResponse response = new CountryListResponse(); try { response = WpfApiHandler.SendToApi <SyncCountryRequest, CountryViewModel, CountryListResponse>(request, "Sync"); } catch (Exception ex) { response.Countries = new List <CountryViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncCountryRequest request) { CountryListResponse response = new CountryListResponse(); try { response = this.countryService.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(ICountryService countryService, Action <int, int> callback = null) { try { SyncCountryRequest request = new SyncCountryRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; CountryListResponse response = countryService.Sync(request); if (response.Success) { toSync = response?.Countries?.Count ?? 0; List <CountryViewModel> countrysFromDB = response.Countries; using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName)) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM Countries WHERE Identifier = @Identifier"; SqliteCommand insertCommand = db.CreateCommand(); insertCommand.CommandText = SqlCommandInsertPart; foreach (var country in countrysFromDB) { deleteCommand.Parameters.AddWithValue("@Identifier", country.Identifier); deleteCommand.ExecuteNonQuery(); deleteCommand.Parameters.Clear(); if (country.IsActive) { country.IsSynced = true; insertCommand = AddCreateParameters(insertCommand, country); 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; } }