From 451467855617f8f0e164693c72bf7fcac3459004 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Wed, 8 Feb 2023 13:11:11 -0700 Subject: [PATCH] Test Pinned via API || --- .vscode/tasks.json | 2 +- Server/ApiControllers/PinController.cs | 7 ++-- Server/Controllers/ExportController.cs | 7 +++- Server/Controllers/PagesController.cs | 25 ++++++++++--- Server/Models/AppSettings.cs | 1 + Server/Models/Binder/AppSettings.cs | 4 ++ Server/Program.cs | 2 +- Server/Repositories/PinRepository.cs | 36 +++++++++++++++++- Server/Views/Export/Index.cshtml | 6 ++- Server/Views/Pages/AwaitingDispo.cshtml | 2 +- Server/Views/Pages/RunHeaders.cshtml | 2 +- Server/Views/Pages/RunInfo.cshtml | 11 ++---- Server/appsettings.Development.json | 1 + Server/appsettings.json | 1 + Server/wwwroot/js/site.js | 45 ++++++++++------------- Shared/Models/Stateless/IPinRepository.cs | 5 ++- Tests/UnitTestPinController.cs | 2 +- 17 files changed, 107 insertions(+), 52 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 72ffadb..b530ca9 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -44,7 +44,7 @@ "args": [ "watch", "--launch-profile", - "https", + "http", "run", "--project", "${workspaceFolder}/Server/OI.Metrology.Server.csproj", diff --git a/Server/ApiControllers/PinController.cs b/Server/ApiControllers/PinController.cs index 82b26eb..d7091a5 100644 --- a/Server/ApiControllers/PinController.cs +++ b/Server/ApiControllers/PinController.cs @@ -29,16 +29,17 @@ public class PinController : Controller, IPinController if (was is not null) _Logger.LogDebug($"{toolTypeId} was: {was}"); HttpContext.Session.SetString(toolTypeId, json); + _PinRepository.SetPinnedTable(headerCommon); return Ok(); } [HttpGet] - [Route("{id}/pinned")] - public IActionResult GetPinnedTable(int id) + [Route("{id}/pinned/{rds}")] + public IActionResult GetPinnedTable(int id, string rds = "") { string? cde = HttpContext.Session.GetString(((int)IPinRepository.ToolId.CDE).ToString()); string? bioRad = HttpContext.Session.GetString(((int)IPinRepository.ToolId.BioRad).ToString()); - return Json(_PinRepository.GetPinnedTable(_MetrologyRepository, id, bioRad, cde), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); + return Json(_PinRepository.GetPinnedTable(_MetrologyRepository, id, rds, bioRad, cde), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); } } \ No newline at end of file diff --git a/Server/Controllers/ExportController.cs b/Server/Controllers/ExportController.cs index dc90a3a..b242f8b 100644 --- a/Server/Controllers/ExportController.cs +++ b/Server/Controllers/ExportController.cs @@ -11,6 +11,8 @@ namespace OI.Metrology.Server.Controllers; public class ExportController : Controller { + + private readonly string _ApiUrl; private readonly ILogger _Logger; private readonly bool _IsTestDatabase; private readonly AppSettings _AppSettings; @@ -22,6 +24,7 @@ public class ExportController : Controller _AppSettings = appSettings; _MetrologyRepository = metrologyRepository; _IsTestDatabase = appSettings.ConnectionString.Contains("test", StringComparison.InvariantCultureIgnoreCase); + _ApiUrl = string.IsNullOrEmpty(appSettings.ApiUrl) ? Url.Content("~/") : appSettings.ApiUrl[0] == '~' ? Url.Content(appSettings.ApiUrl) : appSettings.ApiUrl; } public override void OnActionExecuted(ActionExecutedContext context) @@ -29,7 +32,7 @@ public class ExportController : Controller base.OnActionExecuted(context); ViewBag.IsTestDatabase = _IsTestDatabase; } - + [HttpGet] [Route("/Export")] public ActionResult Index() @@ -41,6 +44,7 @@ public class ExportController : Controller }; MonIn monIn = MonIn.GetInstance(); _ = monIn.SendStatus(_AppSettings.MonASite, _AppSettings.MonAResource, "Heartbeat", State.Up); + ViewBag.ApiUrl = _ApiUrl; return View(model); } @@ -81,6 +85,7 @@ public class ExportController : Controller _ = monIn.SendStatus(_AppSettings.MonASite, _AppSettings.MonAResource, "Heartbeat", State.Warning); } } + ViewBag.ApiUrl = _ApiUrl; return View("Index", model); } diff --git a/Server/Controllers/PagesController.cs b/Server/Controllers/PagesController.cs index 481107a..01c1f5f 100644 --- a/Server/Controllers/PagesController.cs +++ b/Server/Controllers/PagesController.cs @@ -8,6 +8,8 @@ namespace OI.Metrology.Server.Controllers; public class PagesController : Controller { + + private readonly string _ApiUrl; private readonly bool _IsTestDatabase; private readonly IMetrologyRepository _MetrologyRepository; @@ -15,6 +17,7 @@ public class PagesController : Controller { _MetrologyRepository = metrologyRepository; _IsTestDatabase = appSettings.ConnectionString.Contains("test", StringComparison.InvariantCultureIgnoreCase); + _ApiUrl = string.IsNullOrEmpty(appSettings.ApiUrl) ? Url.Content("~/") : appSettings.ApiUrl[0] == '~' ? Url.Content(appSettings.ApiUrl) : appSettings.ApiUrl; } public override void OnActionExecuted(ActionExecutedContext context) @@ -25,14 +28,20 @@ public class PagesController : Controller [HttpGet] [Route("/")] - public IActionResult Index() => - View("AwaitingDispo"); + public IActionResult Index() + { + ViewBag.ApiUrl = _ApiUrl; + return View("AwaitingDispo"); + } [HttpGet] [Route("/AwaitingDispo")] [Route("/Metrology/AwaitingDispo")] - public IActionResult AwaitingDispo() => - View(); + public IActionResult AwaitingDispo() + { + ViewBag.ApiUrl = _ApiUrl; + return View(); + } [HttpGet] [Route("/RunInfo")] @@ -49,14 +58,18 @@ public class PagesController : Controller { m.HeaderAttachmentID = _MetrologyRepository.GetHeaderAttachmentID(tooltypeid, headerid); } + ViewBag.ApiUrl = _ApiUrl; return View(m); } [HttpGet] [Route("/RunHeaders")] [Route("/Metrology/RunHeaders")] - public IActionResult RunHeaders() => - View(); + public IActionResult RunHeaders() + { + ViewBag.ApiUrl = _ApiUrl; + return View(); + } [HttpGet] [Route("/Crash")] diff --git a/Server/Models/AppSettings.cs b/Server/Models/AppSettings.cs index cdd0d92..bc7fd0d 100644 --- a/Server/Models/AppSettings.cs +++ b/Server/Models/AppSettings.cs @@ -5,6 +5,7 @@ namespace OI.Metrology.Server.Models; public record AppSettings(string ApiLoggingContentTypes, string ApiLoggingPathPrefixes, string ApiLogPath, + string ApiUrl, string AttachmentPath, string BuildNumber, string Company, diff --git a/Server/Models/Binder/AppSettings.cs b/Server/Models/Binder/AppSettings.cs index e51b5f1..b41e669 100644 --- a/Server/Models/Binder/AppSettings.cs +++ b/Server/Models/Binder/AppSettings.cs @@ -11,6 +11,7 @@ public class AppSettings [Display(Name = "Api Logging Content Types"), Required] public string ApiLoggingContentTypes { get; set; } [Display(Name = "Api Logging Path Prefixes"), Required] public string ApiLoggingPathPrefixes { get; set; } [Display(Name = "Api Log Path"), Required] public string ApiLogPath { get; set; } + [Display(Name = "Api URL"), Required] public string ApiUrl { get; set; } [Display(Name = "Attachment Path"), Required] public string AttachmentPath { get; set; } [Display(Name = "Build Number"), Required] public string BuildNumber { get; set; } [Display(Name = "Company"), Required] public string Company { get; set; } @@ -46,6 +47,8 @@ public class AppSettings throw new NullReferenceException(nameof(ApiLoggingPathPrefixes)); if (appSettings.ApiLogPath is null) throw new NullReferenceException(nameof(ApiLogPath)); + if (appSettings.ApiUrl is null) + throw new NullReferenceException(nameof(ApiUrl)); if (appSettings.AttachmentPath is null) throw new NullReferenceException(nameof(AttachmentPath)); if (appSettings.BuildNumber is null) @@ -80,6 +83,7 @@ public class AppSettings appSettings.ApiLoggingContentTypes, appSettings.ApiLoggingPathPrefixes, appSettings.ApiLogPath, + appSettings.ApiUrl, appSettings.AttachmentPath, appSettings.BuildNumber, appSettings.Company, diff --git a/Server/Program.cs b/Server/Program.cs index 13ff438..9daf05b 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -102,6 +102,7 @@ public class Program if (!webApplicationBuilder.Environment.IsDevelopment()) { _ = webApplication.UseExceptionHandler("/Error"); + _ = webApplication.UseHttpsRedirection(); _ = webApplication.UseHsts(); } else @@ -120,7 +121,6 @@ public class Program _ = webApplication.UseFileServer(enableDirectoryBrowsing: true); _ = webApplication.UseStaticFiles(); _ = webApplication.UseSession(); - _ = webApplication.UseHttpsRedirection(); _ = webApplication.UseMiddleware(); _ = webApplication.MapControllers(); log.Information("Starting Web Application"); diff --git a/Server/Repositories/PinRepository.cs b/Server/Repositories/PinRepository.cs index 8897818..47efd6a 100644 --- a/Server/Repositories/PinRepository.cs +++ b/Server/Repositories/PinRepository.cs @@ -9,20 +9,54 @@ public class PinRepository : IPinRepository private readonly string _MockRoot; private readonly Serilog.ILogger _Log; + private readonly Dictionary> _RdsToHeaderCommonCollection; public PinRepository(string mockRoot) { _MockRoot = mockRoot; + _RdsToHeaderCommonCollection = new(); _Log = Serilog.Log.ForContext(); } - Result IPinRepository.GetPinnedTable(IMetrologyRepository metrologyRepository, int id, string? bioRad, string? cde) + void IPinRepository.SetPinnedTable(HeaderCommon headerCommon) + { + Dictionary? toolIdToHeader; + if (!string.IsNullOrEmpty(headerCommon.RDS)) + { + if (!_RdsToHeaderCommonCollection.TryGetValue(headerCommon.RDS, out toolIdToHeader)) + { + _RdsToHeaderCommonCollection.Add(headerCommon.RDS, new()); + if (!_RdsToHeaderCommonCollection.TryGetValue(headerCommon.RDS, out toolIdToHeader)) + throw new Exception(); + } + if (toolIdToHeader.ContainsKey(headerCommon.ToolTypeID)) + toolIdToHeader[headerCommon.ToolTypeID] = headerCommon; + else + toolIdToHeader.Add(headerCommon.ToolTypeID, headerCommon); + } + } + + Result IPinRepository.GetPinnedTable(IMetrologyRepository metrologyRepository, int id, string? rds, string? bioRad, string? cde) { Result? r; HeaderCommon? cdeHeader = cde is null ? null : JsonSerializer.Deserialize(cde); long cdeId = cdeHeader is null ? (long)IPinRepository.ToolId.CDE : cdeHeader.ToolTypeID; HeaderCommon? bioRadHeader = bioRad is null ? null : JsonSerializer.Deserialize(bioRad); long bioRadId = bioRadHeader is null ? (long)IPinRepository.ToolId.BioRad : bioRadHeader.ToolTypeID; + if (!string.IsNullOrEmpty(rds) && (cdeHeader is null || bioRadHeader is null)) + { + Dictionary? toolIdToHeader; + if (!_RdsToHeaderCommonCollection.TryGetValue(rds, out toolIdToHeader)) + { + _RdsToHeaderCommonCollection.Add(rds, new()); + if (!_RdsToHeaderCommonCollection.TryGetValue(rds, out toolIdToHeader)) + throw new Exception(); + } + if (cdeHeader is null && toolIdToHeader.ContainsKey(cdeId)) + cdeHeader = toolIdToHeader[cdeId]; + if (bioRadHeader is null && toolIdToHeader.ContainsKey(bioRadId)) + bioRadHeader = toolIdToHeader[bioRadId]; + } if (cdeId != id && bioRadId != id) r = new() { Results = Array.Empty(), TotalRows = 0 }; else diff --git a/Server/Views/Export/Index.cshtml b/Server/Views/Export/Index.cshtml index 5e0263b..1dd8b82 100644 --- a/Server/Views/Export/Index.cshtml +++ b/Server/Views/Export/Index.cshtml @@ -47,9 +47,11 @@