Add machine family editing in admin view.

This commit is contained in:
2020-05-27 14:36:49 +01:00
parent bae525e37e
commit 86cfb40ed8
7 changed files with 99 additions and 86 deletions

View File

@@ -1,73 +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.MachineFamily
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>Machine family</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="CompanyId" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
<span asp-validation-for="CompanyId" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label">
</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" 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

@@ -31,9 +31,11 @@
} }
@page "/admin/machine_families/details/{Id:int}" @page "/admin/machine_families/details/{Id:int}"
@page "/admin/machine_families/edit/{Id:int}"
@inherits OwningComponentBase<MachineFamiliesService> @inherits OwningComponentBase<MachineFamiliesService>
@inject IStringLocalizer<MachineFamiliesService> L @inject IStringLocalizer<MachineFamiliesService> L
@inject CompaniesService CompaniesService @inject CompaniesService CompaniesService
@inject NavigationManager NavigationManager
@attribute [Authorize(Roles = "UberAdmin, Admin")] @attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Machine family details"]</h3> <h3>@L["Machine family details"]</h3>
<hr /> <hr />
@@ -48,7 +50,7 @@
<div> <div>
<Field> <Field>
<FieldLabel>@L["Company"]</FieldLabel> <FieldLabel>@L["Company"]</FieldLabel>
<Select Disabled="!_editable" TValue="int" @bind-SelectedValue="@_model.CompanyId"> <Select Disabled="!_editing" TValue="int" @bind-SelectedValue="@_model.CompanyId">
@foreach (var company in _companies) @foreach (var company in _companies)
{ {
<SelectItem TValue="int" Value="@company.Id">@company.Name</SelectItem> <SelectItem TValue="int" Value="@company.Id">@company.Name</SelectItem>
@@ -57,10 +59,28 @@
</Field> </Field>
<Field> <Field>
<FieldLabel>@L["Name"]</FieldLabel> <FieldLabel>@L["Name"]</FieldLabel>
<TextEdit ReadOnly="!_editable" @bind-Text="@_model.Name"/> <Validation Validator="@ValidateName">
<TextEdit Disabled="!_editing" @bind-Text="@_model.Name">
<Feedback>
<ValidationError>@L["Please enter a valid name."]</ValidationError>
</Feedback>
</TextEdit>
</Validation>
@if (_editing)
{
<FieldHelp>@L["Must not contain \"family\" or \"series\". Different regional names should be separated with /. Different numbers should be separated with comma."]</FieldHelp>
}
</Field> </Field>
</div> </div>
<div> <div>
<span class="btn btn-primary">@L["Edit"]</span> @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/machine_families" class="btn btn-secondary">@L["Back to list"]</a> <a href="/admin/machine_families" class="btn btn-secondary">@L["Back to list"]</a>
</div> </div>

View File

@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Blazorise;
using Marechai.Shared;
using Marechai.ViewModels; using Marechai.ViewModels;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@@ -8,9 +11,9 @@ namespace Marechai.Pages.Admin.Details
public partial class MachineFamily public partial class MachineFamily
{ {
List<CompanyViewModel> _companies; List<CompanyViewModel> _companies;
bool _editable; bool _editing;
bool _loaded; bool _loaded;
Database.Models.MachineFamily _model; MachineFamilyViewModel _model;
[Parameter] [Parameter]
public int Id { get; set; } public int Id { get; set; }
@@ -27,7 +30,38 @@ namespace Marechai.Pages.Admin.Details
_companies = await CompaniesService.GetAsync(); _companies = await CompaniesService.GetAsync();
_model = await Service.GetAsync(Id); _model = await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machine_families/edit/", StringComparison.InvariantCulture);
StateHasChanged(); StateHasChanged();
} }
void OnEditClicked()
{
_editing = true;
StateHasChanged();
}
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
StateHasChanged();
}
async void OnSaveClicked()
{
if(string.IsNullOrWhiteSpace(_model.Name) ||
_model.Name?.Length > 255)
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
StateHasChanged();
}
void ValidateName(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Family name must contain less than 255 characters."], 255);
} }
} }

View File

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

View File

@@ -170,4 +170,20 @@
<value>Volver a la lista</value> <value>Volver a la lista</value>
<comment>Back to list</comment> <comment>Back to list</comment>
</data> </data>
<data name="Save" xml:space="preserve">
<value>Guardar</value>
<comment>Save</comment>
</data>
<data name="Please enter a valid name." xml:space="preserve">
<value>Por favor introduce un nombre válido.</value>
<comment>Please enter a valid name.</comment>
</data>
<data name="Must not contain &quot;family&quot; or &quot;series&quot;. Different regional names should be separated with /. Different numbers should be separated with comma." xml:space="preserve">
<value>No debe contener &quot;familia&quot; or &quot;serie/quot;. Diferentes nombres regionales deben separarse con /. Diferentes números deben separarse con coma.</value>
<comment>Must not contain &quot;family&quot; or &quot;series&quot;. Different regional names should be separated with /. Different numbers should be separated with comma.</comment>
</data>
<data name="Family name must contain less than 255 characters." xml:space="preserve">
<value>El nombre de la familia debe contener menos de 255 caracteres.</value>
<comment>Family name must contain less than 255 characters.</comment>
</data>
</root> </root>

View File

@@ -20,7 +20,24 @@ namespace Marechai.Services
Id = m.Id, Company = m.Company.Name, Name = m.Name Id = m.Id, Company = m.Company.Name, Name = m.Name
}).ToListAsync(); }).ToListAsync();
public async Task<MachineFamily> GetAsync(int id) => await _context.MachineFamilies.FindAsync(id); public async Task<MachineFamilyViewModel> GetAsync(int id) =>
await _context.MachineFamilies.Where(f => f.Id == id).Select(m => new MachineFamilyViewModel
{
Id = m.Id, CompanyId = m.CompanyId, Name = m.Name
}).FirstOrDefaultAsync();
public async Task UpdateAsync(MachineFamilyViewModel viewModel)
{
MachineFamily model = await _context.MachineFamilies.FindAsync(viewModel.Id);
if(model is null)
return;
model.Name = viewModel.Name;
model.CompanyId = viewModel.CompanyId;
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id) public async Task DeleteAsync(int id)
{ {

View File

@@ -2,7 +2,8 @@
{ {
public class MachineFamilyViewModel : BaseViewModel<int> public class MachineFamilyViewModel : BaseViewModel<int>
{ {
public string Company; public string Company { get; set; }
public string Name; public string Name { get; set; }
public int CompanyId { get; set; }
} }
} }