浏览代码

article loading/editing

ganahrhr 4 年之前
父节点
当前提交
a9a51fcfdb
共有 6 个文件被更改,包括 197 次插入111 次删除
  1. 34 7
      MySQLConnector.cs
  2. 146 0
      Pages/DocEdit.razor
  3. 0 91
      Pages/DocsUpload.razor
  4. 15 12
      Pages/Verifying.razor
  5. 1 1
      Shared/NavMenu.razor
  6. 1 0
      _Imports.razor

+ 34 - 7
MySQLConnector.cs

@@ -7,7 +7,7 @@ namespace HyperCube
 {
     public class MySQLConnector
     {
-        private MySQLConnector(){}
+        private MySQLConnector(){ }
 
         private readonly string Server = "dmatter.net";
         private readonly string DatabaseName = "documents";
@@ -67,13 +67,12 @@ namespace HyperCube
             if (connected)
             {
                 SQLcom = new(sql, Connection);
-
                 MySqlDataReader rdr = SQLcom.ExecuteReader();
 
                 while (rdr.Read())
                 {
-                    Console.WriteLine("{0} {1} {2} {3} {4}", rdr.GetInt32(0), rdr.GetString(1),
-                            rdr.GetString(2), rdr.GetDateTime(3), rdr.GetString(4));
+                    //Console.WriteLine("{0} {1} {2} {3} {4}", rdr.GetInt32(0), rdr.GetString(1),
+                    //        rdr.GetString(2), rdr.GetDateTime(3), rdr.GetString(4));
 
                     articleModel = new();
                     articleModel.Filename = rdr.GetString(1);
@@ -85,9 +84,8 @@ namespace HyperCube
                 }
 
                 await Task.WhenAll();
-
-                Console.WriteLine("End reading DB");
-                
+                rdr.Close();
+                //Console.WriteLine("End reading DB");
                 return articleModels;
             }
             else
@@ -96,6 +94,35 @@ namespace HyperCube
             return null;
         }
 
+        public async Task<Models.ArticleModel> SQLSelectArticle(string sql)
+        {
+            Models.ArticleModel articleModel = new();
+
+            bool connected = IsConnect();
+            if (connected)
+            {
+                SQLcom = new(sql, Connection);
+                MySqlDataReader rdr = SQLcom.ExecuteReader();
+
+                while (rdr.Read())  //id, filename, article_name, authors, date_publish, annotation, keywords
+                {
+                    articleModel.Filename = rdr.GetString(1);
+                    articleModel.Name = rdr.GetString(2);
+                    articleModel.Authors = rdr.GetString(3);
+                    articleModel.PublishDate = rdr.GetDateTime(4);
+                    articleModel.Annotation = rdr.GetString(5);
+                    articleModel.Keywords = rdr.GetString(6);
+                }
+                await Task.WhenAll();
+                rdr.Close();
+                return articleModel;
+            }
+            else
+                Console.WriteLine("Not connected to DB.");
+
+            return null;
+        }
+
         public uint SQLGetID(string sql)
         {
             bool connected = IsConnect();

+ 146 - 0
Pages/DocEdit.razor

@@ -0,0 +1,146 @@
+@page "/docedit"
+@page "/docedit/{docID:int}"
+@using HyperCube.Models;
+@inject NavigationManager NavigationManager;
+@inject IJSRuntime JsRuntime;
+@attribute [Authorize]
+@*@attribute [Authorize(Roles = "admin")]*@
+
+<EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
+    <DataAnnotationsValidator />
+    <ValidationSummary />
+
+    <h1>@header</h1>
+    <br>
+    <div style="width: 50%;">
+        @if (docID < 1) {
+            <p><InputFile id="inputDefault" OnChange="@HandleSelection"
+                           accept="application/pdf" /></p>
+            //accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+        }
+        else {
+            //<a href="" @onclick="@SomeAction" @onclick:preventDefault />
+            <p>Исходный документ: <a href="" @onclick="@DownloadDoc" @onclick:preventDefault>@articleModel.Filename</a></p>
+        }
+        <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
+        <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
+        <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
+        <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
+        <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
+        <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
+        @if (docID < 1) {
+            <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
+        } else {
+            <p><button class="btn btn-danger" type="button" @onclick="@Cancel">Отклонить</button>
+               <button class="btn btn-primary" type="submit">Утвердить</button></p>
+        }
+        <p>Статус: @status</p>
+    </div>
+</EditForm>
+
+@code {
+    [Parameter]
+    public int docID { get; set; }
+
+    const string FOLDER_NAME = "articles_storage";
+    const long MAX_FILE_SIZE = 5120000; //bytes
+
+    ArticleModel articleModel = new ArticleModel();
+    string status;
+    string header;
+    string storageFolderPath;
+    MemoryStream memoryStream;
+
+    protected override async Task OnInitializedAsync()
+    {
+        string path = AppDomain.CurrentDomain.BaseDirectory;
+        storageFolderPath = (Path.Combine(path, FOLDER_NAME));
+
+        if (docID > 0)
+        {
+            header = "Проверка материала";
+
+            MySQLConnector dbCon = MySQLConnector.Instance();
+
+            string stringSQL = $"SELECT id, filename, article_name, authors, date_publish, annotation, keywords FROM articles WHERE id={docID}";
+            articleModel = await dbCon.SQLSelectArticle(stringSQL);
+
+            status = $"Article ID={docID} loaded.";
+        }
+        else
+            header = "Загрузка материала";
+    }
+
+    private async void HandleValidSubmit()
+    {
+        MySQLConnector dbCon = MySQLConnector.Instance();
+
+        string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
+            $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
+            $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
+        long id = dbCon.SQLInsert(stringSQL);
+
+        stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
+        $"VALUES ('{id}', {1}, {1})";
+        dbCon.SQLInsert(stringSQL);
+
+        dbCon.Close();
+
+        //Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
+        string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
+        Directory.CreateDirectory(storageFolderPath);
+        FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
+        await memoryStream.CopyToAsync(fs);
+        memoryStream.Close();
+        fs.Close();
+
+        Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}");
+        status = "New article data saved.";
+
+        bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
+        if (confirmed)
+            NavigationManager.NavigateTo("docedit", true);
+        else
+            NavigationManager.NavigateTo("");
+    }
+
+    private async Task HandleSelection(InputFileChangeEventArgs e)
+    {
+        IBrowserFile file = e.File;
+        if (file != null)
+        {
+            Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
+            memoryStream = new();
+            await stream.CopyToAsync(memoryStream);
+            status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
+
+            DocParse docParse = new DocParse();
+            articleModel = DocParse.ReadPDF(memoryStream);
+            articleModel.Filename = file.Name;
+        }
+    }
+
+    private async void Cancel()
+    {
+        bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
+        if (confirmed)
+        {
+            ///какая-то логика отмены...
+            NavigationManager.NavigateTo("");
+        }
+    }
+
+    private async void DownloadDoc()
+    {
+        string fullpath = Path.Combine(storageFolderPath, $"{docID}_{articleModel.Filename}");
+        if (File.Exists(fullpath))
+        {
+            //await JsRuntime.InvokeAsync<string>("downloadFile", fullpath);
+        }
+        else
+        {
+            Console.WriteLine($"File {fullpath} not found.");
+            status = $"File {fullpath} not found.";
+        }
+    }
+}

+ 0 - 91
Pages/DocsUpload.razor

@@ -1,91 +0,0 @@
-@page "/docsupload"
-@using System.IO;
-@attribute [Authorize]
-@*@attribute [Authorize(Roles = "admin")]*@
-
-<EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
-    <DataAnnotationsValidator />
-    <ValidationSummary />
-
-    <h1>Загрузка материала</h1>
-
-    <div style="width: 50%;">
-        <p><InputFile id="inputDefault" OnChange="@HandleSelection" accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document" /></p>
-        <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
-        <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
-        <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
-        <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
-        <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
-        <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
-        <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
-        <p>Статус: @status</p>
-    </div>
-</EditForm>
-
-@code {
-    @inject NavigationManager NavigationManager;
-    @inject IJSRuntime JsRuntime;
-
-    private const string FOLDER_NAME = "articles_storage";
-    private const long MAX_FILE_SIZE = 5120000; //bytes
-
-    private Models.ArticleModel articleModel = new Models.ArticleModel();
-
-    private string status;
-    MemoryStream memoryStream;
-
-    private async void HandleValidSubmit()
-    {
-        MySQLConnector dbCon = MySQLConnector.Instance();
-
-        string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
-            $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
-            $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
-        long id = dbCon.SQLInsert(stringSQL);
-
-        stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
-        $"VALUES ('{id}', {1}, {1})";
-        dbCon.SQLInsert(stringSQL);
-
-        dbCon.Close();
-
-        string path = System.AppDomain.CurrentDomain.BaseDirectory;
-        Directory.CreateDirectory(Path.Combine(path, FOLDER_NAME));
-        string fullpath = Path.Combine(path, FOLDER_NAME, $"{id}_{articleModel.Filename}");
-
-        Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
-
-        FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
-        //byte[] bytes = new byte[memoryStream.Length];
-        //await memoryStream.ReadAsync(bytes, 0, (int)memoryStream.Length);
-        //await fs.WriteAsync(bytes, 0, bytes.Length);
-        await memoryStream.CopyToAsync(fs);
-        memoryStream.Close();
-        fs.Close();
-
-        Console.WriteLine("Article data saved");
-        status = "Article data saved";
-
-        bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
-        if (confirmed)
-            NavigationManager.NavigateTo("docsupload", true);
-        else
-            NavigationManager.NavigateTo("");
-    }
-
-    async Task HandleSelection(InputFileChangeEventArgs e)
-    {
-        IBrowserFile file = e.File;
-        if (file != null)
-        {
-            Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
-            memoryStream = new();
-            await stream.CopyToAsync(memoryStream);
-            status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
-
-            DocParse docParse = new DocParse();
-            articleModel = DocParse.ReadPDF(memoryStream);
-            articleModel.Filename = file.Name;
-        }
-    }
-}

+ 15 - 12
Pages/Verifying.razor

@@ -7,23 +7,25 @@
 <table class="table">
     <thead>
         <tr>
+            <th>№</th>
             <th>Файл</th>
             <th>Название</th>
             <th>Дата издания</th>
             <th>Авторы</th>
-            <th>Проверка</th>
+            @*<th></th>*@
         </tr>
     </thead>
     <tbody>
         @foreach (var articleModel in articleModels)
         {
-            <tr>
-                <td>@articleModel.Value.Filename</td>
-                <td>@articleModel.Value.Name</td>
-                <td>@articleModel.Value.PublishDate</td>
-                <td>@articleModel.Value.Authors</td>
-                <td><button @onclick="(() => VerifyArticle(articleModel.Key))">Верифицировать</button></td>
-            </tr>
+        <tr>
+            <td>@(counter++)</td>
+            <td>@articleModel.Value.Filename</td>
+            <td><a href="@($"/docedit/{articleModel.Key}")">@articleModel.Value.Name</a></td>
+            <td>@articleModel.Value.PublishDate.ToString("d")</td>
+            <td>@articleModel.Value.Authors</td>
+            @*<td><button @onclick="(() => VerifyArticle(articleModel.Key))">Верифицировать</button></td>*@
+        </tr>
         }
     </tbody>
 </table>
@@ -31,6 +33,7 @@
 
 @code {
     private Dictionary<int, Models.ArticleModel> articleModels;
+    private int counter = 1;
 
     protected override async Task OnInitializedAsync()
     {
@@ -44,8 +47,8 @@
         //status = results;
     }
 
-    private void VerifyArticle(int id)
-    {
-        Console.WriteLine($"Verifyed arcticle ID: {id}!");
-    }
+    //private void VerifyArticle(int id)
+    //{
+    //    Console.WriteLine($"Verifyed arcticle ID: {id}!");
+    //}
 }

+ 1 - 1
Shared/NavMenu.razor

@@ -13,7 +13,7 @@
             </NavLink>
         </li>
         <li class="nav-item px-3">
-            <NavLink class="nav-link" href="docsupload">
+            <NavLink class="nav-link" href="docedit">
                 <span class="oi oi-plus" aria-hidden="true"></span> Загрузка
             </NavLink>
         </li>

+ 1 - 0
_Imports.razor

@@ -1,4 +1,5 @@
 @using System.Net.Http
+@using System.IO;
 @using Microsoft.AspNetCore.Authorization
 @using Microsoft.AspNetCore.Components.Authorization
 @using Microsoft.AspNetCore.Components.Forms