DocEdit.razor 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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="" @onclick="@DownloadDoc" @onclick:preventDefault>@articleModel.Filename</a></p>
  26. <p>Исходный документ: <a href=@FOLDER_NAME download="@articleModel.Filename" target="_top">@articleModel.Filename</a></p>
  27. }
  28. <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
  29. <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
  30. <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
  31. <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
  32. <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
  33. <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
  34. @if (docID < 1)
  35. {
  36. <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
  37. }
  38. else
  39. {
  40. <p>
  41. <button class="btn btn-danger" type="button" @onclick="@Cancel">Отклонить</button>
  42. <button class="btn btn-primary" type="submit">Утвердить</button>
  43. </p>
  44. }
  45. <p>Статус: @status</p>
  46. </div>
  47. </EditForm>
  48. @code {
  49. [Parameter]
  50. public int docID { get; set; }
  51. const string FOLDER_NAME = "articles_storage";
  52. const long MAX_FILE_SIZE = 5120000; //bytes
  53. const int ACC_ID = 1; //temp
  54. ArticleModel oldArticleModel = new();
  55. ArticleModel articleModel = new();
  56. string status;
  57. string header;
  58. string storageFolderPath;
  59. MemoryStream memoryStream;
  60. protected override async Task OnInitializedAsync()
  61. {
  62. string path = AppDomain.CurrentDomain.BaseDirectory;
  63. storageFolderPath = (Path.Combine(path, FOLDER_NAME));
  64. if (docID > 0)
  65. {
  66. header = "Проверка материала";
  67. MySQLConnector dbCon = MySQLConnector.Instance();
  68. string stringSQL = $"SELECT id, filename, article_name, authors, date_publish, annotation, keywords " +
  69. $"FROM articles " +
  70. $"WHERE id={docID}";
  71. oldArticleModel = await dbCon.SQLSelectArticle(stringSQL);
  72. articleModel = (ArticleModel)oldArticleModel.Clone();
  73. status = $"Article ID={docID} loaded.";
  74. }
  75. else
  76. header = "Загрузка материала";
  77. AccountModel account = await GetCurrentAcc();
  78. //header += $", uuid:{account.UUID}, name: {account.Name}";
  79. }
  80. private async void HandleValidSubmit()
  81. {
  82. MySQLConnector dbCon = MySQLConnector.Instance();
  83. long id = 0;
  84. string stringSQL;
  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}', keywords='{articleModel.Keywords}' " +
  91. $"WHERE id={docID}";
  92. dbCon.SQLInsert(stringSQL);
  93. }
  94. else
  95. {
  96. stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords) " +
  97. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  98. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  99. id = dbCon.SQLInsert(stringSQL);
  100. }
  101. int action_type = docID > 0 ? 2 : 1;
  102. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
  103. $"VALUES ('{id}', '{action_type}', '{ACC_ID}')";
  104. dbCon.SQLInsert(stringSQL);
  105. Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(articleModel, oldArticleModel);
  106. foreach (KeyValuePair<string, PropertyInfo> prop in propDict)
  107. {
  108. //Console.WriteLine($"property name: {prop.Key}, value: {prop.Value.GetValue(articleModel, null)}");
  109. stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
  110. $"VALUES ('{id}', '{ACC_ID}', '{prop.Key}', '{prop.Value.GetValue(oldArticleModel, null)}', '{prop.Value.GetValue(articleModel, null)}')";
  111. dbCon.SQLInsert(stringSQL);
  112. }
  113. dbCon.Close();
  114. if (docID > 0)
  115. {
  116. status = propDict.Count > 0 ? "All changes saved, article has veryfied." : "Article verifyed without any changes.";
  117. }
  118. else
  119. {
  120. string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
  121. Directory.CreateDirectory(storageFolderPath);
  122. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  123. memoryStream.Position = 0;
  124. await memoryStream.CopyToAsync(fs);
  125. Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}, memory size:{memoryStream.Length}b, file size: {fs.Length}b");
  126. memoryStream.Close();
  127. fs.Close();
  128. status = "New article data saved.";
  129. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
  130. if (confirmed)
  131. NavigationManager.NavigateTo("docedit", true);
  132. else
  133. NavigationManager.NavigateTo("");
  134. }
  135. }
  136. private async Task HandleSelection(InputFileChangeEventArgs e)
  137. {
  138. IBrowserFile file = e.File;
  139. if (file != null)
  140. {
  141. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  142. memoryStream = new();
  143. await stream.CopyToAsync(memoryStream);
  144. status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
  145. DocParse docParse = new DocParse();
  146. oldArticleModel = DocParse.ReadPDF(memoryStream);
  147. oldArticleModel.Filename = file.Name;
  148. articleModel = (ArticleModel)oldArticleModel.Clone();
  149. }
  150. }
  151. private async void Cancel()
  152. {
  153. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
  154. if (confirmed)
  155. {
  156. ///какая-то логика отмены...
  157. NavigationManager.NavigateTo("");
  158. }
  159. }
  160. private async Task<AccountModel> GetCurrentAcc()
  161. {
  162. AccountModel account = new();
  163. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  164. var user = authState.User;
  165. if (user.Identity.IsAuthenticated)
  166. {
  167. var currentUser = await UserManager.GetUserAsync(user);
  168. account.UUID = currentUser.Id;
  169. account.Name = currentUser.UserName;
  170. account.Email = currentUser.Email;
  171. ///tmp
  172. account.AccRole = Role.User;
  173. return account;
  174. }
  175. return null;
  176. }
  177. }