mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 11:14:27 +00:00
Add edit view for tested sequential media with form validation
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
@page "/admin/tested-media/sequential/edit/{Id:int}"
|
||||
@attribute [Authorize]
|
||||
@layout AdminLayout
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
|
||||
|
||||
<PageTitle>Edit Tested Sequential Media</PageTitle>
|
||||
|
||||
@if(_notFound)
|
||||
{
|
||||
<div class="alert alert-danger mt-3">Tested sequential media not found.</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<EditForm Model="_model" OnValidSubmit="HandleValidSubmitAsync">
|
||||
<DataAnnotationsValidator />
|
||||
<ValidationSummary />
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Manufacturer</label>
|
||||
<InputText class="form-control" @bind-Value="_model.Manufacturer" />
|
||||
<ValidationMessage For="@(() => _model.Manufacturer)" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Model</label>
|
||||
<InputText class="form-control" @bind-Value="_model.Model" />
|
||||
<ValidationMessage For="@(() => _model.Model)" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Medium Type Name</label>
|
||||
<InputText class="form-control" @bind-Value="_model.MediumTypeName" />
|
||||
<ValidationMessage For="@(() => _model.MediumTypeName)" />
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<a href="/admin/tested-media/sequential" class="btn btn-secondary ms-2">Cancel</a>
|
||||
</EditForm>
|
||||
}
|
||||
|
||||
@if(_success)
|
||||
{
|
||||
<div class="alert alert-success mt-3">Tested sequential media saved successfully.</div>
|
||||
}
|
||||
@if(_error)
|
||||
{
|
||||
<div class="alert alert-danger mt-3">Error saving tested sequential media.</div>
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Aaru.CommonTypes.Metadata;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
|
||||
namespace Aaru.Server.Components.Admin.Pages.TestedMedia.Sequential;
|
||||
|
||||
public partial class Edit : ComponentBase
|
||||
{
|
||||
private bool _error;
|
||||
private EditModel _model = new();
|
||||
private bool _notFound;
|
||||
private bool _success;
|
||||
[Parameter]
|
||||
public int Id { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
TestedSequentialMedia? entity = await ctx.TestedSequentialMedia.FindAsync(Id);
|
||||
|
||||
if(entity != null)
|
||||
{
|
||||
_model = new EditModel
|
||||
{
|
||||
Id = entity.Id,
|
||||
Manufacturer = entity.Manufacturer,
|
||||
Model = entity.Model,
|
||||
MediumTypeName = entity.MediumTypeName
|
||||
};
|
||||
}
|
||||
else
|
||||
_notFound = true;
|
||||
}
|
||||
|
||||
private async Task HandleValidSubmitAsync()
|
||||
{
|
||||
_success = false;
|
||||
_error = false;
|
||||
|
||||
try
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
TestedSequentialMedia? entity = await ctx.TestedSequentialMedia.FindAsync(_model.Id);
|
||||
|
||||
if(entity == null)
|
||||
{
|
||||
_error = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
entity.Manufacturer = _model.Manufacturer;
|
||||
entity.Model = _model.Model;
|
||||
entity.MediumTypeName = _model.MediumTypeName;
|
||||
await ctx.SaveChangesAsync();
|
||||
_success = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
public class EditModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public string Manufacturer { get; set; } = string.Empty;
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public string Model { get; set; } = string.Empty;
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public string MediumTypeName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user