mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add edit functionality for uploaded reports with form validation
This commit is contained in:
55
Aaru.Server/Components/Admin/Pages/Reports/Edit.razor
Normal file
55
Aaru.Server/Components/Admin/Pages/Reports/Edit.razor
Normal file
@@ -0,0 +1,55 @@
|
||||
@page "/admin/reports/edit/{Id:int}"
|
||||
@using Aaru.CommonTypes.Enums
|
||||
@attribute [Authorize]
|
||||
@layout AdminLayout
|
||||
|
||||
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
|
||||
|
||||
<PageTitle>Edit Uploaded Report</PageTitle>
|
||||
|
||||
@if(!isLoaded)
|
||||
{
|
||||
<div>Loading...</div>
|
||||
}
|
||||
else if(report == null)
|
||||
{
|
||||
<div class="alert alert-danger">Uploaded report not found.</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<EditForm Model="report" OnValidSubmit="HandleValidSubmit">
|
||||
<DataAnnotationsValidator />
|
||||
<ValidationSummary />
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Manufacturer</label>
|
||||
<InputText class="form-control" @bind-Value="report.Manufacturer" />
|
||||
<ValidationMessage For="@(() => report.Manufacturer)" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Model</label>
|
||||
<InputText class="form-control" @bind-Value="report.Model" />
|
||||
<ValidationMessage For="@(() => report.Model)" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Revision</label>
|
||||
<InputText class="form-control" @bind-Value="report.Revision" />
|
||||
<ValidationMessage For="@(() => report.Revision)" />
|
||||
</div>
|
||||
<div class="mb-3 form-check">
|
||||
<InputCheckbox class="form-check-input" @bind-Value="report.CompactFlash" />
|
||||
<label class="form-check-label">Compact Flash</label>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Type</label>
|
||||
<InputSelect class="form-control" @bind-Value="report.Type">
|
||||
@foreach(object? type in Enum.GetValues(typeof(DeviceType)))
|
||||
{
|
||||
<option value="@type">@type</option>
|
||||
}
|
||||
</InputSelect>
|
||||
<ValidationMessage For="@(() => report.Type)" />
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<button type="button" class="btn btn-secondary ms-2" @onclick="GoBack">Cancel</button>
|
||||
</EditForm>
|
||||
}
|
||||
76
Aaru.Server/Components/Admin/Pages/Reports/Edit.razor.cs
Normal file
76
Aaru.Server/Components/Admin/Pages/Reports/Edit.razor.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Aaru.CommonTypes.Enums;
|
||||
using Aaru.Server.Database.Models;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
|
||||
namespace Aaru.Server.Components.Admin.Pages.Reports;
|
||||
|
||||
public partial class Edit : ComponentBase
|
||||
{
|
||||
DbContext db;
|
||||
bool isLoaded;
|
||||
|
||||
UploadedReportEditViewModel report;
|
||||
[Parameter]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Inject]
|
||||
public NavigationManager Navigation { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
db = await DbContextFactory.CreateDbContextAsync();
|
||||
UploadedReport? entity = await db.Reports.FindAsync(Id);
|
||||
|
||||
if(entity != null)
|
||||
{
|
||||
report = new UploadedReportEditViewModel
|
||||
{
|
||||
Id = entity.Id,
|
||||
Manufacturer = entity.Manufacturer,
|
||||
Model = entity.Model,
|
||||
Revision = entity.Revision,
|
||||
CompactFlash = entity.CompactFlash,
|
||||
Type = entity.Type
|
||||
};
|
||||
}
|
||||
|
||||
isLoaded = true;
|
||||
}
|
||||
|
||||
protected async Task HandleValidSubmit()
|
||||
{
|
||||
UploadedReport? entity = await db.Reports.FindAsync(Id);
|
||||
|
||||
if(entity != null)
|
||||
{
|
||||
entity.Manufacturer = report.Manufacturer;
|
||||
entity.Model = report.Model;
|
||||
entity.Revision = report.Revision;
|
||||
entity.CompactFlash = report.CompactFlash;
|
||||
entity.Type = report.Type;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
Navigation.NavigateTo("/admin/reports");
|
||||
}
|
||||
|
||||
protected void GoBack()
|
||||
{
|
||||
Navigation.NavigateTo("/admin/reports");
|
||||
}
|
||||
|
||||
public class UploadedReportEditViewModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string Manufacturer { get; set; }
|
||||
[Required]
|
||||
public string Model { get; set; }
|
||||
public string Revision { get; set; }
|
||||
public bool CompactFlash { get; set; }
|
||||
[Required]
|
||||
public DeviceType Type { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user