From 89e215aec3a34e2542b091d5c770198fb7c57486 Mon Sep 17 00:00:00 2001 From: "Georgy.Khatuncev" Date: Fri, 20 Sep 2024 10:54:32 +0500 Subject: [PATCH] Migrape API to NET8 Use entity schema from root, add controller, add CORS for proxy --- APICycleVDP/APICycleVDP.csproj | 12 +++- APICycleVDP/APICycleVDP.http | 6 -- APICycleVDP/Controller/CyclesController.cs | 71 ++++++++++++++++++++++ APICycleVDP/Program.cs | 37 +++++------ APICycleVDP/Properties/launchSettings.json | 6 +- APICycleVDP/appsettings.json | 7 +++ 6 files changed, 108 insertions(+), 31 deletions(-) delete mode 100644 APICycleVDP/APICycleVDP.http create mode 100644 APICycleVDP/Controller/CyclesController.cs diff --git a/APICycleVDP/APICycleVDP.csproj b/APICycleVDP/APICycleVDP.csproj index 1b28a01..64687dd 100644 --- a/APICycleVDP/APICycleVDP.csproj +++ b/APICycleVDP/APICycleVDP.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -6,4 +6,14 @@ enable + + + + + + + + + + diff --git a/APICycleVDP/APICycleVDP.http b/APICycleVDP/APICycleVDP.http deleted file mode 100644 index baa7d8c..0000000 --- a/APICycleVDP/APICycleVDP.http +++ /dev/null @@ -1,6 +0,0 @@ -@APICycleVDP_HostAddress = http://localhost:5127 - -GET {{APICycleVDP_HostAddress}}/weatherforecast/ -Accept: application/json - -### diff --git a/APICycleVDP/Controller/CyclesController.cs b/APICycleVDP/Controller/CyclesController.cs new file mode 100644 index 0000000..cdc7db2 --- /dev/null +++ b/APICycleVDP/Controller/CyclesController.cs @@ -0,0 +1,71 @@ +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using DbCycleVDP; + +namespace APICycleVDP.Controller +{ + [EnableCors("All")] + public class CyclesController : ControllerBase + { + [HttpPost] + public ActionResult CurrCycles() + { + var result = new JObject + { + ["currTime"] = DateTime.Now + }; + + var tasks = new List>(); + //var v = new DB.WorkDB(); + for (var i = 1; i <= 48; i++) + tasks.Add(WorkDB.GetCycle(i)); + + var arr = new JArray(); + for (var i = 0; i < tasks.Count; i++) + { + var tmp = tasks[i].GetAwaiter().GetResult(); + if (tmp != null) + { + var t = new JObject(); + t["vdp"] = tmp.NumVdp; + t["cycle"] = tmp.NumCycle; + t["factStart"] = tmp.FactStart; + t["factEnd"] = tmp.FactEnd; + t["thinkEnd"] = tmp.ThinkEnd; + arr.Add(t); + } + } + result["data"] = arr; + + return Content(result.ToString(), "application/json"); + } + } + + + public static class WorkDB + { + async public static Task GetCycle(int vdp) + { + var result = new TableCycle(); + await Task.Run(() => + { + try + { + using var db = new DbFurnace(); + var tmp = (from u in db.Cycles + where + u.NumVdp == vdp + orderby u.FactStart descending + select u).FirstOrDefault(); + result = tmp; + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + }); + return result; + } + } +} diff --git a/APICycleVDP/Program.cs b/APICycleVDP/Program.cs index c79173c..5758fa3 100644 --- a/APICycleVDP/Program.cs +++ b/APICycleVDP/Program.cs @@ -1,27 +1,22 @@ var builder = WebApplication.CreateBuilder(args); -var app = builder.Build(); -var summaries = new[] +builder.Services.AddControllers(); +builder.Services.AddCors(options => { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; - -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; + options.AddPolicy("AllowAllOrigins", + builder => + { + builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + }); }); -app.Run(); +var app = builder.Build(); -internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} +app.UseCors("AllowAllOrigins"); + +app.MapControllerRoute(name: "CurrCycles", pattern: "/currcycles", + defaults: new { controller = "Cycles", action = "CurrCycles" }); + +app.Run(); \ No newline at end of file diff --git a/APICycleVDP/Properties/launchSettings.json b/APICycleVDP/Properties/launchSettings.json index 8ae95ee..b9148c3 100644 --- a/APICycleVDP/Properties/launchSettings.json +++ b/APICycleVDP/Properties/launchSettings.json @@ -13,8 +13,8 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "launchUrl": "weatherforecast", - "applicationUrl": "http://localhost:5127", + "launchUrl": "currcycles", + "applicationUrl": "http://localhost:65041", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -22,7 +22,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "currcycles", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/APICycleVDP/appsettings.json b/APICycleVDP/appsettings.json index 10f68b8..0727d64 100644 --- a/APICycleVDP/appsettings.json +++ b/APICycleVDP/appsettings.json @@ -1,4 +1,11 @@ { + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://*:65041" + } + } + }, "Logging": { "LogLevel": { "Default": "Information",