mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add edit view for Compact Disc Offset with form validation
This commit is contained in:
@@ -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>
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user