Rimmon 3 роки тому
батько
коміт
85555ad150

+ 52 - 0
Controllers/ValuesController.cs

@@ -0,0 +1,52 @@
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using HyperCube.Models;
+
+// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
+
+namespace HyperCube.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    public class ValuesController : ControllerBase
+    {
+        // GET: api/<ValuesController>
+        [HttpGet]
+        public IEnumerable<string> Get()
+        {
+            return new string[] {};
+        }
+
+        // GET api/<ValuesController>/5
+        [HttpGet("{id}")]
+        public async Task<string> Get(int id)
+        {
+            string transactionId = "not found";
+            var article = ArticleModel.Find(id);
+            if (article != null)
+                transactionId = await SmartContract.Verify(article);
+            return transactionId;
+        }
+
+        // POST api/<ValuesController>
+        [HttpPost]
+        public void Post([FromBody] string value)
+        {
+        }
+
+        // PUT api/<ValuesController>/5
+        [HttpPut("{id}")]
+        public void Put(int id, [FromBody] string value)
+        {
+        }
+
+        // DELETE api/<ValuesController>/5
+        [HttpDelete("{id}")]
+        public void Delete(int id)
+        {
+        }
+    }
+}

+ 2 - 0
HyperCube.csproj.user

@@ -5,6 +5,8 @@
     <ActiveDebugProfile>HyperCube</ActiveDebugProfile>
     <RazorPage_SelectedScaffolderID>RazorPageScaffolder</RazorPage_SelectedScaffolderID>
     <RazorPage_SelectedScaffolderCategoryPath>root/Common/RazorPage</RazorPage_SelectedScaffolderCategoryPath>
+    <Controller_SelectedScaffolderID>ApiControllerWithActionsScaffolder</Controller_SelectedScaffolderID>
+    <Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
     <DebuggerFlavor>ProjectDebugger</DebuggerFlavor>

+ 27 - 1
Models/ArticleModel.cs

@@ -1,11 +1,12 @@
 using System;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel.DataAnnotations;
 using System.Threading.Tasks;
 using Console = HyperCube.Utils.AdvConsole;
 
 namespace HyperCube.Models
-{
+{    
     public enum ArticleStatus {
         [Display(Name = "Новая")]
         New = 0,
@@ -22,6 +23,21 @@ namespace HyperCube.Models
 
     public class ArticleModel : ICloneable
     {
+        public static Dictionary<int, ArticleModel> articleModels;
+        public static async Task LoadArticles()
+        {
+            MySQLConnector dbCon = MySQLConnector.Instance();
+
+            string stringSQL = $"SELECT a.id, filename, article_name, date_publish, authors, ah.action_type " +
+                    $"FROM articles a " +
+                    $"LEFT JOIN actions_history ah ON a.id = ah.article_id " +
+                    $"AND EXISTS (SELECT 1 FROM actions_history ah1 WHERE ah.article_id = ah1.article_id HAVING MAX(ah1.date_add) = ah.date_add) " +
+                    $"ORDER BY a.id";
+            articleModels = await dbCon.SQLSelectArticles(stringSQL);
+            //dbCon.Close();
+        }
+
+
         public int ID { get; set; }
         public string Filename { get; set; }
         [Required]        
@@ -52,6 +68,16 @@ namespace HyperCube.Models
         private int? rating;
         private string initiatorUUID = "";
 
+        public static ArticleModel Find(int id)
+        {
+            if (id > 0) {
+
+                if (articleModels.ContainsKey(id))
+                    return articleModels[id];
+            }
+            return null;
+        }
+
         public async Task<int> GetEditsCount(string acc_id = "")
         {
             MySQLConnector dbCon = MySQLConnector.Instance();

+ 24 - 0
Models/SmartContract.cs

@@ -33,6 +33,30 @@ namespace HyperCube.Models
             return null;
         }
 
+        public static async Task<string> Verify(ArticleModel articleModel)
+        {
+            if (articleModel == null)
+                return "Verify failed";
+            Console.WriteLine($"Verify starting");
+            try
+            {
+                VerifyContract verifyContract = Find("Verify", Blockchain.GetMain()) as VerifyContract;
+                if (verifyContract != null)
+                {
+                    Console.WriteLine($"VerifyContract found");
+                    return await verifyContract.Run(articleModel);
+                }
+                else
+                    Console.WriteLine($"VerifyContract null");
+
+            }
+            catch (Exception e)
+            {
+                Console.WriteLine(e.Message + "stack trace" + e.StackTrace);
+            }
+            return "Verify failed";
+        }
+
         public SmartContract()
         { }
 

+ 4 - 13
Pages/Verifying.razor

@@ -3,6 +3,7 @@
 @using System.ComponentModel.DataAnnotations;
 @using System.Linq;
 @using System.Reflection;
+@using HyperCube.Models;
 
 @attribute [Authorize]
 
@@ -30,7 +31,7 @@
             </tr>
         </thead>
         <tbody>
-            @foreach (var articleModel in articleModels)
+            @foreach (var articleModel in ArticleModel.articleModels)
             {
                 <tr>
                     <td>@(counter++)</td>
@@ -49,23 +50,13 @@
 
 
 @code {
-    private Dictionary<int, Models.ArticleModel> articleModels;
+
     private int counter = 1;
 
     protected override async Task OnInitializedAsync()
     {
-        MySQLConnector dbCon = MySQLConnector.Instance();
-
-        string stringSQL = $"SELECT a.id, filename, article_name, date_publish, authors, ah.action_type " +
-                $"FROM articles a " +
-                $"LEFT JOIN actions_history ah ON a.id = ah.article_id " +
-                $"AND EXISTS (SELECT 1 FROM actions_history ah1 WHERE ah.article_id = ah1.article_id HAVING MAX(ah1.date_add) = ah.date_add) " +
-                $"ORDER BY a.id";
-        articleModels = await dbCon.SQLSelectArticles(stringSQL);
-
-        dbCon.Close();
 
-        //status = results;
+        
     }
 
     private static string GetDisplayName(Enum enumValue)

+ 3 - 1
Startup.cs

@@ -24,6 +24,7 @@ namespace HyperCube
         {
             Configuration = configuration;
             Models.AccountModel.InitializeAccounts();
+            Models.ArticleModel.LoadArticles();
         }
 
         public IConfiguration Configuration { get; }
@@ -42,6 +43,7 @@ namespace HyperCube
             services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
             services.AddDatabaseDeveloperPageExceptionFilter();
             services.AddSingleton<WeatherForecastService>();
+            services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -67,7 +69,7 @@ namespace HyperCube
             //});
 
             app.UseRouting();
-
+            app.UseMvcWithDefaultRoute();
             app.UseAuthentication();
             app.UseAuthorization();