Add edit view for Compact Disc Offset with form validation

This commit is contained in:
2025-09-12 13:07:29 +01:00
parent 172e61c1fd
commit 462083fef1
2 changed files with 145 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
@page "/admin/cd-offsets/edit/{Id:int}"
@attribute [Authorize]
@layout AdminLayout
@rendermode InteractiveServer
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
<PageTitle>Edit Compact Disc Offset</PageTitle>
@if(_notFound)
{
<div class="alert alert-danger mt-3">Offset not found.</div>
}
else
{
<EditForm Model="_model" OnValidSubmit="HandleValidSubmit">
<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">Offset</label>
<InputNumber class="form-control" @bind-Value="_model.Offset" />
<ValidationMessage For="@(() => _model.Offset)" />
</div>
<div class="mb-3">
<label class="form-label">Submissions</label>
<InputNumber class="form-control" @bind-Value="_model.Submissions" />
<ValidationMessage For="@(() => _model.Submissions)" />
</div>
<div class="mb-3">
<label class="form-label">Agreement</label>
<InputNumber class="form-control" @bind-Value="_model.Agreement" step="0.01" />
<ValidationMessage For="@(() => _model.Agreement)" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
<a href="/admin/cd-offsets" class="btn btn-secondary ms-2">Cancel</a>
</EditForm>
}
@if(_success)
{
<div class="alert alert-success mt-3">Offset saved successfully.</div>
}
@if(_error)
{
<div class="alert alert-danger mt-3">Error saving offset.</div>
}

View File

@@ -0,0 +1,89 @@
using System.ComponentModel.DataAnnotations;
using Aaru.Server.Database.Models;
using Microsoft.AspNetCore.Components;
using DbContext = Aaru.Server.Database.DbContext;
namespace Aaru.Server.Components.Admin.Pages.CompactDiscOffsets;
public partial class Edit : ComponentBase
{
private bool _error;
private OffsetEditModel _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();
CompactDiscOffset? entity = await ctx.CdOffsets.FindAsync(Id);
if(entity != null)
{
_model = new OffsetEditModel
{
Id = entity.Id,
Manufacturer = entity.Manufacturer,
Model = entity.Model,
Offset = entity.Offset,
Submissions = entity.Submissions,
Agreement = entity.Agreement
};
}
else
_notFound = true;
}
private async Task HandleValidSubmit()
{
_success = false;
_error = false;
try
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
CompactDiscOffset? entity = await ctx.CdOffsets.FindAsync(_model.Id);
if(entity == null)
{
_error = true;
return;
}
entity.Manufacturer = _model.Manufacturer;
entity.Model = _model.Model;
entity.Offset = _model.Offset;
entity.Submissions = _model.Submissions;
entity.Agreement = _model.Agreement;
entity.ModifiedWhen = DateTime.UtcNow;
await ctx.SaveChangesAsync();
_success = true;
}
catch
{
_error = true;
}
}
public class OffsetEditModel
{
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]
[Range(-10000, 10000)]
public short Offset { get; set; }
[Required]
[Range(0, int.MaxValue)]
public int Submissions { get; set; }
[Required]
[Range(0, 100)]
public float Agreement { get; set; }
}
}