Add USB Devices management page with duplicate handling

This commit is contained in:
2025-09-12 12:31:59 +01:00
parent 6c3f1561d9
commit b112c6a5aa
3 changed files with 267 additions and 0 deletions

View File

@@ -59,6 +59,9 @@
<NavLink class="nav-link" href="/admin/supported-densities">
Supported densities
</NavLink>
<NavLink class="nav-link" href="/admin/usb/devices">
USB Devices
</NavLink>
<NavLink class="nav-link" href="/admin/usb/products">
USB Products
</NavLink>

View File

@@ -0,0 +1,124 @@
@page "/admin/usb/devices"
@attribute [Authorize]
@layout AdminLayout
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
<PageTitle>USB Devices</PageTitle>
@if(!_initialized)
{
<div class="stats-section">
<h1 style="color: red; align-content: center; padding: 2rem">Loading...</h1>
</div>
return;
}
<section class="stats-section">
<h4>USB Devices</h4>
<table class="table table-dark table-striped table-bordered mt-4 mb-4">
<thead class="thead-dark">
<tr>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Usb), nameof(Usb.Manufacturer))
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Usb), nameof(Usb.Product))
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Usb), nameof(Usb.VendorID))
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Usb), nameof(Usb.ProductID))
</th>
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Usb), nameof(Usb.RemovableMedia))
</th>
<th class="fw-bold bg-secondary text-light">
Actions
</th>
</tr>
</thead>
<tbody>
@foreach(Usb item in _items)
{
<tr>
<td>
@item.Manufacturer
</td>
<td>
@item.Product
</td>
<td>
@item.VendorID
</td>
<td>
@item.ProductID
</td>
<td>
@item.RemovableMedia
</td>
<td>
<a href="/admin/usb/devices/@item.Id" class="btn btn-primary btn-sm">
<i class="bi bi-eye"></i> Details
</a>
<button class="btn btn-danger btn-sm" @onclick="async () => await ShowDeleteModal(item.Id)">
<i class="bi bi-trash"></i> Delete
</button>
</td>
</tr>
}
</tbody>
</table>
@if(_duplicates.Count > 0)
{
<div>
The following USB devices have duplicates.
<table class="table">
<tbody>
@foreach(UsbModel item in _duplicates)
{
<tr>
<td>
@item.Manufacturer
</td>
<td>
@item.Product
</td>
<td>
@item.VendorID
</td>
<td>
@item.ProductID
</td>
</tr>
}
</tbody>
</table>
</div>
<Button class="btn btn-danger" @onclick="ConsolidateDuplicatesAsync">Consolidate Duplicates</Button>
}
</section>
<BlazorBootstrap.Modal @ref="_consolidateModal" Title="Consolidate duplicates" Size="ModalSize.Small">
<BodyTemplate>
<div class="text-danger">Are you sure you want to delete the duplicates?</div>
</BodyTemplate>
<FooterTemplate>
<button class="btn btn-secondary" @onclick="HideConsolidateModalAsync">Cancel</button>
<button class="btn btn-danger" @onclick="ConfirmConsolidateAsync">Delete</button>
</FooterTemplate>
</BlazorBootstrap.Modal>
<BlazorBootstrap.Modal @ref="_deleteModal" Title="Delete USB device" Size="ModalSize.Small">
<BodyTemplate>
<div class="text-danger">Are you sure you want to delete this USB device?</div>
</BodyTemplate>
<FooterTemplate>
<button class="btn btn-secondary" @onclick="HideDeleteModal">Cancel</button>
<button class="btn btn-danger" @onclick="ConfirmDelete">Delete</button>
</FooterTemplate>
</BlazorBootstrap.Modal>

View File

@@ -0,0 +1,140 @@
using Aaru.Server.Database.Models;
using BlazorBootstrap;
using Microsoft.EntityFrameworkCore;
using DbContext = Aaru.Server.Database.DbContext;
namespace Aaru.Server.Components.Admin.Pages.Usb.Devices;
public partial class List
{
private Modal? _consolidateModal;
private int _deleteId;
private Modal? _deleteModal;
List<UsbModel> _duplicates;
bool _initialized;
List<CommonTypes.Metadata.Usb> _items;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
StateHasChanged();
await RefreshItemsAsync();
_initialized = true;
StateHasChanged();
}
async Task RefreshItemsAsync()
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
_items = await ctx.Usb.OrderBy(static u => u.Manufacturer)
.ThenBy(static u => u.Product)
.ThenBy(static u => u.VendorID)
.ThenBy(static u => u.ProductID)
.ToListAsync();
_duplicates = await ctx.Usb.GroupBy(static x => new
{
x.Manufacturer,
x.Product,
x.VendorID,
x.ProductID
})
.Where(static x => x.Count() > 1)
.Select(static x => new UsbModel
{
Manufacturer = x.Key.Manufacturer,
Product = x.Key.Product,
VendorID = x.Key.VendorID,
ProductID = x.Key.ProductID
})
.ToListAsync();
}
Task ConsolidateDuplicatesAsync() => _consolidateModal?.ShowAsync();
Task HideConsolidateModalAsync() => _consolidateModal?.HideAsync();
async Task ConfirmConsolidateAsync()
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
foreach(UsbModel duplicate in _duplicates)
{
CommonTypes.Metadata.Usb? master = ctx.Usb.FirstOrDefault(m => m.Manufacturer == duplicate.Manufacturer &&
m.Product == duplicate.Product &&
m.VendorID == duplicate.VendorID &&
m.ProductID == duplicate.ProductID);
if(master is null) continue;
foreach(CommonTypes.Metadata.Usb slave in await ctx.Usb
.Where(m => m.Manufacturer == duplicate.Manufacturer &&
m.Product == duplicate.Product &&
m.VendorID == duplicate.VendorID &&
m.ProductID == duplicate.ProductID)
.Skip(1)
.ToArrayAsync())
{
if(slave.Descriptors != null && master.Descriptors != null)
{
if(!master.Descriptors.SequenceEqual(slave.Descriptors)) continue;
}
foreach(Device device in ctx.Devices.Where(d => d.USB.Id == slave.Id)) device.USB = master;
foreach(UploadedReport report in ctx.Reports.Where(d => d.USB.Id == slave.Id)) report.USB = master;
if(master.Descriptors is null && slave.Descriptors != null)
{
master.Descriptors = slave.Descriptors;
ctx.Usb.Update(master);
}
ctx.Usb.Remove(slave);
}
}
await ctx.SaveChangesAsync();
await RefreshItemsAsync();
StateHasChanged();
}
private async Task ShowDeleteModal(int id)
{
_deleteId = id;
if(_deleteModal != null) await _deleteModal.ShowAsync();
}
private async Task HideDeleteModal()
{
if(_deleteModal != null) await _deleteModal.HideAsync();
}
private async Task ConfirmDelete()
{
await DeleteAsync(_deleteId);
await HideDeleteModal();
await RefreshItemsAsync();
}
private async Task DeleteAsync(int id)
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
CommonTypes.Metadata.Usb? usb = await ctx.Usb.FindAsync(id);
if(usb is not null)
{
ctx.Usb.Remove(usb);
await ctx.SaveChangesAsync();
}
}
}