public SectorListResponse GetSectorsNewerThen(int companyId, DateTime?lastUpdateTime) { SectorListResponse response = new SectorListResponse(); try { if (lastUpdateTime != null) { response.Sectors = unitOfWork.GetSectorRepository() .GetSectorsNewerThen(companyId, (DateTime)lastUpdateTime) .ConvertToSectorViewModelList(); } else { response.Sectors = unitOfWork.GetSectorRepository() .GetSectors(companyId) .ConvertToSectorViewModelList(); } response.Success = true; } catch (Exception ex) { response.Sectors = new List <SectorViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public SectorListResponse Sync(SyncSectorRequest request) { SectorListResponse response = new SectorListResponse(); try { response.Sectors = new List <SectorViewModel>(); if (request.LastUpdatedAt != null) { response.Sectors.AddRange(unitOfWork.GetSectorRepository() .GetSectorsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToSectorViewModelList() ?? new List <SectorViewModel>()); } else { response.Sectors.AddRange(unitOfWork.GetSectorRepository() .GetSectors(request.CompanyId) ?.ConvertToSectorViewModelList() ?? new List <SectorViewModel>()); } response.Success = true; } catch (Exception ex) { response.Sectors = new List <SectorViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public SectorListResponse Sync(SyncSectorRequest request) { SectorListResponse response = new SectorListResponse(); try { response = WpfApiHandler.SendToApi <SyncSectorRequest, SectorViewModel, SectorListResponse>(request, "Sync"); } catch (Exception ex) { response.Sectors = new List <SectorViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public SectorListResponse GetSectorsForPopup(int companyId, Guid countryIdentifier, string filterString) { SectorListResponse response = new SectorListResponse(); List <SectorViewModel> Sectors = new List <SectorViewModel>(); using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); try { SqliteCommand selectCommand = new SqliteCommand( SqlCommandSelectPart + "FROM Sectors " + "WHERE (@Filter IS NULL OR @Filter = '' OR SecondCode LIKE @Filter OR Name LIKE @Filter) " + "AND CountryIdentifier = @CountryIdentifier " + "AND CompanyId = @CompanyId " + "ORDER BY IsSynced, Id DESC " + "LIMIT @ItemsPerPage;", db); selectCommand.Parameters.AddWithValue("@Filter", ((object)filterString) != null ? "%" + filterString + "%" : ""); selectCommand.Parameters.AddWithValue("@CountryIdentifier", countryIdentifier); selectCommand.Parameters.AddWithValue("@CompanyId", ((object)companyId) != null ? companyId : 0); selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100); SqliteDataReader query = selectCommand.ExecuteReader(); while (query.Read()) { SectorViewModel dbEntry = Read(query); Sectors.Add(dbEntry); } } catch (SqliteException error) { MainWindow.ErrorMessage = error.Message; response.Success = false; response.Message = error.Message; response.Sectors = new List <SectorViewModel>(); return(response); } db.Close(); } response.Success = true; response.Sectors = Sectors; return(response); }
public JsonResult GetSectors(int companyId) { SectorListResponse response = new SectorListResponse(); try { response = sectorService.GetSectors(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] SyncSectorRequest request) { SectorListResponse response = new SectorListResponse(); try { response = this.sectorService.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 SectorListResponse GetSectors(int companyId) { SectorListResponse response = new SectorListResponse(); try { response.Sectors = unitOfWork.GetSectorRepository().GetSectors(companyId) .ConvertToSectorViewModelList(); response.Success = true; } catch (Exception ex) { response.Sectors = new List <SectorViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public SectorListResponse GetSectors(int companyId) { SectorListResponse response = new SectorListResponse(); try { response = WpfApiHandler.GetFromApi <List <SectorViewModel>, SectorListResponse>("GetSectors", new Dictionary <string, string>() { { "CompanyId", companyId.ToString() } }); } catch (Exception ex) { response.Sectors = new List <SectorViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public SectorListResponse GetSectorsByPage(int companyId, SectorViewModel sectorSearchObject, int currentPage = 1, int itemsPerPage = 50) { SectorListResponse response = new SectorListResponse(); List <SectorViewModel> Sectors = new List <SectorViewModel>(); using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); try { SqliteCommand selectCommand = new SqliteCommand( SqlCommandSelectPart + "FROM Sectors " + "WHERE (@SecondCode IS NULL OR @SecondCode = '' OR SecondCode LIKE @SecondCode) " + "AND (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " + "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("@SecondCode", ((object)sectorSearchObject.Search_SecondCode) != null ? "%" + sectorSearchObject.Search_SecondCode + "%" : ""); selectCommand.Parameters.AddWithValue("@Name", ((object)sectorSearchObject.Search_Name) != null ? "%" + sectorSearchObject.Search_Name + "%" : ""); selectCommand.Parameters.AddWithValue("@CountryName", ((object)sectorSearchObject.Search_Country) != null ? "%" + sectorSearchObject.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()) { SectorViewModel dbEntry = Read(query); Sectors.Add(dbEntry); } selectCommand = new SqliteCommand( "SELECT Count(*) " + "FROM Sectors " + "WHERE (@SecondCode IS NULL OR @SecondCode = '' OR SecondCode LIKE @SecondCode) " + "AND (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " + "AND (@CountryName IS NULL OR @CountryName = '' OR CountryName LIKE @CountryName) " + "AND CompanyId = @CompanyId;", db); selectCommand.Parameters.AddWithValue("@SecondCode", ((object)sectorSearchObject.Search_SecondCode) != null ? "%" + sectorSearchObject.Search_SecondCode + "%" : ""); selectCommand.Parameters.AddWithValue("@Name", ((object)sectorSearchObject.Search_Name) != null ? "%" + sectorSearchObject.Search_Name + "%" : ""); selectCommand.Parameters.AddWithValue("@CountryName", ((object)sectorSearchObject.Search_Country) != null ? "%" + sectorSearchObject.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.Sectors = new List <SectorViewModel>(); return(response); } db.Close(); } response.Success = true; response.Sectors = Sectors; return(response); }
public void Sync(ISectorService sectorService, Action <int, int> callback = null) { try { SyncSectorRequest request = new SyncSectorRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; SectorListResponse response = sectorService.Sync(request); if (response.Success) { toSync = response?.Sectors?.Count ?? 0; List <SectorViewModel> sectorsFromDB = response.Sectors; using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName)) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM Sectors WHERE Identifier = @Identifier"; SqliteCommand insertCommand = db.CreateCommand(); insertCommand.CommandText = SqlCommandInsertPart; foreach (var sector in sectorsFromDB) { deleteCommand.Parameters.AddWithValue("@Identifier", sector.Identifier); deleteCommand.ExecuteNonQuery(); deleteCommand.Parameters.Clear(); if (sector.IsActive) { sector.IsSynced = true; insertCommand = AddCreateParameters(insertCommand, sector); 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; } }