using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HyperCube.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
namespace HyperCube
{
///
/// Синглтон приложения
///
public class AppData
{
[Inject]
AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject]
UserManager UserManager { get; set; }
private AccountModel _currentAccount;
public AccountModel CurrentAccount
{
get
{
if (_currentAccount == null)
{
GetCurrentAcc();
return _currentAccount;
}
else
return _currentAccount;
}
}
private static Dictionary _articles;
public static Dictionary Articles
{
get
{
if (_articles == null)
LoadArticles();
return _articles;
}
}
public static async Task LoadArticles()
{
Console.WriteLine("GetArticles()");
MySQLConnector dbCon = MySQLConnector.Instance();
string stringSQL = $"SELECT a.id, filename, article_name, date_publish, authors, ah.action_type " +
$"FROM articles a " +
$"LEFT JOIN actions_history ah ON a.id = ah.article_id " +
$"AND EXISTS (SELECT 1 FROM actions_history ah1 WHERE ah.article_id = ah1.article_id HAVING MAX(ah1.date_add) = ah.date_add) " +
$"ORDER BY a.id";
_articles = await dbCon.SQLSelectArticles(stringSQL);
}
private async Task GetCurrentAcc()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
///tmp
Dictionary accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
var currentUser = await UserManager.GetUserAsync(user);
if (accounts.ContainsKey(currentUser.Id))
_currentAccount = accounts[currentUser.Id];
}
}
}
}