Browse Source

Merge branch 'hybrid' of http://dev.prmsys.net:3001/Rimmon/HyperCube into hybrid

Rimmon 3 years ago
parent
commit
12cbb5db79
10 changed files with 145 additions and 105 deletions
  1. 78 0
      AppData.cs
  2. 4 19
      Models/ArticleModel.cs
  3. 8 14
      Models/Blockchain.cs
  4. 19 38
      MySQLConnector.cs
  5. 1 2
      Pages/DocEdit.razor
  6. 7 4
      Pages/DocEdit.razor.cs
  7. 2 26
      Pages/Verifying.razor
  8. 24 0
      Pages/Verifying.razor.cs
  9. 1 1
      Shared/Sidebar.razor.cs
  10. 1 1
      Startup.cs

+ 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];                
+            }            
+        }
+    }
+}

+ 4 - 19
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;
         }
@@ -91,7 +76,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 +94,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");

+ 8 - 14
Models/Blockchain.cs

@@ -183,22 +183,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;
         }
 

+ 19 - 38
MySQLConnector.cs

@@ -29,29 +29,10 @@ namespace HyperCube
         }
 
         public async Task<bool> IsConnect()
-        {
-           
+        {           
             string connstring = $"Server={Server}; database={DatabaseName}; UID={UserName}; password={Password}";
             if (Connection != null)
-            {
-                Console.WriteLine($"IsConnect {Connection.State}");
-                //try
-                //{
-                    await SQLSelectComplex("select COUNT(*) from accounts", false);
-                //    }
-                //    catch (Exception e)
-                //    {
-                //        Console.WriteLine("SQL Exception " + e.Message + "stack trace" + e.StackTrace);
-                //        Console.WriteLine($"catch SQL IsConnect making NEW Connection!");
-                //        Connection = new MySqlConnection(connstring);
-                //        Connection.Open();
-                //    }
-                //    //finally
-                //    //{
-
-                //    //}
-                //    Console.WriteLine($"SQL IsConnect state: {Connection.State}");
-            }
+                await SQLSelectComplex("select COUNT(*) from accounts", false);
             else
                 Console.WriteLine($"SQL IsConnect Connection null");
 
@@ -109,7 +90,6 @@ namespace HyperCube
                     //if (!rdr.IsDBNull(15))
                     //    newacc.eth_address = rdr.GetString(15);
 
-
                     if (!accs.ContainsKey(newacc.UUID))
                         accs.Add(newacc.UUID, newacc);
                     newacc.bsel = rdr.GetByte(17);
@@ -182,7 +162,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 +173,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 +188,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,23 +223,23 @@ namespace HyperCube
             bool connected = true;
             if (check)
             {
-                Console.WriteLine($"SQLSelectComplex check");
+                //Console.WriteLine($"SQLSelectComplex check");
                 connected = await IsConnect();
             }
             if (connected)
             {
-                Console.WriteLine($"SQLSelectComplex connected");
-                List<Dictionary<string, object>> retval = new List<Dictionary<string, object>>();
+                //Console.WriteLine($"SQLSelectComplex connected");
+                List<Dictionary<string, object>> retval = new();
                 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");
-                        Dictionary<string, object> data = new Dictionary<string, object>();
+                        //Console.WriteLine($"SQLSelectComplex Reader.Read");
+                        Dictionary<string, object> data = new();
 
                         for (int i = 0; i < Reader.FieldCount; i++)
                         {
@@ -286,7 +267,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="Дата издания" />

+ 7 - 4
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; }
@@ -133,9 +133,9 @@ namespace HyperCube.Pages
                     $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
                 id = await dbCon.SQLInsert(stringSQL);
                 NewProjectSmopp(id);
-            }
+            }          
 
-            ///temp
+            /// tmp
             int action_type = docID > 0 ? 2 : 1;
 
             stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
@@ -152,7 +152,7 @@ namespace HyperCube.Pages
                 await dbCon.SQLInsert(stringSQL);
             }
 
-            dbCon.Close();
+            //dbCon.Close();
 
             if (docID > 0)
             {
@@ -184,6 +184,9 @@ namespace HyperCube.Pages
                 else
                     NavigationManager.NavigateTo("");
             }
+
+            /// reloading articles
+            await AppData.LoadArticles();
         }
 
         private async Task HandleSelection(InputFileChangeEventArgs e)

+ 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; }