using Biskilog_Accounting.Shared.CustomModels; using Biskilog_Accounting.Shared.Interfaces; using System.Text.Json; using Biskilog_Accounting.Shared.POSModels; using System.Text.RegularExpressions; namespace Biskilog_Accounting.Client.Repos { public class ProductRepository : IProduct { private IEnumerable m_products; private IEnumerable m_units; private IEnumerable m_brands; private IEnumerable m_categories; private readonly HttpClient m_http; public event EventHandler ProductsChanged; public event EventHandler UnitsChanged; public event EventHandler BrandsChanged; public event EventHandler CategoriesChanged; public ProductRepository(HttpClient a_http) { m_products = new List(); m_units = new List(); m_brands = new List(); m_categories = new List(); m_http = a_http; } public async Task FetchProducts() { var response = await m_http.GetAsync($"api/products/fetch"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var products = JsonSerializer.Deserialize>(jsonContent, options); m_products = products; ProductsChanged?.Invoke(this, EventArgs.Empty); } } public ProductItem GetProductById(string a_id) { if (m_products.Count() > 0) { return m_products.First(i => i.Product.Pcode == a_id); } return null; } public ProductItem GetProductByName(string name) { throw new NotImplementedException(); } public IEnumerable GetProducts(string a_productKey) { a_productKey = Regex.Replace(a_productKey, @"\s", "").ToLower(); return m_products.Where(p => Regex.Replace(p.Product!.ProductName!, @"\s", "").ToLower().StartsWith(a_productKey) || Regex.Replace(p.Product!.Pcode!, @"\s", "").ToLower().StartsWith(a_productKey) || Regex.Replace(p.Product!.Pdesc!, @"\s", "").ToLower().StartsWith(a_productKey)); } public void RefreshList() { throw new NotImplementedException(); } public IEnumerable GetUnitofmeasures() { return m_units; } public async Task FetchUnits() { var response = await m_http.GetAsync($"api/products/units"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var units = JsonSerializer.Deserialize>(jsonContent, options); m_units = units; UnitsChanged?.Invoke(this, EventArgs.Empty); } } public string GetUnitName(string a_unitCode) { return m_units?.FirstOrDefault(u => u.UnitCode == a_unitCode)?.Unitname; } public IEnumerable GetBrands(string a_brandKey = "") { a_brandKey = Regex.Replace(a_brandKey, @"\s", "").ToLower(); return m_brands.Where(b => Regex.Replace(b.Id, @"\s", "").ToLower().StartsWith(a_brandKey) || Regex.Replace(b.Brand!, @"\s", "").ToLower().StartsWith(a_brandKey)); } public IEnumerable GetCategories(string a_categoryKey = "") { a_categoryKey = Regex.Replace(a_categoryKey, @"\s", "").ToLower(); return m_categories.Where(c => Regex.Replace(c.Id, @"\s", "").ToLower().StartsWith(a_categoryKey) || Regex.Replace(c.Category!, @"\s", "").ToLower().StartsWith(a_categoryKey)); } public async Task FetchBrands() { var response = await m_http.GetAsync($"api/products/brands"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var brands = JsonSerializer.Deserialize>(jsonContent, options); m_brands = brands; BrandsChanged?.Invoke(this, EventArgs.Empty); } } public async Task FetchCategories() { var response = await m_http.GetAsync($"api/products/categories"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var categories = JsonSerializer.Deserialize>(jsonContent, options); m_categories = categories; CategoriesChanged?.Invoke(this, EventArgs.Empty); } } public IEnumerable GetLowstockItems() { throw new NotImplementedException(); } public Task FetchLowStockProducts() { throw new NotImplementedException(); } public Task SyncProducts(List a_item) { throw new NotImplementedException(); } public Task SyncInventory(List a_item) { throw new NotImplementedException(); } public Task SyncInventoryEntries(List a_item) { throw new NotImplementedException(); } public Task SyncPriceChanges(List a_items) { throw new NotImplementedException(); } public Task SyncProductAltUnit(List a_items) { throw new NotImplementedException(); } public Task SyncRestockAsync(List a_items) { throw new NotImplementedException(); } public Task SyncUnitOfMeasureAsync(List a_items) { throw new NotImplementedException(); } public Task SyncStockAsync(List a_items) { throw new NotImplementedException(); } public Task SyncBrandsAsync(List a_items) { throw new NotImplementedException(); } public Task SyncCategoriesAsync(List a_items) { throw new NotImplementedException(); } public DateTime GetLastSyncDate(string a_tablename) { throw new NotImplementedException(); } public void SetLastSyncDate(string a_tableName, DateTime a_timestamp) { throw new NotImplementedException(); } } }