public MunicipalityListResponse GetMunicipalitiesNewerThen(int companyId, DateTime?lastUpdateTime) { MunicipalityListResponse response = new MunicipalityListResponse(); try { if (lastUpdateTime != null) { response.Municipalities = unitOfWork.GetMunicipalityRepository() .GetMunicipalitiesNewerThen(companyId, (DateTime)lastUpdateTime) .ConvertToMunicipalityViewModelList(); } else { response.Municipalities = unitOfWork.GetMunicipalityRepository() .GetMunicipalities(companyId) .ConvertToMunicipalityViewModelList(); } 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.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 GetMunicipalitiesForPopup(int companyId, Guid regionIdentifier, string filterString) { MunicipalityListResponse response = new MunicipalityListResponse(); List <MunicipalityViewModel> Municipalities = new List <MunicipalityViewModel>(); using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); try { SqliteCommand selectCommand = new SqliteCommand( SqlCommandSelectPart + "FROM Municipalities " + "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name OR Code LIKE @Name) " + "OR (@MunicipalityCode IS NULL OR @MunicipalityCode = '' OR MunicipalityCode LIKE @MunicipalityCode) " + "OR (@RegionName IS NULL OR @RegionName = '' OR RegionName LIKE @RegionName) " + "OR (@CountryName IS NULL OR @CountryName = '' OR CountryName LIKE @CountryName) " + "AND RegionIdentifier = @RegionIdentifier " + "ORDER BY IsSynced, Id DESC " + "LIMIT @ItemsPerPage;", db); selectCommand.Parameters.AddWithValue("@Name", ((object)filterString) != null ? "%" + filterString + "%" : ""); selectCommand.Parameters.AddWithValue("@MunicipalityCode", ((object)filterString) != null ? "%" + filterString + "%" : ""); selectCommand.Parameters.AddWithValue("@RegionName", ((object)filterString) != null ? "%" + filterString + "%" : ""); selectCommand.Parameters.AddWithValue("@CountryName", ((object)filterString) != null ? "%" + filterString + "%" : ""); selectCommand.Parameters.AddWithValue("@RegionIdentifier", regionIdentifier); selectCommand.Parameters.AddWithValue("@CompanyId", ((object)filterString) != null ? companyId : 0); selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100); SqliteDataReader query = selectCommand.ExecuteReader(); while (query.Read()) { MunicipalityViewModel dbEntry = Read(query); Municipalities.Add(dbEntry); } } catch (SqliteException error) { MainWindow.ErrorMessage = error.Message; response.Success = false; response.Message = error.Message; response.Municipalities = new List <MunicipalityViewModel>(); return(response); } db.Close(); } response.Success = true; response.Municipalities = Municipalities; 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 MunicipalityListResponse GetMunicipalities(int companyId) { MunicipalityListResponse response = new MunicipalityListResponse(); try { response.Municipalities = unitOfWork.GetMunicipalityRepository().GetMunicipalities(companyId) .ConvertToMunicipalityViewModelList(); response.Success = true; } catch (Exception ex) { response.Municipalities = new List <MunicipalityViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult GetMunicipalities(int companyId) { MunicipalityListResponse response = new MunicipalityListResponse(); try { response = MunicipalityService.GetMunicipalities(companyId); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; Console.WriteLine(ex.Message); } return(Json(response, new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented })); }
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 MunicipalityListResponse GetMunicipalities(int companyId) { MunicipalityListResponse response = new MunicipalityListResponse(); try { response = WpfApiHandler.GetFromApi <List <MunicipalityViewModel>, MunicipalityListResponse>("GetMunicipalities", new Dictionary <string, string>() { { "CompanyId", companyId.ToString() } }); } catch (Exception ex) { response.Municipalities = new List <MunicipalityViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
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; } }
public MunicipalityListResponse GetMunicipalitiesByPage(int companyId, MunicipalityViewModel MunicipalitySearchObject, int currentPage = 1, int itemsPerPage = 50) { MunicipalityListResponse response = new MunicipalityListResponse(); List <MunicipalityViewModel> Municipalities = new List <MunicipalityViewModel>(); using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); try { SqliteCommand selectCommand = new SqliteCommand( SqlCommandSelectPart + "FROM Municipalities " + "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " + "AND (@MunicipalityCode IS NULL OR @MunicipalityCode = '' OR MunicipalityCode LIKE @MunicipalityCode) " + "AND (@RegionName IS NULL OR @RegionName = '' OR RegionName LIKE @RegionName) " + "AND (@CountryName IS NULL OR @CountryName = '' OR CountryName LIKE @CountryName) " + "AND CompanyId = @CompanyId " + "ORDER BY IsSynced, Id DESC " + "LIMIT @ItemsPerPage OFFSET @Offset;", db); selectCommand.Parameters.AddWithValue("@Name", ((object)MunicipalitySearchObject.Search_Name) != null ? "%" + MunicipalitySearchObject.Search_Name + "%" : ""); selectCommand.Parameters.AddWithValue("@MunicipalityCode", ((object)MunicipalitySearchObject.Search_MunicipalityCode) != null ? "%" + MunicipalitySearchObject.Search_MunicipalityCode + "%" : ""); selectCommand.Parameters.AddWithValue("@RegionName", ((object)MunicipalitySearchObject.Search_Region) != null ? "%" + MunicipalitySearchObject.Search_Region + "%" : ""); selectCommand.Parameters.AddWithValue("@CountryName", ((object)MunicipalitySearchObject.Search_Country) != null ? "%" + MunicipalitySearchObject.Search_Country + "%" : ""); selectCommand.Parameters.AddWithValue("@CompanyId", companyId); selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage); selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage); SqliteDataReader query = selectCommand.ExecuteReader(); while (query.Read()) { MunicipalityViewModel dbEntry = Read(query); Municipalities.Add(dbEntry); } selectCommand = new SqliteCommand( "SELECT Count(*) " + "FROM Municipalities " + "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " + "AND (@MunicipalityCode IS NULL OR @MunicipalityCode = '' OR MunicipalityCode LIKE @MunicipalityCode) " + "AND (@RegionName IS NULL OR @RegionName = '' OR RegionName LIKE @RegionName) " + "AND (@CountryName IS NULL OR @CountryName = '' OR CountryName LIKE @CountryName) " + "AND CompanyId = @CompanyId;", db); selectCommand.Parameters.AddWithValue("@Name", ((object)MunicipalitySearchObject.Search_Name) != null ? "%" + MunicipalitySearchObject.Search_Name + "%" : ""); selectCommand.Parameters.AddWithValue("@MunicipalityCode", ((object)MunicipalitySearchObject.Search_MunicipalityCode) != null ? "%" + MunicipalitySearchObject.Search_MunicipalityCode + "%" : ""); selectCommand.Parameters.AddWithValue("@RegionName", ((object)MunicipalitySearchObject.Search_Region) != null ? "%" + MunicipalitySearchObject.Search_Region + "%" : ""); selectCommand.Parameters.AddWithValue("@CountryName", ((object)MunicipalitySearchObject.Search_Country) != null ? "%" + MunicipalitySearchObject.Search_Country + "%" : ""); selectCommand.Parameters.AddWithValue("@CompanyId", companyId); query = selectCommand.ExecuteReader(); if (query.Read()) { response.TotalItems = query.GetInt32(0); } } catch (SqliteException error) { MainWindow.ErrorMessage = error.Message; response.Success = false; response.Message = error.Message; response.Municipalities = new List <MunicipalityViewModel>(); return(response); } db.Close(); } response.Success = true; response.Municipalities = Municipalities; return(response); }