AppData.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. private static Dictionary<int, ArticleModel> _articles;
  35. public static Dictionary<int, ArticleModel> Articles
  36. {
  37. get
  38. {
  39. if (_articles == null)
  40. LoadArticles();
  41. return _articles;
  42. }
  43. }
  44. public static async Task LoadArticles()
  45. {
  46. Console.WriteLine("GetArticles()");
  47. MySQLConnector dbCon = MySQLConnector.Instance();
  48. string stringSQL = $"SELECT a.id, filename, article_name, date_publish, authors, ah.action_type " +
  49. $"FROM articles a " +
  50. $"LEFT JOIN actions_history ah ON a.id = ah.article_id " +
  51. $"AND EXISTS (SELECT 1 FROM actions_history ah1 WHERE ah.article_id = ah1.article_id HAVING MAX(ah1.date_add) = ah.date_add) " +
  52. $"ORDER BY a.id";
  53. _articles = await dbCon.SQLSelectArticles(stringSQL);
  54. }
  55. private async Task GetCurrentAcc()
  56. {
  57. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  58. var user = authState.User;
  59. if (user.Identity.IsAuthenticated)
  60. {
  61. ///tmp
  62. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  63. var currentUser = await UserManager.GetUserAsync(user);
  64. if (accounts.ContainsKey(currentUser.Id))
  65. _currentAccount = accounts[currentUser.Id];
  66. }
  67. }
  68. }
  69. }