Add instruction set extension editing in admin view.

This commit is contained in:
2020-05-27 18:33:22 +01:00
parent b51119648f
commit 766f20c9fc
7 changed files with 110 additions and 75 deletions

View File

@@ -1,65 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Edit.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view edit
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.InstructionSetExtension
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>Instruction set extension</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Extension" class="control-label">
</label>
<input asp-for="Extension" class="form-control" />
<span asp-validation-for="Extension" class="text-danger">
</span>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Save" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1227</Version>
<Version>3.0.99.1228</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>

View File

@@ -31,9 +31,11 @@
}
@page "/admin/instruction_set_extensions/details/{Id:int}"
@page "/admin/instruction_set_extensions/edit/{Id:int}"
@inherits OwningComponentBase<InstructionSetExtensionsService>
@inject IStringLocalizer<InstructionSetExtensionsService> L
@attribute [Authorize(Roles = "UberAdmin, Admin")]
@inject NavigationManager NavigationManager
<h3>@L["Instruction set extension details"]</h3>
<hr />
@@ -47,10 +49,24 @@
<div>
<Field>
<FieldLabel>@L["Extension"]</FieldLabel>
<TextEdit ReadOnly="!_editable" @bind-Text="@_model.Extension"/>
<Validation Validator="@ValidateName">
<TextEdit Disabled="!_editing" @bind-Text="@_model.Extension">
<Feedback>
<ValidationError>@L["Please enter a valid extension name."]</ValidationError>
</Feedback>
</TextEdit>
</Validation>
</Field>
</div>
<div>
<span class="btn btn-primary">@L["Edit"]</span>
<a href="/admin/companies" class="btn btn-secondary">@L["Back to list"]</a>
@if (!_editing)
{
<Button Color="Color.Primary" Clicked="@OnEditClicked">@L["Edit"]</Button>
}
else
{
<Button Color="Color.Success" Clicked="@OnSaveClicked">@L["Save"]</Button>
<Button Color="Color.Danger" Clicked="@OnCancelClicked">@L["Cancel"]</Button>
}
<a href="/admin/instruction_set_extensions" class="btn btn-secondary">@L["Back to list"]</a>
</div>

View File

@@ -1,11 +1,14 @@
using System;
using System.Threading.Tasks;
using Blazorise;
using Marechai.Shared;
using Microsoft.AspNetCore.Components;
namespace Marechai.Pages.Admin.Details
{
public partial class InstructionSetExtension
{
bool _editable;
bool _editing;
bool _loaded;
Database.Models.InstructionSetExtension _model;
[Parameter]
@@ -23,7 +26,51 @@ namespace Marechai.Pages.Admin.Details
_model = await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/instruction_set_extensions/edit/",
StringComparison.InvariantCulture);
StateHasChanged();
}
void OnEditClicked()
{
_editing = true;
StateHasChanged();
}
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
StateHasChanged();
}
async void OnSaveClicked()
{
if(string.IsNullOrWhiteSpace(_model.Extension) ||
_model.Extension.Length > 45 ||
!Service.VerifyUnique(_model.Extension))
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
StateHasChanged();
}
void ValidateName(ValidatorEventArgs e)
{
Validators.ValidateString(e, L["Extension name cannot contain more than 45 characters."], 45);
if(e.Status != ValidationStatus.Success)
return;
if(Service.VerifyUnique(_model.Extension))
return;
e.Status = ValidationStatus.Error;
e.ErrorText = L["Extension name must be unique."];
}
}
}

View File

@@ -64,9 +64,7 @@
</td>
<td>
<a class="btn btn-primary" href="/admin/instruction_set_extensions/details/@item.Id">@L["Details"]</a>
<span class="btn btn-secondary">
@L["Edit"]
</span>
<a class="btn btn-primary" href="/admin/instruction_set_extensions/edit/@item.Id">@L["Edit"]</a>
<Button Color="Color.Danger" Clicked="() => {ShowModal(item.Id);}">@L["Delete"]</Button>
</td>
</tr>

View File

@@ -166,4 +166,20 @@
<value>Volver a la lista</value>
<comment>Back to list</comment>
</data>
<data name="Save" xml:space="preserve">
<value>Guardar</value>
<comment>Save</comment>
</data>
<data name="Please enter a valid extension name." xml:space="preserve">
<value>Por favor introduce un nombre válido para la extensión.</value>
<comment>Please enter a valid extension name.</comment>
</data>
<data name="Extension name cannot contain more than 45 characters." xml:space="preserve">
<value>El nombre de la extensión no puede contener más de 45 caracteres.</value>
<comment>Extension name cannot contain more than 45 characters.</comment>
</data>
<data name="Extension name must be unique." xml:space="preserve">
<value>El nombre de la extensión debe ser único.</value>
<comment>Extension name must be unique.</comment>
</data>
</root>

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -13,10 +14,28 @@ namespace Marechai.Services
public InstructionSetExtensionsService(MarechaiContext context) => _context = context;
public async Task<List<InstructionSetExtension>> GetAsync() =>
await _context.InstructionSetExtensions.OrderBy(e => e.Extension).ToListAsync();
await _context.InstructionSetExtensions.OrderBy(e => e.Extension).Select(e => new InstructionSetExtension
{
Extension = e.Extension, Id = e.Id
}).ToListAsync();
public async Task<InstructionSetExtension> GetAsync(int id) =>
await _context.InstructionSetExtensions.FindAsync(id);
await _context.InstructionSetExtensions.Where(e => e.Id == id).Select(e => new InstructionSetExtension
{
Extension = e.Extension, Id = e.Id
}).FirstOrDefaultAsync();
public async Task UpdateAsync(InstructionSetExtension viewModel)
{
InstructionSetExtension model = await _context.InstructionSetExtensions.FindAsync(viewModel.Id);
if(model is null)
return;
model.Extension = viewModel.Extension;
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
@@ -29,5 +48,9 @@ namespace Marechai.Services
await _context.SaveChangesAsync();
}
public bool VerifyUnique(string extension) =>
!_context.InstructionSetExtensions.Any(i => string.Equals(i.Extension, extension,
StringComparison.InvariantCultureIgnoreCase));
}
}