using Biskilog_Accounting.Shared.CustomModels; using Biskilog_Accounting.Shared.Interfaces; using Biskilog_Accounting.Shared.POSModels; using System.Text.Json; namespace Biskilog_Accounting.Client.Repos { public class SalesRepository : ISalesInterface { private IEnumerable m_transactions; private IEnumerable m_recentTransactions; private readonly HttpClient m_http; public event EventHandler TransactionsChanged; public event EventHandler FetchComplete; public event EventHandler FetchStart; public SalesRepository(HttpClient a_http) { m_recentTransactions = m_transactions = new List(); m_http = a_http; } public async Task FetchRecentTransaction(int a_limit) { FetchStart?.Invoke(this, EventArgs.Empty); var response = await m_http.GetAsync($"api/analytics/sales/recent/{a_limit}"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var recent = JsonSerializer.Deserialize>(jsonContent, options); m_recentTransactions = recent; } FetchComplete?.Invoke(this, EventArgs.Empty); } public async Task FetchTransaction(DateTime a_start, DateTime a_end) { FetchStart?.Invoke(this, EventArgs.Empty); string start = a_start.ToString("yyyy-MM-dd"); string end = a_end.ToString("yyyy-MM-dd"); var response = await m_http.GetAsync($"api/sales/transactions/{start}/{end}"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var recent = JsonSerializer.Deserialize>(jsonContent, options); m_transactions = recent; TransactionsChanged?.Invoke(this, new EventArgs()); } FetchComplete?.Invoke(this, EventArgs.Empty); } public IEnumerable GetTransactions(DateTime a_start, DateTime a_end) { return m_transactions; } public IEnumerable GetRecentTransaction() { return m_recentTransactions; } public async Task FetchReceipt(string a_receiptId) { FetchStart?.Invoke(this, EventArgs.Empty); var response = await m_http.GetAsync($"api/sales/transactions/lookup/{a_receiptId}"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var recent = JsonSerializer.Deserialize>(jsonContent, options); m_transactions = recent; TransactionsChanged?.Invoke(this, new EventArgs()); } FetchComplete?.Invoke(this, EventArgs.Empty); } public IEnumerable GetReceipt(string a_receiptId) { throw new NotImplementedException(); } public async Task> GetReceiptDetail(string a_receiptId) { var response = await m_http.GetAsync($"api/sales/receipt/lookup/{a_receiptId}"); if (response.IsSuccessStatusCode) { var jsonContent = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var recent = JsonSerializer.Deserialize>(jsonContent, options); return recent; } return null; } } }