Преглед на файлове

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

Rimmon преди 4 години
родител
ревизия
4b625d70fe
променени са 5 файла, в които са добавени 85 реда и са изтрити 26 реда
  1. 5 10
      Models/AccountModel.cs
  2. 43 1
      Models/ArticleModel.cs
  3. 22 9
      MySQLConnector.cs
  4. 13 5
      Pages/DocEdit.razor
  5. 2 1
      Pages/Verifying.razor

+ 5 - 10
Models/AccountModel.cs

@@ -5,18 +5,13 @@ using System.Collections.Generic;
 
 namespace HyperCube.Models
 {
-    public enum Role { Admin = 0, Verifier, User }
+    public enum Role { Admin = 1, Verifier, User }
 
     public class AccountModel
     {
         public static Dictionary<string, AccountModel> Loaded = new();
         public static AccountModel Current;
         public string eth_address { get; set; }
-
-        /// <summary>
-        /// Internal ID
-        /// </summary>
-        public uint ID { get; set; }
         /// <summary>
         /// ASP Identity ID
         /// </summary>
@@ -34,12 +29,12 @@ namespace HyperCube.Models
             return Current;
         }
 
-        public static AccountModel Find(string email)
+        public static AccountModel Find(string uuid)
         {
-            if (email == null)
+            if (uuid == null)
                 return null;
-            if (Loaded.ContainsKey(email))
-                return Loaded[email];
+            if (Loaded.ContainsKey(uuid))
+                return Loaded[uuid];
             else
                 return null;
         }

+ 43 - 1
Models/ArticleModel.cs

@@ -1,6 +1,7 @@
 using System;
 using System.ComponentModel;
 using System.ComponentModel.DataAnnotations;
+using System.Threading.Tasks;
 
 namespace HyperCube.Models
 {
@@ -20,6 +21,7 @@ namespace HyperCube.Models
 
     public class ArticleModel : ICloneable
     {
+        public int ID { get; set; }
         public string Filename { get; set; }
         [Required]        
         public string Name { get; set; }        
@@ -31,7 +33,8 @@ namespace HyperCube.Models
         public string Keywords { get; set; }
         [Required]
         public string Annotation { get; set; }
-        public string Text { get; set; }
+        public string Text { get; set; }        
+
         public ArticleStatus Status { get; set; } = ArticleStatus.New;
         [Required]
         public int? Rating
@@ -46,6 +49,45 @@ namespace HyperCube.Models
         }
 
         private int? rating;
+        private string initiatorUUID = "";
+
+        public async Task<int> GetEditsCount(string acc_id = "")
+        {
+            MySQLConnector dbCon = MySQLConnector.Instance();
+
+            string stringSQL = $"SELECT COUNT(*) " +
+                $"FROM articles_edit_log ";
+            if (acc_id.Length < 36)
+                stringSQL += $"WHERE article_id={this.ID}";            
+            else
+                stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
+
+            int count = await dbCon.SQLSelectCount(stringSQL);
+            dbCon.Close();
+            return count;
+        }
+
+        public async Task<string> GetInitiatorUUID()
+        {
+            if (initiatorUUID.Length < 36)
+            {
+                Console.WriteLine("initiatorUUID is null, getting it from DB");
+
+                MySQLConnector dbCon = MySQLConnector.Instance();
+
+                string stringSQL = $"SELECT acc_id " +
+                    $"FROM articles " +
+                    $"JOIN actions_history ON actions_history.article_id = articles.id " +
+                    $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Added}";
+
+                initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
+                dbCon.Close();                
+            }
+            else
+                Console.WriteLine("initiatorUUID already set");
+
+            return initiatorUUID;
+        }
 
         public object Clone()
         {

+ 22 - 9
MySQLConnector.cs

@@ -72,14 +72,14 @@ namespace HyperCube
 
                 while (rdr.Read())
                 {
-                    AccountModel newacc = new AccountModel();
+                    AccountModel newacc = new ();
                     newacc.UUID = rdr.GetString(0); 
                     newacc.Name = rdr.GetString(1);
                     newacc.Email = rdr.GetString(3);
                     if (!rdr.IsDBNull(15))
                         newacc.eth_address = rdr.GetString(15);
-                    if (!accs.ContainsKey(newacc.Email))
-                        accs.Add(newacc.Email, newacc);
+                    if (!accs.ContainsKey(newacc.UUID))
+                        accs.Add(newacc.UUID, newacc);
                 }
 
                 await Task.WhenAll();
@@ -148,17 +148,17 @@ namespace HyperCube
                 while (rdr.Read()) ///id, filename, article_name, date_publish, action_type/status
                 {                    
                     articleModel = new();
-                    int id = rdr.GetInt32(0);
+                    articleModel.ID = rdr.GetInt32(0);
                     articleModel.Filename = rdr.GetString(1);
                     articleModel.Name = rdr.GetString(2);
                     articleModel.PublishDate = rdr.GetDateTime(3);
                     articleModel.Authors = rdr.GetString(4);
                     articleModel.Status = (ArticleStatus)rdr.GetInt32(5);
 
-                    if (articleModels.ContainsKey(id))
+                    if (articleModels.ContainsKey(articleModel.ID))
                         continue;
 
-                    articleModels.Add(id, articleModel);
+                    articleModels.Add(articleModel.ID, articleModel);
                 }
 
                 await Task.WhenAll();
@@ -183,6 +183,7 @@ namespace HyperCube
 
                 while (rdr.Read())  ///id, filename, article_name, authors, date_publish, annotation, keywords, action_type/status, rating
                 {
+                    articleModel.ID = rdr.GetInt32(0);
                     articleModel.Filename = rdr.GetString(1);
                     articleModel.Name = rdr.GetString(2);
                     articleModel.Authors = rdr.GetString(3);
@@ -202,15 +203,27 @@ namespace HyperCube
             return null;
         }
 
-        public uint SQLGetID(string sql)
+        public async Task<string> SQLSelectUUID(string sql)
         {
             bool connected = IsConnect();
             if (connected)
             {
                 SQLcom = new(sql, Connection);
-                object obj = SQLcom.ExecuteScalar();
+                object obj = await SQLcom.ExecuteScalarAsync();
+                return obj.ToString();
+            }
+            else return "";
+        }
 
-                return (uint)obj;
+        public async Task<int> SQLSelectCount(string sql)
+        {
+            bool connected = IsConnect();
+            if (connected)
+            {
+                SQLcom = new(sql, Connection);
+                object obj = await SQLcom.ExecuteScalarAsync();
+                int count = int.Parse(obj.ToString());
+                return count;
             }
             else return 0;
         }

+ 13 - 5
Pages/DocEdit.razor

@@ -79,10 +79,11 @@
 
     const string FOLDER_NAME = "articles_storage";
     const long MAX_FILE_SIZE = 5120000; //bytes
-    const int ACC_ID = 1; //temp
+    //const int ACC_ID = 1; //temp
 
     ArticleModel oldArticleModel = new();
     ArticleModel articleModel = new();
+    AccountModel currentAcc = new();
     string status;
     string header;
     string storageFolderPath;
@@ -107,6 +108,8 @@
 
     protected override async Task OnInitializedAsync()
     {
+        currentAcc = await GetCurrentAcc();
+
         string path = AppDomain.CurrentDomain.BaseDirectory;
         storageFolderPath = (Path.Combine(path, FOLDER_NAME));
 
@@ -124,14 +127,18 @@
             oldArticleModel = await dbCon.SQLSelectArticle(stringSQL);
             articleModel = (ArticleModel)oldArticleModel.Clone();
 
-            status = $"Article ID={docID} loaded, status: {articleModel.Status}";
+            string initiator = await articleModel.GetInitiatorUUID();
+            status = $"Article ID={docID} loaded, status: {articleModel.Status}, initiatorUUID: {await articleModel.GetInitiatorUUID()}";
         }
         else
             header = "Загрузка материала";
 
 
         InitializeAccount();
-        //header += $", uuid:{account.UUID}, name: {account.Name}";
+
+        //int count = await articleModel.GetEditsCount();
+        //int countbyid = await articleModel.GetEditsCount(currentAcc.UUID);
+        //header += $", uuid:{currentAcc.UUID}, name: {currentAcc.Name}, edits count:{count}, count by accid: {countbyid}";
     }
 
     private async void HandleValidSubmit()
@@ -158,10 +165,11 @@
             id = dbCon.SQLInsert(stringSQL);
         }
 
+        ///temp
         int action_type = docID > 0 ? 2 : 1;
 
         stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
-            $"VALUES ('{id}', '{action_type}', '{ACC_ID}')";
+            $"VALUES ('{id}', '{action_type}', '{currentAcc.UUID}')";
         dbCon.SQLInsert(stringSQL);
 
         Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(articleModel, oldArticleModel);
@@ -170,7 +178,7 @@
             //Console.WriteLine($"property name: {prop.Key}, value: {prop.Value.GetValue(articleModel, null)}");
 
             stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
-                $"VALUES ('{id}', '{ACC_ID}', '{prop.Key}', '{prop.Value.GetValue(oldArticleModel, null)}', '{prop.Value.GetValue(articleModel, null)}')";
+                $"VALUES ('{id}', '{currentAcc.UUID}', '{prop.Key}', '{prop.Value.GetValue(oldArticleModel, null)}', '{prop.Value.GetValue(articleModel, null)}')";
             dbCon.SQLInsert(stringSQL);
         }
 

+ 2 - 1
Pages/Verifying.razor

@@ -44,7 +44,8 @@
         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)";
+                $"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();