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

+ 78 - 0
AppData.cs

@@ -0,0 +1,78 @@
+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];                
+            }            
+        }
+    }
+}

+ 2 - 17
Models/ArticleModel.cs

@@ -23,21 +23,6 @@ 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();
-        }
-
-
         public int ID { get; set; }
         public string Filename { get; set; }
         public string FilenameReal { get { return ID + "_" + Filename; } }
@@ -73,8 +58,8 @@ namespace HyperCube.Models
         {
             if (id > 0) {
 
-                if (articleModels.ContainsKey(id))
-                    return articleModels[id];
+                if (AppData.Articles.ContainsKey(id))
+                    return AppData.Articles[id];
             }
             return null;
         }

+ 8 - 14
Models/Blockchain.cs

@@ -169,22 +169,16 @@ namespace HyperCube.Models
         }
 
         public static Blockchain GetMain()
-        {            
-            try
-            {
-                if (loaded.Count > AccountModel.GetCurrent().blockchain_selected)
-                {
-                    var bc = loaded[AccountModel.GetCurrent().blockchain_selected];
-                    Console.WriteLine($"GetMain blockchain_selected {AccountModel.GetCurrent().blockchain_selected} " + bc.address);
-                    return bc;
-                }
-                else
-                    Console.WriteLine($"Error: blockchains loaded {loaded.Count}");
-            }
-            catch (Exception e)
+        {
+            if (loaded.Count > AccountModel.GetCurrent()?.blockchain_selected)
             {
-                Console.WriteLine("GetMain exception " + e.Message + ", stack trace:" + e.StackTrace);
+                var bc = loaded[AccountModel.GetCurrent().blockchain_selected];
+                Console.WriteLine($"GetMain blockchain_selected {AccountModel.GetCurrent().blockchain_selected} " + bc.address);
+                return bc;
             }
+            else
+                Console.WriteLine($"Error: blockchains loaded {loaded.Count}");
+
             return null;
         }
 

+ 16 - 15
MySQLConnector.cs

@@ -34,7 +34,7 @@ namespace HyperCube
             string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
             if (Connection != null)
             {
-                Console.WriteLine($"IsConnect {Connection.State}");
+                //Console.WriteLine($"IsConnect {Connection.State}");
                 //try
                 //{
                     await SQLSelectComplex("select COUNT(*) from accounts", false);
@@ -182,7 +182,7 @@ namespace HyperCube
             //return null;
             Console.WriteLine($"SQLSelectArticles {sql}");
             Dictionary<int, ArticleModel> articleModels = new();
-            Models.ArticleModel articleModel;
+            ArticleModel articleModel;
 
             bool connected = await IsConnect();
             if (connected)
@@ -193,6 +193,7 @@ namespace HyperCube
                 bool stop = false;
                 while (rdr.Read()) ///id, filename, article_name, date_publish, action_type/status
                 {
+                    /// tmp
                     for (int i = 0; i < rdr.FieldCount; i++)
                     {
                         if (rdr.IsDBNull(i))
@@ -207,26 +208,26 @@ namespace HyperCube
                         stop = false;
                         continue;
                     }
+
                     articleModel = new();
                     articleModel.ID = rdr.GetInt32(0);
                     articleModel.Filename = rdr.GetString(1);
-                    articleModel.Name = rdr.GetString(2);
+                    if (!rdr.IsDBNull(2)) articleModel.Name = rdr.GetString(2); else articleModel.Name = "NULL";
                     articleModel.PublishDate = rdr.GetDateTime(3);
                     articleModel.Authors = rdr.GetString(4);
-                    articleModel.Status = (ArticleStatus)rdr.GetInt32(5);
+                    if (!rdr.IsDBNull(5)) articleModel.Status = (ArticleStatus)rdr.GetInt32(5); else articleModel.Status = ArticleStatus.New;
 
                     if (articleModels.ContainsKey(articleModel.ID))
                         continue;
-
-                    Console.WriteLine($"SQLSelectArticles count {count} model id {articleModel.ID}");
+                    
                     count++;
 
                     articleModels.Add(articleModel.ID, articleModel);
-
                 }
-                Console.WriteLine($"SQLSelectArticles2");
+
+                Console.WriteLine($"SQLSelectArticles count: {count}");
                 rdr.Close();
-                //await Task.WhenAll();
+                
                 return articleModels;
             }
             else
@@ -242,22 +243,22 @@ namespace HyperCube
             bool connected = true;
             if (check)
             {
-                Console.WriteLine($"SQLSelectComplex check");
+                //Console.WriteLine($"SQLSelectComplex check");
                 connected = await IsConnect();
             }
             if (connected)
             {
-                Console.WriteLine($"SQLSelectComplex connected");
+                //Console.WriteLine($"SQLSelectComplex connected");
                 List<Dictionary<string, object>> retval = new List<Dictionary<string, object>>();
                 MySqlCommand SQLcom2 = new(request, Connection);
-                Console.WriteLine($"SQLSelectComplex new SQLcom");
+                //Console.WriteLine($"SQLSelectComplex new SQLcom");
                 //try
                 //{
                 var Reader = await SQLcom2.ExecuteReaderAsync();
-                Console.WriteLine($"SQLSelectComplex ExecuteReader");
+                //Console.WriteLine($"SQLSelectComplex ExecuteReader");
                 while (Reader.Read())
                     {
-                        Console.WriteLine($"SQLSelectComplex Reader.Read");
+                        //Console.WriteLine($"SQLSelectComplex Reader.Read");
                         Dictionary<string, object> data = new Dictionary<string, object>();
 
                         for (int i = 0; i < Reader.FieldCount; i++)
@@ -286,7 +287,7 @@ namespace HyperCube
                         retval.Add(data);
                     }
                     
-                Console.WriteLine($"SQLSelectComplex Reader.Close");
+                //Console.WriteLine($"SQLSelectComplex Reader.Close");
                 Reader.Close();
                 await Task.WhenAll();
                 //}

+ 1 - 2
Pages/DocEdit.razor

@@ -31,8 +31,7 @@
                 }
                 else
                 {
-                    <p>Исходный документ: <a href=@fullName download target="_top">@articleModel.Filename</a></p>
-                    <p>Исходный документ: @*<a href=@FOLDER_NAME download="@articleModel.Filename" target="_top">@articleModel.Filename</a>*@</p>
+                    <p>Исходный документ: <a href=@fullName download target="_top">@articleModel.Filename</a></p>                    
                 }
                 <InputText type="text" class="form-control upload__input" id="article_name" @bind-Value="articleModel.Name" placeholder="Наименование статьи" />
                 <InputDate type="date" class="form-control upload__input" id="date_publish" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" />

+ 1 - 1
Pages/DocEdit.razor.cs

@@ -14,7 +14,7 @@ using System.Net;
 
 namespace HyperCube.Pages
 {
-    public partial class DocEdit
+    public partial class DocEdit : ComponentBase
     {
         [Parameter]
         public int docID { get; set; }

+ 2 - 26
Pages/Verifying.razor

@@ -1,8 +1,5 @@
 @page "/verifying"
 
-@using System.ComponentModel.DataAnnotations;
-@using System.Linq;
-@using System.Reflection;
 @using HyperCube.Models;
 
 @attribute [Authorize]
@@ -31,7 +28,7 @@
             </tr>
         </thead>
         <tbody>
-            @foreach (var articleModel in ArticleModel.articleModels)
+            @foreach (var articleModel in AppData.Articles)
             {
                 <tr>
                     <td>@(counter++)</td>
@@ -46,25 +43,4 @@
             }
         </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();
-    }
-}
+</div>

+ 24 - 0
Pages/Verifying.razor.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Components;
+
+namespace HyperCube.Pages
+{
+    public partial class Verifying : ComponentBase
+    {
+        private int counter = 1;
+
+        private static string GetDisplayName(Enum enumValue)
+        {
+            return enumValue.GetType()
+                            .GetMember(enumValue.ToString())
+                            .First()
+                            .GetCustomAttribute<DisplayAttribute>()
+                            .GetName();
+        }
+    }
+}

+ 1 - 1
Shared/Sidebar.razor.cs

@@ -9,7 +9,7 @@ using Console = HyperCube.Utils.AdvConsole;
 
 namespace HyperCube.Shared
 {
-    public partial class Sidebar
+    public partial class Sidebar : ComponentBase
     {
         [Inject]
         public AuthenticationStateProvider AuthenticationStateProvider { get; set; }

+ 1 - 1
Startup.cs

@@ -24,7 +24,7 @@ namespace HyperCube
         {
             Configuration = configuration;
             Models.AccountModel.InitializeAccounts();
-            Models.ArticleModel.LoadArticles();
+            //Models.ArticleModel.LoadArticles();
         }
 
         public IConfiguration Configuration { get; }