DocEdit.razor 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. @page "/docedit"
  2. @page "/docedit/{docID:int}"
  3. @using System.Reflection;
  4. @using HyperCube.Models;
  5. @inject NavigationManager NavigationManager;
  6. @inject IJSRuntime JsRuntime;
  7. @attribute [Authorize]
  8. @*@attribute [Authorize(Roles = "admin")]*@
  9. @inject AuthenticationStateProvider AuthenticationStateProvider
  10. @using Microsoft.AspNetCore.Identity;
  11. @inject UserManager<IdentityUser> UserManager;
  12. <EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
  13. <DataAnnotationsValidator />
  14. <ValidationSummary />
  15. <h1>@header</h1>
  16. <br>
  17. <div style="width: 50%;">
  18. @if (docID < 1)
  19. {
  20. <p><InputFile id="inputDefault" OnChange="@HandleSelection" accept="application/pdf" /></p>
  21. //accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  22. }
  23. else
  24. {
  25. <p>Исходный документ: <a href=@FOLDER_NAME download="@articleModel.Filename" target="_top">@articleModel.Filename</a></p>
  26. }
  27. <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
  28. <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
  29. <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
  30. <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
  31. <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
  32. <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
  33. @if (docID < 1)
  34. {
  35. <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
  36. }
  37. else
  38. {
  39. @*<p>
  40. <InputRadioGroup @bind-Value="articleModel.Status">
  41. Оценка:
  42. <br>
  43. @foreach (var status in (ArticleStatus[])Enum.GetValues(typeof(ArticleStatus)))
  44. {
  45. <InputRadio Value="status" />
  46. @status
  47. <br />
  48. }
  49. </InputRadioGroup>
  50. </p>*@
  51. <p>
  52. <InputRadioGroup @bind-Value="articleModel.Rating">
  53. Оценка:
  54. <br>
  55. @for (int i = 1; i < 6; i++)
  56. {
  57. <InputRadio Value="i" />
  58. @i
  59. <br />
  60. }
  61. </InputRadioGroup>
  62. </p>
  63. <p>
  64. <button class="btn btn-danger" type="button" @onclick="@Cancel">Отклонить</button>
  65. <button @onclick="Verify" class="btn btn-primary" type="submit">Утвердить</button>
  66. </p>
  67. }
  68. <p>Статус: @status</p>
  69. </div>
  70. </EditForm>
  71. @code {
  72. [Parameter]
  73. public int docID { get; set; }
  74. const string FOLDER_NAME = "articles_storage";
  75. const long MAX_FILE_SIZE = 5120000; //bytes
  76. //const int ACC_ID = 1; //temp
  77. ArticleModel oldArticleModel = new();
  78. ArticleModel articleModel = new();
  79. AccountModel currentAcc = new();
  80. string status;
  81. string header;
  82. string storageFolderPath;
  83. MemoryStream memoryStream;
  84. async Task Verify()
  85. {
  86. try
  87. {
  88. VerifyContract verifyContract = SmartContract.Find("Verify") as VerifyContract;
  89. if (verifyContract != null)
  90. {
  91. await verifyContract.Run(articleModel);
  92. }
  93. }
  94. catch (Exception e)
  95. {
  96. Console.WriteLine(e.Message);
  97. }
  98. }
  99. protected override async Task OnInitializedAsync()
  100. {
  101. currentAcc = await GetCurrentAcc();
  102. string path = AppDomain.CurrentDomain.BaseDirectory;
  103. storageFolderPath = (Path.Combine(path, FOLDER_NAME));
  104. if (docID > 0)
  105. {
  106. header = "Проверка материала";
  107. MySQLConnector dbCon = MySQLConnector.Instance();
  108. string stringSQL = $"SELECT articles.id, filename, article_name, authors, date_publish, annotation, keywords, action_type, rating " +
  109. $"FROM articles " +
  110. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  111. $"WHERE articles.id={docID} " +
  112. $"ORDER BY actions_history.id DESC LiMIT 1";
  113. oldArticleModel = await dbCon.SQLSelectArticle(stringSQL);
  114. articleModel = (ArticleModel)oldArticleModel.Clone();
  115. string initiator = await articleModel.GetInitiatorUUID();
  116. status = $"Article ID={docID} loaded, status: {articleModel.Status}, initiatorUUID: {await articleModel.GetInitiatorUUID()}";
  117. }
  118. else
  119. header = "Загрузка материала";
  120. InitializeAccount();
  121. //int count = await articleModel.GetEditsCount();
  122. //int countbyid = await articleModel.GetEditsCount(currentAcc.UUID);
  123. //header += $", uuid:{currentAcc.UUID}, name: {currentAcc.Name}, edits count:{count}, count by accid: {countbyid}";
  124. }
  125. private async void HandleValidSubmit()
  126. {
  127. MySQLConnector dbCon = MySQLConnector.Instance();
  128. long id = 0;
  129. string stringSQL;
  130. if (docID > 0)
  131. {
  132. id = docID;
  133. stringSQL = $"UPDATE articles " +
  134. $"SET filename='{articleModel.Filename}', article_name='{articleModel.Name}', authors='{articleModel.Authors}', " +
  135. $"date_publish='{articleModel.PublishDate.ToString("yyyy-MM-dd")}', annotation='{articleModel.Annotation}', " +
  136. $"keywords='{articleModel.Keywords}', rating={articleModel.Rating} " +
  137. $"WHERE id={docID}";
  138. dbCon.SQLInsert(stringSQL);
  139. }
  140. else
  141. {
  142. stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords) " +
  143. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  144. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  145. id = dbCon.SQLInsert(stringSQL);
  146. }
  147. ///temp
  148. int action_type = docID > 0 ? 2 : 1;
  149. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
  150. $"VALUES ('{id}', '{action_type}', '{currentAcc.UUID}')";
  151. dbCon.SQLInsert(stringSQL);
  152. Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(articleModel, oldArticleModel);
  153. foreach (KeyValuePair<string, PropertyInfo> prop in propDict)
  154. {
  155. //Console.WriteLine($"property name: {prop.Key}, value: {prop.Value.GetValue(articleModel, null)}");
  156. stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
  157. $"VALUES ('{id}', '{currentAcc.UUID}', '{prop.Key}', '{prop.Value.GetValue(oldArticleModel, null)}', '{prop.Value.GetValue(articleModel, null)}')";
  158. dbCon.SQLInsert(stringSQL);
  159. }
  160. dbCon.Close();
  161. if (docID > 0)
  162. {
  163. status = propDict.Count > 0 ? "All changes saved, article has veryfied." : "Article verifyed without any changes.";
  164. }
  165. else
  166. {
  167. string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
  168. Directory.CreateDirectory(storageFolderPath);
  169. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  170. memoryStream.Position = 0;
  171. await memoryStream.CopyToAsync(fs);
  172. Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}, memory size:{memoryStream.Length}b, file size: {fs.Length}b");
  173. memoryStream.Close();
  174. fs.Close();
  175. status = "New article data saved.";
  176. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
  177. if (confirmed)
  178. NavigationManager.NavigateTo("docedit", true);
  179. else
  180. NavigationManager.NavigateTo("");
  181. }
  182. }
  183. private async Task HandleSelection(InputFileChangeEventArgs e)
  184. {
  185. IBrowserFile file = e.File;
  186. if (file != null)
  187. {
  188. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  189. memoryStream = new();
  190. await stream.CopyToAsync(memoryStream);
  191. status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
  192. DocParse docParse = new DocParse();
  193. oldArticleModel = DocParse.ReadPDF(memoryStream);
  194. oldArticleModel.Filename = file.Name;
  195. articleModel = (ArticleModel)oldArticleModel.Clone();
  196. }
  197. }
  198. private async void Cancel()
  199. {
  200. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
  201. if (confirmed)
  202. {
  203. ///какая-то логика отмены...
  204. NavigationManager.NavigateTo("");
  205. }
  206. }
  207. public async void InitializeAccount()
  208. {
  209. AccountModel.Current = await GetCurrentAcc();
  210. Console.WriteLine("InitializeAccount in DocEdit "+ AccountModel.Current.Name);
  211. }
  212. private async Task<AccountModel> GetCurrentAcc()
  213. {
  214. AccountModel account = new();
  215. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  216. var user = authState.User;
  217. if (user.Identity.IsAuthenticated)
  218. {
  219. var currentUser = await UserManager.GetUserAsync(user);
  220. account.UUID = currentUser.Id;
  221. account.Name = currentUser.UserName;
  222. account.Email = currentUser.Email;
  223. var acc = AccountModel.Find(account.Email);
  224. if (acc != null)
  225. account = acc;
  226. ///tmp
  227. account.AccRole = Role.User;
  228. return account;
  229. }
  230. return null;
  231. }
  232. }