using Biskilog_Accounting.Shared.CustomModels ;
using System.Net.Http.Json ;
using System.Text.Json ;
namespace Biskilog_Accounting.Client.Pages.Dashboard
{
public partial class Dashboard
{
private TradeSummary m_tradeSummary { get ; set ; } = new TradeSummary ( ) ;
private List < WeeklySaleItem > m_weeklySaleItems { get ; set ; } = new List < WeeklySaleItem > { } ;
private string m_username { get ; set ; } = string . Empty ;
private bool loadWeeklySales = true ;
protected override async Task OnInitializedAsync ( )
{
m_username = m_tokenService . GetUserNameFromToken ( await m_tokenService . GetToken ( ) ) ! ;
await GetTradeSummary ( ) ;
await GetWeeklySales ( ) ;
return ;
}
/// <summary>
/// Gets the tade summary
/// </summary>
/// <returns></returns>
async Task GetTradeSummary ( )
{
try
{
var response = await m_http . GetAsync ( "api/analytics/tradesummary" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var tradeSummary = JsonSerializer . Deserialize < TradeSummary > ( jsonContent , options ) ;
m_tradeSummary = tradeSummary ;
StateHasChanged ( ) ;
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
}
/// <summary>
/// Gets the tade summary
/// </summary>
/// <returns></returns>
async Task GetWeeklySales ( )
{
try
{
var response = await m_http . GetAsync ( "api/analytics/sales/weekly" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var sales = JsonSerializer . Deserialize < List < WeeklySaleItem > > ( jsonContent , options ) ;
m_weeklySaleItems = sales ;
loadWeeklySales = false ;
StateHasChanged ( ) ;
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
}
}
}