123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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
- {
- /// <summary>
- /// Синглтон приложения
- /// </summary>
- public class AppData
- {
- [Inject]
- AuthenticationStateProvider AuthenticationStateProvider { get; set; }
- [Inject]
- UserManager<IdentityUser> UserManager { get; set; }
- private AccountModel _currentAccount;
- public AccountModel CurrentAccount
- {
- get
- {
- if (_currentAccount == null)
- {
- GetCurrentAcc();
- return _currentAccount;
- }
- else
- return _currentAccount;
- }
- }
- private static Dictionary<int, ArticleModel> _articles;
- public static Dictionary<int, ArticleModel> 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<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
- var currentUser = await UserManager.GetUserAsync(user);
- if (accounts.ContainsKey(currentUser.Id))
- _currentAccount = accounts[currentUser.Id];
- }
- }
- }
- }
|