AppData.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using HyperCube.Models;
  6. using Microsoft.AspNetCore.Components;
  7. using Microsoft.AspNetCore.Components.Authorization;
  8. using Microsoft.AspNetCore.Identity;
  9. namespace HyperCube
  10. {
  11. /// <summary>
  12. /// Синглтон приложения
  13. /// </summary>
  14. public class AppData
  15. {
  16. [Inject]
  17. AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  18. [Inject]
  19. UserManager<IdentityUser> UserManager { get; set; }
  20. private AccountModel _currentAccount;
  21. public AccountModel CurrentAccount
  22. {
  23. get
  24. {
  25. if (_currentAccount == null)
  26. {
  27. GetCurrentAcc();
  28. return _currentAccount;
  29. }
  30. else
  31. return _currentAccount;
  32. }
  33. }
  34. public ReportModel Report = new();
  35. private static Dictionary<int, ArticleModel> _articles;
  36. public static Dictionary<int, ArticleModel> Articles
  37. {
  38. get
  39. {
  40. if (_articles == null)
  41. LoadArticles();
  42. return _articles;
  43. }
  44. }
  45. public static async Task LoadArticles()
  46. {
  47. Console.WriteLine("GetArticles()");
  48. MySQLConnector dbCon = MySQLConnector.Instance();
  49. string stringSQL = $"SELECT a.id, filename, article_name, date_publish, authors, ah.action_type " +
  50. $"FROM articles a " +
  51. $"LEFT JOIN actions_history ah ON a.id = ah.article_id " +
  52. $"AND EXISTS (SELECT 1 FROM actions_history ah1 WHERE ah.article_id = ah1.article_id HAVING MAX(ah1.date_add) = ah.date_add) " +
  53. $"ORDER BY a.id";
  54. _articles = await dbCon.SQLSelectArticles(stringSQL);
  55. }
  56. private async Task GetCurrentAcc()
  57. {
  58. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  59. var user = authState.User;
  60. if (user.Identity.IsAuthenticated)
  61. {
  62. ///tmp
  63. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  64. var currentUser = await UserManager.GetUserAsync(user);
  65. if (accounts.ContainsKey(currentUser.Id))
  66. _currentAccount = accounts[currentUser.Id];
  67. }
  68. }
  69. }
  70. }