DocEdit.razor.cs 9.0 KB

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