DocEdit.razor.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using Microsoft.AspNetCore.Components;
  2. using Microsoft.AspNetCore.Components.Authorization;
  3. using Microsoft.AspNetCore.Components.Forms;
  4. using Microsoft.JSInterop;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Threading.Tasks;
  10. using HyperCube.Models;
  11. namespace HyperCube.Pages
  12. {
  13. public partial class DocEdit
  14. {
  15. [Parameter]
  16. public int docID { get; set; }
  17. const string FOLDER_NAME = "articles_storage";
  18. const long MAX_FILE_SIZE = 5120000; //bytes
  19. string transactionId;
  20. ArticleModel articleModelClone = new();
  21. ArticleModel articleModel = new();
  22. AccountModel currentAcc = new();
  23. AccountModel initiatorAcc = new();
  24. string status;
  25. string header;
  26. string storageFolderPath;
  27. MemoryStream memoryStream;
  28. Modal modal { get; set; }
  29. int editsCount;
  30. async Task<string> Verify()
  31. {
  32. try
  33. {
  34. VerifyContract verifyContract = SmartContract.Find("Verify") as VerifyContract;
  35. if (verifyContract != null)
  36. {
  37. Console.WriteLine($"VerifyContract found");
  38. transactionId = await verifyContract.Run(articleModel);
  39. return transactionId;
  40. }
  41. else
  42. Console.WriteLine($"VerifyContract null");
  43. }
  44. catch (Exception e)
  45. {
  46. Console.WriteLine(e.Message + "stack trace" + e.StackTrace);
  47. }
  48. return "Verify failed";
  49. }
  50. protected override async Task OnInitializedAsync()
  51. {
  52. currentAcc = await GetCurrentAcc();
  53. string path = AppDomain.CurrentDomain.BaseDirectory;
  54. storageFolderPath = (Path.Combine(path, FOLDER_NAME));
  55. if (docID > 0)
  56. {
  57. header = "Проверка материала";
  58. MySQLConnector dbCon = MySQLConnector.Instance();
  59. string stringSQL = $"SELECT articles.id, filename, article_name, authors, date_publish, annotation, keywords, action_type, rating " +
  60. $"FROM articles " +
  61. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  62. $"WHERE articles.id={docID} " +
  63. $"ORDER BY actions_history.id DESC LiMIT 1";
  64. articleModelClone = await dbCon.SQLSelectArticle(stringSQL);
  65. articleModel = (ArticleModel)articleModelClone.Clone();
  66. string initiator = await articleModel.GetInitiatorUUID();
  67. initiatorAcc = AccountModel.Find(initiator);
  68. status = $"Article ID={docID} loaded, status: {articleModel.Status}, initiator: {initiatorAcc.Name}";
  69. }
  70. else
  71. header = "Загрузка материала";
  72. await InitializeAccount();
  73. //int count = await articleModel.GetEditsCount();
  74. //int countbyid = await articleModel.GetEditsCount(currentAcc.UUID);
  75. //header += $", uuid:{currentAcc.UUID}, name: {currentAcc.Name}, edits count:{count}, count by accid: {countbyid}";
  76. }
  77. private async Task HandleValidSubmit()
  78. {
  79. MySQLConnector dbCon = MySQLConnector.Instance();
  80. long id = 0;
  81. string stringSQL;
  82. if (docID > 0)
  83. {
  84. id = docID;
  85. stringSQL = $"UPDATE articles " +
  86. $"SET filename='{articleModel.Filename}', article_name='{articleModel.Name}', authors='{articleModel.Authors}', " +
  87. $"date_publish='{articleModel.PublishDate.ToString("yyyy-MM-dd")}', annotation='{articleModel.Annotation}', " +
  88. $"keywords='{articleModel.Keywords}', rating={articleModel.Rating} " +
  89. $"WHERE id={docID}";
  90. dbCon.SQLInsert(stringSQL);
  91. }
  92. else
  93. {
  94. stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords) " +
  95. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  96. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  97. id = dbCon.SQLInsert(stringSQL);
  98. }
  99. ///temp
  100. int action_type = docID > 0 ? 2 : 1;
  101. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
  102. $"VALUES ('{id}', '{action_type}', '{currentAcc.UUID}')";
  103. dbCon.SQLInsert(stringSQL);
  104. Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(articleModel, articleModelClone);
  105. foreach (KeyValuePair<string, PropertyInfo> prop in propDict)
  106. {
  107. //Console.WriteLine($"property name: {prop.Key}, value: {prop.Value.GetValue(articleModel, null)}");
  108. stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
  109. $"VALUES ('{id}', '{currentAcc.UUID}', '{prop.Key}', '{prop.Value.GetValue(articleModelClone, null)}', '{prop.Value.GetValue(articleModel, null)}')";
  110. dbCon.SQLInsert(stringSQL);
  111. }
  112. dbCon.Close();
  113. if (docID > 0)
  114. {
  115. status = propDict.Count > 0 ? "All changes saved, article has veryfied." : "Article verifyed without any changes.";
  116. transactionId = await Verify();
  117. Console.WriteLine("transactionId found " + transactionId);
  118. ///tmp
  119. editsCount = await articleModel.GetEditsCount(currentAcc.UUID);
  120. modal.Open();
  121. }
  122. else
  123. {
  124. string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
  125. Directory.CreateDirectory(storageFolderPath);
  126. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  127. memoryStream.Position = 0;
  128. await memoryStream.CopyToAsync(fs);
  129. Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}, memory size:{memoryStream.Length}b, file size: {fs.Length}b");
  130. memoryStream.Close();
  131. fs.Close();
  132. status = "New article data saved.";
  133. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
  134. if (confirmed)
  135. NavigationManager.NavigateTo("docedit", true);
  136. else
  137. NavigationManager.NavigateTo("");
  138. }
  139. }
  140. private async Task HandleSelection(InputFileChangeEventArgs e)
  141. {
  142. IBrowserFile file = e.File;
  143. if (file != null)
  144. {
  145. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  146. memoryStream = new();
  147. await stream.CopyToAsync(memoryStream);
  148. status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
  149. DocParse docParse = new DocParse();
  150. articleModelClone = DocParse.ReadPDF(memoryStream);
  151. articleModelClone.Filename = file.Name;
  152. articleModel = (ArticleModel)articleModelClone.Clone();
  153. }
  154. }
  155. private async Task Cancel()
  156. {
  157. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
  158. if (confirmed)
  159. {
  160. ///какая-то логика отмены...
  161. NavigationManager.NavigateTo("");
  162. }
  163. }
  164. public async Task InitializeAccount()
  165. {
  166. AccountModel.Current = await GetCurrentAcc();
  167. Console.WriteLine("InitializeAccount in DocEdit " + AccountModel.Current.Name);
  168. }
  169. private async Task<AccountModel> GetCurrentAcc()
  170. {
  171. AccountModel account = new();
  172. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  173. var user = authState.User;
  174. if (user.Identity.IsAuthenticated)
  175. {
  176. var currentUser = await UserManager.GetUserAsync(user);
  177. account.UUID = currentUser.Id;
  178. //account.Name = currentUser.UserName;
  179. //account.Email = currentUser.Email;
  180. var acc = AccountModel.Find(account.UUID);
  181. if (acc != null)
  182. account = acc;
  183. ///tmp
  184. account.AccRole = Role.User;
  185. return account;
  186. }
  187. return null;
  188. }
  189. }
  190. }