public MunicipalityListResponse Sync(SyncMunicipalityRequest request) { MunicipalityListResponse response = new MunicipalityListResponse(); try { response.Municipalities = new List <MunicipalityViewModel>(); if (request.LastUpdatedAt != null) { response.Municipalities.AddRange(unitOfWork.GetMunicipalityRepository() .GetMunicipalitiesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToMunicipalityViewModelList() ?? new List <MunicipalityViewModel>()); } else { response.Municipalities.AddRange(unitOfWork.GetMunicipalityRepository() .GetMunicipalities(request.CompanyId) ?.ConvertToMunicipalityViewModelList() ?? new List <MunicipalityViewModel>()); } response.Success = true; } catch (Exception ex) { response.Municipalities = new List <MunicipalityViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public MunicipalityListResponse Sync(SyncMunicipalityRequest request) { MunicipalityListResponse response = new MunicipalityListResponse(); try { response = WpfApiHandler.SendToApi <SyncMunicipalityRequest, MunicipalityViewModel, MunicipalityListResponse>(request, "Sync"); } catch (Exception ex) { response.Municipalities = new List <MunicipalityViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncMunicipalityRequest request) { MunicipalityListResponse response = new MunicipalityListResponse(); try { response = this.MunicipalityService.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(IMunicipalityService municipalityService, Action <int, int> callback = null) { try { SyncMunicipalityRequest request = new SyncMunicipalityRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; MunicipalityListResponse response = municipalityService.Sync(request); if (response.Success) { toSync = response?.Municipalities?.Count ?? 0; List <MunicipalityViewModel> municipalitysFromDB = response.Municipalities; using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName)) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM Municipalities WHERE Identifier = @Identifier"; SqliteCommand insertCommand = db.CreateCommand(); insertCommand.CommandText = SqlCommandInsertPart; foreach (var municipality in municipalitysFromDB) { deleteCommand.Parameters.AddWithValue("@Identifier", municipality.Identifier); deleteCommand.ExecuteNonQuery(); deleteCommand.Parameters.Clear(); if (municipality.IsActive) { municipality.IsSynced = true; insertCommand = AddCreateParameters(insertCommand, municipality); 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; } }