Переглянути джерело

промежутки рефактора

ganahrhr 3 роки тому
батько
коміт
15ad0b5b88

+ 97 - 0
AppData.cs

@@ -0,0 +1,97 @@
+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;
+            }
+        }
+
+        public async Task<Dictionary<string, AccountModel>> LoadAccounts()
+        {
+            Console.WriteLine("LoadAccounts()");
+
+            Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
+
+            foreach (var acc in accounts)
+            {
+                acc.Value.LoadRoles();
+                var wallets = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM account_wallets WHERE account_uuid='{acc.Value.UUID}'");
+
+                if (wallets.Count > 0)
+                {
+                    foreach (var wallet in wallets)
+                    {
+                        var bc_id = Convert.ToInt32(wallet["blockchain_id"]);
+                        var wallet_id = Convert.ToString(wallet["uuid"]);
+                        if (bc_id == 0)
+                            acc.Value.ETH_Address = wallet_id;
+                        else
+                            acc.Value.ETH_Address1 = wallet_id;
+                        Console.WriteLine($"acc {acc.Value.Name} wallet0 {acc.Value.ETH_Address} wallet1 {acc.Value.ETH_Address1}");
+                    }
+                }
+            }
+
+            return accounts;
+        }
+
+        public async Task<Dictionary<int, ArticleModel>> GetArticles()
+        {
+            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";
+            return 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];                
+            }            
+        }
+    }
+}

+ 2 - 1
Controllers/ValuesController.cs

