diff --git a/src/API/Endpoints/VisualisationEndpoints.cs b/src/API/Endpoints/VisualisationEndpoints.cs index fdb3db7..36e956e 100644 --- a/src/API/Endpoints/VisualisationEndpoints.cs +++ b/src/API/Endpoints/VisualisationEndpoints.cs @@ -24,11 +24,40 @@ public static IEndpointRouteBuilder RegisterVisualisationEndpoints(this IEndpoin .ProducesProblem(StatusCodes.Status500InternalServerError) .RequireAuthorization("visualisation-write"); + + group.MapGet("/processedfiles", GetRecentProcessedFiles) + .Produces(); + group.RequireAuthorization("visualisation-read"); return routes; } + private static async Task GetRecentProcessedFiles([FromServices] OfflocContext offlocContext, [FromServices] DeliusContext deliusContext) + { + var offlocFiles = await offlocContext.ProcessedFiles + .OrderByDescending(pf => pf.ValidFrom) + .Take(5) + .Select(c => new ProcessedFileDto() + { + FileName = c.FileName, + Status = c.Status, + ValidFrom = c.ValidFrom + }).ToArrayAsync(); + + var deliusFiles = await deliusContext.ProcessedFiles + .OrderByDescending(pf => pf.ValidFrom) + .Take(5) + .Select(c => new ProcessedFileDto() + { + FileName = c.FileName, + Status = c.Status, + ValidFrom = c.ValidFrom + }).ToArrayAsync(); + + return Results.Ok(offlocFiles.Union(deliusFiles)); + } + public static async Task GetDetailsByUpciAsync([FromServices] ApiServices services, string upci) { var cluster = await services.VisualisationRepository.GetDetailsByUpciAsync(upci); @@ -41,7 +70,7 @@ public static async Task GenerateClusterAsync([FromServices] ApiService { var cluster = await services.ClusteringRepository.GenerateClusterAsync(); - if(cluster is null) + if (cluster is null) { return Results.NotFound(); } @@ -53,7 +82,7 @@ public static async Task SaveNetworkAsync([FromServices] ApiServices se { var success = await services.VisualisationRepository.SaveNetworkAsync(network); - return success is false + return success is false ? Results.InternalServerError() : Results.Ok(); diff --git a/src/Libraries/Infrastructure/DTOs/ProcessedFileDto.cs b/src/Libraries/Infrastructure/DTOs/ProcessedFileDto.cs new file mode 100644 index 0000000..ff75fdf --- /dev/null +++ b/src/Libraries/Infrastructure/DTOs/ProcessedFileDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Infrastructure.DTOs; + +public class ProcessedFileDto +{ + public required string FileName {get;set;} + public DateTime ValidFrom {get;set;} + public required string Status {get;set;} +} \ No newline at end of file diff --git a/src/Libraries/Infrastructure/Entities/Delius/ProcessedFile.cs b/src/Libraries/Infrastructure/Entities/Delius/ProcessedFile.cs index 6cb5b28..c44d419 100644 --- a/src/Libraries/Infrastructure/Entities/Delius/ProcessedFile.cs +++ b/src/Libraries/Infrastructure/Entities/Delius/ProcessedFile.cs @@ -1,8 +1,14 @@ -namespace Infrastructure.Entities.Delius; +using Microsoft.Identity.Client; + +namespace Infrastructure.Entities.Delius; public partial class ProcessedFile { public string FileName { get; set; } = null!; public int FileId { get; set; } + + public string Status {get; set;} = null!; + + public DateTime ValidFrom { get;set; } } diff --git a/src/Libraries/Infrastructure/Entities/Offloc/ProcessedFile.cs b/src/Libraries/Infrastructure/Entities/Offloc/ProcessedFile.cs index bc45b82..17b6659 100644 --- a/src/Libraries/Infrastructure/Entities/Offloc/ProcessedFile.cs +++ b/src/Libraries/Infrastructure/Entities/Offloc/ProcessedFile.cs @@ -5,4 +5,8 @@ public partial class ProcessedFile public string FileName { get; set; } = null!; public int FileId { get; set; } + + public DateTime ValidFrom {get;set;} + + public string Status{ get;set;} = null!; } diff --git a/src/Visualiser/Pages/ProcessedFiles.cshtml b/src/Visualiser/Pages/ProcessedFiles.cshtml new file mode 100644 index 0000000..53049f5 --- /dev/null +++ b/src/Visualiser/Pages/ProcessedFiles.cshtml @@ -0,0 +1,44 @@ +@page "{handler?}" + +@model ProcessedFilesModel + +@{ + ViewData["Title"] = "Processed Files"; +} + +

Most recent processed files

+ + + +@if (!Model.ModelState.IsValid) +{ +
+ @foreach (var error in ViewData.ModelState.Values.SelectMany(v => v.Errors)) + { +
@error.ErrorMessage
+ } +
+} + +@if(Model.ProcessedFiles is { Length: > 0 }) +{ + + + + + + + + + + @foreach(var file in Model.ProcessedFiles) + { + + + + + + } + +
File nameDateStatus
@file.FileName@file.ValidFrom@file.Status
+} \ No newline at end of file diff --git a/src/Visualiser/Pages/ProcessedFiles.cshtml.cs b/src/Visualiser/Pages/ProcessedFiles.cshtml.cs new file mode 100644 index 0000000..da126c3 --- /dev/null +++ b/src/Visualiser/Pages/ProcessedFiles.cshtml.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Identity.Abstractions; +using Microsoft.Identity.Client; + +namespace Visualiser.Pages +{ + public class ProcessedFilesModel(IDownstreamApi api) : PageModel + { + + // Bindable property for the view + public ProcessedFileDto[] ProcessedFiles { get; private set; } = Array.Empty(); + + public async Task OnGetAsync() + { + HttpResponseMessage? response = null; + try + { + response = await api.CallApiForUserAsync("DMS", opts => opts.RelativePath = "Visualisation/processedfiles"); + } + catch (Exception ex) when (ex.InnerException is MsalUiRequiredException { ErrorCode: MsalError.UserNullError }) + { + return Challenge(); + } + + if (response is null) + { + ModelState.AddModelError(string.Empty, "No response"); + return Page(); + } + + if (response.IsSuccessStatusCode) + { + + try + { + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + + var json = await response.Content.ReadAsStringAsync(); + ProcessedFiles = JsonSerializer.Deserialize(json, options) + ?? Array.Empty(); + } + catch(JsonException) + { + ModelState.AddModelError(string.Empty, $"Failed to parse API response"); + } + + } + + return Page(); + } + } + + public class ProcessedFileDto + { + public required string FileName { get; set; } + public DateTime ValidFrom { get; set; } + public required string Status { get; set; } + } +} \ No newline at end of file diff --git a/src/Visualiser/Pages/Shared/_Layout.cshtml b/src/Visualiser/Pages/Shared/_Layout.cshtml index 8f2bce9..317c7a3 100644 --- a/src/Visualiser/Pages/Shared/_Layout.cshtml +++ b/src/Visualiser/Pages/Shared/_Layout.cshtml @@ -36,6 +36,11 @@ Home +