Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/API/Endpoints/VisualisationEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,40 @@ public static IEndpointRouteBuilder RegisterVisualisationEndpoints(this IEndpoin
.ProducesProblem(StatusCodes.Status500InternalServerError)
.RequireAuthorization("visualisation-write");


group.MapGet("/processedfiles", GetRecentProcessedFiles)
.Produces<ProcessedFileDto[]>();

group.RequireAuthorization("visualisation-read");

return routes;
}

private static async Task<IResult> 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<IResult> GetDetailsByUpciAsync([FromServices] ApiServices services, string upci)
{
var cluster = await services.VisualisationRepository.GetDetailsByUpciAsync(upci);
Expand All @@ -41,7 +70,7 @@ public static async Task<IResult> GenerateClusterAsync([FromServices] ApiService
{
var cluster = await services.ClusteringRepository.GenerateClusterAsync();

if(cluster is null)
if (cluster is null)
{
return Results.NotFound();
}
Expand All @@ -53,7 +82,7 @@ public static async Task<IResult> SaveNetworkAsync([FromServices] ApiServices se
{
var success = await services.VisualisationRepository.SaveNetworkAsync(network);

return success is false
return success is false
? Results.InternalServerError()
: Results.Ok();

Expand Down
13 changes: 13 additions & 0 deletions src/Libraries/Infrastructure/DTOs/ProcessedFileDto.cs
Original file line number Diff line number Diff line change
@@ -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;}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
}
44 changes: 44 additions & 0 deletions src/Visualiser/Pages/ProcessedFiles.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@page "{handler?}"

@model ProcessedFilesModel

@{
ViewData["Title"] = "Processed Files";
}

<h1>Most recent processed files</h1>



@if (!Model.ModelState.IsValid)
{
<div class="alert alert-danger">
@foreach (var error in ViewData.ModelState.Values.SelectMany(v => v.Errors))
{
<div>@error.ErrorMessage</div>
}
</div>
}

@if(Model.ProcessedFiles is { Length: > 0 })
{
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>File name</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@foreach(var file in Model.ProcessedFiles)
{
<tr>
<td>@file.FileName</td>
<td>@file.ValidFrom</td>
<td>@file.Status</td>
</tr>
}
</tbody>
</table>
}
69 changes: 69 additions & 0 deletions src/Visualiser/Pages/ProcessedFiles.cshtml.cs
Original file line number Diff line number Diff line change
@@ -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<ProcessedFileDto>();

public async Task<IActionResult> 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<ProcessedFileDto[]>(json, options)
?? Array.Empty<ProcessedFileDto>();
}
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; }
}
}
5 changes: 5 additions & 0 deletions src/Visualiser/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
</ul>
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/ProcessedFiles">Files</a>
</li>
</ul>
<partial name="_LoginPartial" />
</div>
</div>
Expand Down
Loading