@@ -25,7 +25,8 @@ namespace HyperCube.Controllers
         public async Task<string> Get(int id, string email)
         {
             string transactionId = $"not found: {id} {email}";
-            var article = ArticleModel.Find(id);
+            ArticleModel article = new();
+            await article.Find(id);
             if (article != null)
                 transactionId = await SmartContract.Verify(article);
             return transactionId;

+ 2 - 2
DocParse.cs

@@ -10,7 +10,7 @@ namespace HyperCube
 {
     public class DocParse
     {
-        public static Models.ArticleModel ReadPDF(MemoryStream ms)
+        public Models.ArticleModel ReadPDF(MemoryStream ms)
         {
             //Console.WriteLine("ReadPDF start");
 
@@ -77,7 +77,7 @@ namespace HyperCube
             return articleModel;
         }
 
-        public static string ReadDocx()
+        public string ReadDocx()
         {
             StringBuilder pageText = new();
 

+ 51 - 45
Models/AccountModel.cs

@@ -4,6 +4,7 @@ using System.Numerics;
 using System.Threading.Tasks;
 using System.Collections.Generic;
 using Console = HyperCube.Utils.AdvConsole;
+using Microsoft.AspNetCore.Components;
 
 namespace HyperCube.Models
 {
@@ -11,6 +12,9 @@ namespace HyperCube.Models
 
     public class AccountModel
     {
+        [Inject]
+        AppData AppData { get; set; }
+
         //public event EventHandler<int> RolesChanged;
         public Action<int> RolesChanged;
         public byte bsel;
@@ -43,13 +47,11 @@ namespace HyperCube.Models
             Console.WriteLine($"role removed, сount: {Roles.Count}");
         }
 
-        public static Dictionary<string, AccountModel> Loaded = new();
-        public static AccountModel Current;
-        string eth_address { get; set; }
-        string eth_address1 { get; set; }
-        /// <summary>
-        /// ASP Identity ID
-        /// </summary>
+        //public Dictionary<string, AccountModel> Loaded = new();
+        //public AccountModel Current;
+
+        public string ETH_Address { get; set; }
+        public string ETH_Address1 { get; set; }
         public string UUID { get; set; }
         public string Name { get; set; }
         public string Email { get; set; }
@@ -60,9 +62,9 @@ namespace HyperCube.Models
         {
             string addr;
             if (bc.port == 8666)
-                addr = eth_address1;
+                addr = ETH_Address1;
             else
-                addr = eth_address;
+                addr = ETH_Address;
 
             return addr;
         }
@@ -71,9 +73,9 @@ namespace HyperCube.Models
         {
             string addr;
             if (bc.port == 8666)
-                addr = eth_address1;
+                addr = ETH_Address1;
             else
-                addr = eth_address;
+                addr = ETH_Address;
 
             if (addr == null || addr == "")
                 addr = await bc.CreateBlockchainAccount(this, PWDHash);
@@ -83,34 +85,38 @@ namespace HyperCube.Models
         public void SetActualAddress(string value, Blockchain bc)
         {
             if (bc.port == 8666)
-                eth_address1 = value;
+                ETH_Address1 = value;
             else
-                eth_address = value;
+                ETH_Address = value;
         }
 
         public AccountModel()
         {
         }
 
-        public static AccountModel GetCurrent()
+        public AccountModel GetCurrent()
         {
-            return Current;
+            return AppData.CurrentAccount;
         }
 
-        public static AccountModel FindByMail(string mail)
+        public async Task<AccountModel> FindByMail(string mail)
         {
-            var selectedUsers = from user in Loaded.Values
+            var loaded = await AppData.LoadAccounts();
+
+            var selectedUsers = from user in loaded.Values
                                 where user.Email == mail
                                 select user;
             return selectedUsers.First();
         }
 
-        public static AccountModel Find(string uuid)
+        public async Task<AccountModel> Find(string uuid)
         {
+            var loaded = await AppData.LoadAccounts();
+
             if (uuid == null)
                 return null;
-            if (Loaded.ContainsKey(uuid))
-                return Loaded[uuid];
+            if (loaded.ContainsKey(uuid))
+                return loaded[uuid];
             else
                 return null;
         }
@@ -128,31 +134,31 @@ namespace HyperCube.Models
             return 0;
         }
 
-        public static async void InitializeAccounts()
-        {
-            Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
-            Console.WriteLine("InitializeAccounts");
-            foreach (var acc in Loaded)
-            {
-                acc.Value.LoadRoles();
-                var wallets = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_wallets where account_uuid='{acc.Value.UUID}'");
-
-                if (wallets.Count > 0)
-                {
-                    foreach (var wallet in wallets)
-                    {
-                        var bc_id = Convert.ToInt32(wallet["blockchain_id"]);
-                        var wallet_id = Convert.ToString(wallet["uuid"]);
-                        if (bc_id == 0)
-                            acc.Value.eth_address = wallet_id;
-                        else
-                            acc.Value.eth_address1 = wallet_id;
-                        Console.WriteLine($"acc {acc.Value.Name} wallet0 {acc.Value.eth_address} wallet1 {acc.Value.eth_address1}");
-                    }
-                }
-
-            }
-        }
+        //public static async void InitializeAccounts()
+        //{
+        //    Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
+        //    Console.WriteLine("InitializeAccounts");
+        //    foreach (var acc in Loaded)
+        //    {
+        //        acc.Value.LoadRoles();
+        //        var wallets = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_wallets where account_uuid='{acc.Value.UUID}'");
+
+        //        if (wallets.Count > 0)
+        //        {
+        //            foreach (var wallet in wallets)
+        //            {
+        //                var bc_id = Convert.ToInt32(wallet["blockchain_id"]);
+        //                var wallet_id = Convert.ToString(wallet["uuid"]);
+        //                if (bc_id == 0)
+        //                    acc.Value.eth_address = wallet_id;
+        //                else
+        //                    acc.Value.eth_address1 = wallet_id;
+        //                Console.WriteLine($"acc {acc.Value.Name} wallet0 {acc.Value.eth_address} wallet1 {acc.Value.eth_address1}");
+        //            }
+        //        }
+
+        //    }
+        //}
 
         public async void LoadRoles()
         {

+ 11 - 21
Models/ArticleModel.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel.DataAnnotations;
 using System.Threading.Tasks;
+using Microsoft.AspNetCore.Components;
 using Console = HyperCube.Utils.AdvConsole;
 
 namespace HyperCube.Models
@@ -23,20 +24,8 @@ namespace HyperCube.Models
 
     public class ArticleModel : ICloneable
     {
-        public static Dictionary<int, ArticleModel> articleModels;
-        public static async Task LoadArticles()
-        {
-            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";
-            articleModels = await dbCon.SQLSelectArticles(stringSQL);
-            //dbCon.Close();
-        }
-
+        [Inject]
+        public AppData AppData { get; set; }
 
         public int ID { get; set; }
         public string Filename { get; set; }
@@ -69,12 +58,13 @@ namespace HyperCube.Models
         private int? rating;
         private string initiatorUUID = "";
 
-        public static ArticleModel Find(int id)
+        public async Task<ArticleModel> Find(int id)
         {
-            if (id > 0) {
-
-                if (articleModels.ContainsKey(id))
-                    return articleModels[id];
+            if (id > 0)
+            {
+                var articles = await AppData.GetArticles();
+                if (articles.ContainsKey(id))
+                    return articles[id];
             }
             return null;
         }
@@ -91,7 +81,7 @@ namespace HyperCube.Models
                 stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
 
             int count = await dbCon.SQLSelectCount(stringSQL);
-            dbCon.Close();
+            //dbCon.Close();
             return count;
         }
 
@@ -109,7 +99,7 @@ namespace HyperCube.Models
                     $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Added}";
 
                 initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
-                dbCon.Close();                
+                //dbCon.Close();                
             }
             else
                 Console.WriteLine("initiatorUUID already set");

+ 4 - 0
Models/Blockchain.cs

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
 using Newtonsoft.Json;
 using Console = HyperCube.Utils.AdvConsole;
 using System.Text.RegularExpressions;
+using Microsoft.AspNetCore.Components;
 
 //0xe5D682717955d6C35d465A3485625C64655a04f4 - HCB in rinkeby
 
@@ -21,6 +22,9 @@ namespace HyperCube.Models
 
     public class Blockchain
     {
+        [Inject]
+        AppData AppData { get; set; }
+
         public static Blockchain Dev
         {
             get {

+ 4 - 23
Pages/Account.razor.cs

@@ -13,36 +13,17 @@ namespace HyperCube.Pages
         public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
         [Inject]
         public UserManager<IdentityUser> UserManager { get; set; }
+        [Inject]
+        AppData AppData { get; set; }
 
         private AccountModel account = new();
         string myBalance = "";
 
         protected override async Task OnInitializedAsync()
         {
-            account = await GetCurrentAcc();
+            account = AppData.CurrentAccount;
             if (Blockchain.GetMain() != null)
                 myBalance = await account.GetBalance();
-        }
-
-        private async Task<AccountModel> GetCurrentAcc()
-        {
-            AccountModel account = new();
-
-            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
-            var user = authState.User;
-
-            if (user.Identity.IsAuthenticated)
-            {
-                var currentUser = await UserManager.GetUserAsync(user);
-                var acc = AccountModel.Find(currentUser.Id);
-                if (acc != null)
-                    account = acc;
-
-                //account.AccRole = Role.User;
-                return account;
-            }
-
-            return null;
-        }
+        }        
     }
 }

+ 33 - 28
Pages/DocEdit.razor.cs

@@ -14,11 +14,14 @@ using System.Net;
 
 namespace HyperCube.Pages
 {
-    public partial class DocEdit
+    public partial class DocEdit : ComponentBase
     {
         [Parameter]
         public int docID { get; set; }
 
+        [Inject]
+        public AppData AppData { get; set; }
+
         const string FOLDER_NAME = "articles_storage";
         const long MAX_FILE_SIZE = 5120000; //bytes
 
@@ -60,7 +63,7 @@ namespace HyperCube.Pages
 
         protected override async Task OnInitializedAsync()
         {
-            currentAcc = await GetCurrentAcc();
+            currentAcc = AppData.CurrentAccount;
 
             string path = AppDomain.CurrentDomain.BaseDirectory+@"wwwroot";
             storageFolderPath = (Path.Combine(path, FOLDER_NAME));
@@ -79,8 +82,10 @@ namespace HyperCube.Pages
                 articleModelClone = await dbCon.SQLSelectArticle(stringSQL);
                 articleModel = (ArticleModel)articleModelClone.Clone();
 
-                string initiator = await articleModel.GetInitiatorUUID();
-                initiatorAcc = AccountModel.Find(initiator);
+                string initiatorID = await articleModel.GetInitiatorUUID();
+                var accounts = await AppData.LoadAccounts();
+                if(accounts.ContainsKey(initiatorID))
+                    initiatorAcc = accounts[initiatorID];
                 status = $"Article ID={docID} loaded, status: {articleModel.Status}, initiator: {initiatorAcc.Name}";
 
             }
@@ -197,7 +202,7 @@ namespace HyperCube.Pages
                 status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
 
                 DocParse docParse = new DocParse();
-                articleModelClone = DocParse.ReadPDF(memoryStream);
+                articleModelClone = docParse.ReadPDF(memoryStream);
                 articleModelClone.Filename = file.Name;
                 articleModel = (ArticleModel)articleModelClone.Clone();
             }
@@ -215,34 +220,34 @@ namespace HyperCube.Pages
 
         public async Task InitializeAccount()
         {
-            AccountModel.Current = await GetCurrentAcc();
-            Console.WriteLine("InitializeAccount in DocEdit " + AccountModel.Current.Name);
+            //AppData.CurrentAccount = await GetCurrentAcc();
+            Console.WriteLine($"InitializeAccount in DocEdit: {AppData.CurrentAccount.Name}");
         }
 
-        private async Task<AccountModel> GetCurrentAcc()
-        {
-            AccountModel account = new();
+        //private async Task<AccountModel> GetCurrentAcc()
+        //{
+        //    AccountModel account = new();
 
-            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
-            var user = authState.User;
+        //    var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+        //    var user = authState.User;
 
-            if (user.Identity.IsAuthenticated)
-            {
-                var currentUser = await UserManager.GetUserAsync(user);
-                account.UUID = currentUser.Id;
-                //account.Name = currentUser.UserName;
-                //account.Email = currentUser.Email;
-
-                var acc = AccountModel.Find(account.UUID);
-                if (acc != null)
-                    account = acc;
-                ///tmp
+        //    if (user.Identity.IsAuthenticated)
+        //    {
+        //        var currentUser = await UserManager.GetUserAsync(user);
+        //        account.UUID = currentUser.Id;
+        //        //account.Name = currentUser.UserName;
+        //        //account.Email = currentUser.Email;
 
-                //account.AccRole = Role.User;
-                return account;
-            }
+        //        var acc = AccountModel.Find(account.UUID);
+        //        if (acc != null)
+        //            account = acc;
+        //        ///tmp
 
-            return null;
-        }
+        //        //account.AccRole = Role.User;
+        //        return account;
+        //    }
+
+        //    return null;
+        //}
     }
 }

+ 4 - 30
Pages/Verifying.razor

@@ -1,10 +1,5 @@
 @page "/verifying"
 
-@using System.ComponentModel.DataAnnotations;
-@using System.Linq;
-@using System.Reflection;
-@using HyperCube.Models;
-
 @attribute [Authorize]
 
 <div class="tabs__content">
@@ -31,40 +26,19 @@
             </tr>
         </thead>
         <tbody>
-            @foreach (var articleModel in ArticleModel.articleModels)
+            @foreach (var article in articles)
             {
                 <tr>
                     <td>@(counter++)</td>
                     @*<td>@articleModel.Value.Filename</td>*@
-                    <td><a href="@($"/docedit/{articleModel.Key}")">@articleModel.Value.Name</a></td>
-                    <td>@articleModel.Value.PublishDate.ToString("d")</td>
+                    <td><a href="@($"/docedit/{article.Key}")">@article.Value.Name</a></td>
+                    <td>@article.Value.PublishDate.ToString("d")</td>
                     @*<td>@articleModel.Value.Authors</td>*@
                     <td><center> - </center></td>
                     <td><center> - </center></td>
-                    <td>@GetDisplayName(articleModel.Value.Status)</td>
+                    <td>@GetDisplayName(article.Value.Status)</td>
                 </tr>
             }
         </tbody>
     </table>
 </div>
-
-
-@code {
-
-    private int counter = 1;
-
-    protected override async Task OnInitializedAsync()
-    {
-
-        
-    }
-
-    private static string GetDisplayName(Enum enumValue)
-    {
-        return enumValue.GetType()
-                        .GetMember(enumValue.ToString())
-                        .First()
-                        .GetCustomAttribute<DisplayAttribute>()
-                        .GetName();
-    }
-}

+ 35 - 0
Pages/Verifying.razor.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.ComponentModel.DataAnnotations;
+using System.Reflection;
+using Microsoft.AspNetCore.Components;
+using HyperCube.Models;
+
+namespace HyperCube.Pages
+{
+    public partial class Verifying : ComponentBase
+    {
+        [Inject]
+        public AppData AppData { get; set; }
+
+        private Dictionary<int, ArticleModel> articles = new();
+        private int counter;
+
+        protected override async Task OnInitializedAsync()
+        {
+            articles = await AppData.GetArticles();
+            counter = 1;
+        }
+
+        private static string GetDisplayName(Enum enumValue)
+        {
+            return enumValue.GetType()
+                            .GetMember(enumValue.ToString())
+                            .First()
+                            .GetCustomAttribute<DisplayAttribute>()
+                            .GetName();
+        }
+    }
+}

+ 0 - 3
Pages/Wallet.razor

@@ -1,9 +1,6 @@
 @page "/wallet"
 
 @using HyperCube.Models;
-@using Microsoft.AspNetCore.Identity;
-@inject AuthenticationStateProvider AuthenticationStateProvider
-@inject UserManager<IdentityUser> UserManager;
 
 @attribute [Authorize]
 

+ 10 - 28
Pages/Wallet.razor.cs

@@ -1,42 +1,24 @@
 using System.Threading.Tasks;
+using Microsoft.AspNetCore.Components;
 using HyperCube.Models;
-using HyperCube;
 
 namespace HyperCube.Pages
 {
     public partial class Wallet
-    {
+    {      
+        [Inject]
+        public AppData AppData { get; set; }
+
         private AccountModel account = new();
         string myBalance = "";
 
         protected override async Task OnInitializedAsync()
         {
-            account = await GetCurrentAcc();
+            account = AppData.CurrentAccount;
             if (Blockchain.GetMain() != null)
                 myBalance = await account.GetBalance();
         }
 
-        private async Task<AccountModel> GetCurrentAcc()
-        {
-            AccountModel account = new();
-
-            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
-            var user = authState.User;
-
-            if (user.Identity.IsAuthenticated)
-            {
-                var currentUser = await UserManager.GetUserAsync(user);
-                var acc = AccountModel.Find(currentUser.Id);
-                if (acc != null)
-                    account = acc;
-
-                account.LoadRoles();
-                return account;
-            }
-
-            return null;
-        }
-
         bool admin
         {
             get { return account.Roles.Contains(Role.Admin); }
@@ -106,14 +88,14 @@ namespace HyperCube.Pages
             }
         }
 
-        void InsertRole(Role role)
+        private async Task InsertRole(Role role)
         {
-            MySQLConnector.Instance().SQLInsert($"insert into account_roles (account_uuid, role_id) values ('{account.UUID}', {(int)role})");
+            await MySQLConnector.Instance().SQLInsert($"insert into account_roles (account_uuid, role_id) values ('{account.UUID}', {(int)role})");
         }
 
-        void DeleteRole(Role role)
+        private async Task DeleteRole(Role role)
         {
-            MySQLConnector.Instance().SQLInsert($"delete from account_roles where account_uuid = '{account.UUID}' and role_id={(int)role}");
+            await MySQLConnector.Instance().SQLInsert($"delete from account_roles where account_uuid = '{account.UUID}' and role_id={(int)role}");
         }
     }
 }

+ 24 - 22
Shared/MainLayout.razor

@@ -5,6 +5,7 @@
 @using Microsoft.AspNetCore.Identity;
 @inject AuthenticationStateProvider AuthenticationStateProvider
 @inject UserManager<IdentityUser> UserManager
+@inject AppData AppData
 
 <AuthorizeView>
     <Authorized>
@@ -45,8 +46,9 @@
     {
         try
         {
-            AccountModel.Current = await GetCurrentAcc();
-            //await Blockchain.GetInstance().Initialize();
+            //AccountModel.Current = await GetCurrentAcc();
+            //await Blockchain.GetInstance().Initialize();            
+
             Console.WriteLine("OnInitializedAsync RegisterNetworks");
             await Blockchain.RegisterNetworks();
             if (Blockchain.Connected != "" && Blockchain.Connected != "none")
@@ -66,29 +68,29 @@
         }
     }
 
-    private async Task<AccountModel> GetCurrentAcc()
-    {
-        AccountModel account = new();
+    //private async Task<AccountModel> GetCurrentAcc()
+    //{
+    //    AccountModel account = new();
 
-        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
-        var user = authState.User;
+    //    var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+    //    var user = authState.User;
 
-        if (user.Identity.IsAuthenticated)
-        {
-            var currentUser = await UserManager.GetUserAsync(user);
-            account.UUID = currentUser.Id;
-            //account.Name = currentUser.UserName;
-            //account.Email = currentUser.Email;
+    //    if (user.Identity.IsAuthenticated)
+    //    {
+    //        var currentUser = await UserManager.GetUserAsync(user);
+    //        account.UUID = currentUser.Id;
+    //        //account.Name = currentUser.UserName;
+    //        //account.Email = currentUser.Email;
 
-            var acc = AccountModel.Find(account.UUID);
-            if (acc != null)
-                account = acc;
-            ///tmp
+    //        var acc = AccountModel.Find(account.UUID);
+    //        if (acc != null)
+    //            account = acc;
+    //        ///tmp
 
-            //account.AccRole = Role.User;
-            return account;
-        }
+    //        //account.AccRole = Role.User;
+    //        return account;
+    //    }
 
-        return null;
-    }
+    //    return null;
+    //}
 }

+ 3 - 35
Shared/Sidebar.razor.cs

@@ -15,6 +15,8 @@ namespace HyperCube.Shared
         public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
         [Inject]
         public UserManager<IdentityUser> UserManager { get; set; }
+        [Inject]
+        AppData AppData { get; set; }
 
         AccountModel account;
         List<Role> roles;
@@ -23,45 +25,11 @@ namespace HyperCube.Shared
 
         protected override async Task OnInitializedAsync()
         {
-            account = await GetCurrentAcc();
+            account = AppData.CurrentAccount;
             if (account != null)
                 account.RolesChanged += Change;
         }
 
-        public async Task InitializeAccount()
-        {
-            AccountModel.Current = await GetCurrentAcc();
-            Console.WriteLine("InitializeAccount in Sidebar " + AccountModel.Current.Name);
-            await Rerender();
-        }
-
-        private async Task<AccountModel> GetCurrentAcc()
-        {
-            AccountModel account = new();
-
-            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
-            var user = authState.User;
-
-            if (user.Identity.IsAuthenticated)
-            {
-                var currentUser = await UserManager.GetUserAsync(user);
-                account.UUID = currentUser.Id;
-                //account.Name = currentUser.UserName;
-                //account.Email = currentUser.Email;
-
-                var acc = AccountModel.Find(account.UUID);
-                if (acc != null)
-                    account = acc;
-                ///tmp
-
-                //account.AccRole = Role.User;
-                //account.Name = "test";
-                return account;
-            }
-
-            return null;
-        }
-
         private void Change(int count)
         {
             Console.WriteLine($"Sidebar role changed, count: {count}");

+ 4 - 2
Startup.cs

@@ -23,8 +23,8 @@ namespace HyperCube
         public Startup(IConfiguration configuration)
         {
             Configuration = configuration;
-            Models.AccountModel.InitializeAccounts();
-            Models.ArticleModel.LoadArticles();
+            //Models.AccountModel.InitializeAccounts();
+            //Models.ArticleModel.LoadArticles();            
         }
 
         public IConfiguration Configuration { get; }
@@ -44,6 +44,8 @@ namespace HyperCube
             services.AddDatabaseDeveloperPageExceptionFilter();
             services.AddSingleton<WeatherForecastService>();
             services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);
+
+            services.AddScoped<AppData>();
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.