mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 11:14:27 +00:00
Add view for CHS.
This commit is contained in:
@@ -11,6 +11,9 @@
|
||||
<NavLink class="nav-link" href="/admin/block-descriptors">
|
||||
Block descriptors
|
||||
</NavLink>
|
||||
<NavLink class="nav-link" href="/admin/chs">
|
||||
CHS
|
||||
</NavLink>
|
||||
<NavLink class="nav-link" href="/admin/commands">
|
||||
Commands
|
||||
</NavLink>
|
||||
|
||||
82
Aaru.Server/Components/Admin/Pages/Chs/View.razor
Normal file
82
Aaru.Server/Components/Admin/Pages/Chs/View.razor
Normal file
@@ -0,0 +1,82 @@
|
||||
@page "/admin/chs"
|
||||
@attribute [Authorize]
|
||||
@layout AdminLayout
|
||||
|
||||
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
|
||||
|
||||
<PageTitle>Chs</PageTitle>
|
||||
|
||||
@if(!_initialized)
|
||||
{
|
||||
<div class="stats-section">
|
||||
<h1 style="color: red; align-content: center; padding: 2rem">Loading...</h1>
|
||||
</div>
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
<section class="stats-section">
|
||||
<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(Chs), nameof(Chs.Cylinders))
|
||||
</th>
|
||||
<th class="fw-bold bg-secondary text-light">
|
||||
@DisplayNameHelper.GetDisplayName(typeof(Chs), nameof(Chs.Heads))
|
||||
</th>
|
||||
<th class="fw-bold bg-secondary text-light">
|
||||
@DisplayNameHelper.GetDisplayName(typeof(Chs), nameof(Chs.Sectors))
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach(Chs item in _items)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@item.Cylinders
|
||||
</td>
|
||||
<td>
|
||||
@item.Heads
|
||||
</td>
|
||||
<td>
|
||||
@item.Sectors
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@if(_duplicates.Count > 0)
|
||||
{
|
||||
<div>
|
||||
The following CHS have duplicates.
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tbody>
|
||||
@foreach(ChsModel item in _duplicates)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@item.Cylinders/@item.Heads/@item.Sectors
|
||||
</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>
|
||||
106
Aaru.Server/Components/Admin/Pages/Chs/View.razor.cs
Normal file
106
Aaru.Server/Components/Admin/Pages/Chs/View.razor.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using Aaru.CommonTypes.Metadata;
|
||||
using Aaru.Server.Database.Models;
|
||||
using BlazorBootstrap;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
|
||||
namespace Aaru.Server.Components.Admin.Pages.Chs;
|
||||
|
||||
public partial class View
|
||||
{
|
||||
private Modal? _consolidateModal;
|
||||
List<ChsModel> _duplicates;
|
||||
bool _initialized;
|
||||
List<CommonTypes.Metadata.Chs> _items;
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
|
||||
StateHasChanged();
|
||||
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
_items = await ctx.Chs.OrderBy(static c => c.Cylinders)
|
||||
.ThenBy(static c => c.Heads)
|
||||
.ThenBy(static c => c.Sectors)
|
||||
.ToListAsync();
|
||||
|
||||
_duplicates = await ctx.Chs.GroupBy(static x => new
|
||||
{
|
||||
x.Cylinders,
|
||||
x.Heads,
|
||||
x.Sectors
|
||||
})
|
||||
.Where(static x => x.Count() > 1)
|
||||
.Select(static x => new ChsModel
|
||||
{
|
||||
Cylinders = x.Key.Cylinders,
|
||||
Heads = x.Key.Heads,
|
||||
Sectors = x.Key.Sectors
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
_initialized = true;
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
Task ConsolidateDuplicatesAsync() => _consolidateModal?.ShowAsync();
|
||||
|
||||
Task HideConsolidateModalAsync() => _consolidateModal?.HideAsync();
|
||||
|
||||
async Task ConfirmConsolidateAsync()
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
foreach(ChsModel duplicate in _duplicates)
|
||||
{
|
||||
CommonTypes.Metadata.Chs? master = ctx.Chs.FirstOrDefault(m => m.Cylinders == duplicate.Cylinders &&
|
||||
m.Heads == duplicate.Heads &&
|
||||
m.Sectors == duplicate.Sectors);
|
||||
|
||||
if(master is null) continue;
|
||||
|
||||
foreach(CommonTypes.Metadata.Chs chs in await ctx.Chs.Where(m => m.Cylinders == duplicate.Cylinders &&
|
||||
m.Heads == duplicate.Heads &&
|
||||
m.Sectors == duplicate.Sectors)
|
||||
.Skip(1)
|
||||
.ToArrayAsync())
|
||||
{
|
||||
foreach(TestedMedia media in ctx.TestedMedia.Where(d => d.CHS.Id == chs.Id)) media.CHS = master;
|
||||
|
||||
foreach(TestedMedia media in ctx.TestedMedia.Where(d => d.CurrentCHS.Id == chs.Id))
|
||||
media.CurrentCHS = master;
|
||||
|
||||
ctx.Chs.Remove(chs);
|
||||
}
|
||||
}
|
||||
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
_items = await ctx.Chs.OrderBy(static c => c.Cylinders)
|
||||
.ThenBy(static c => c.Heads)
|
||||
.ThenBy(static c => c.Sectors)
|
||||
.ToListAsync();
|
||||
|
||||
_duplicates = await ctx.Chs.GroupBy(static x => new
|
||||
{
|
||||
x.Cylinders,
|
||||
x.Heads,
|
||||
x.Sectors
|
||||
})
|
||||
.Where(static x => x.Count() > 1)
|
||||
.Select(static x => new ChsModel
|
||||
{
|
||||
Cylinders = x.Key.Cylinders,
|
||||
Heads = x.Key.Heads,
|
||||
Sectors = x.Key.Sectors
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user