Add machine editing in admin view.

This commit is contained in:
2020-05-27 14:24:47 +01:00
parent c93195dfb3
commit bae525e37e
9 changed files with 332 additions and 236 deletions

View File

@@ -1,100 +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
*******************************************************************************/
}
@using Marechai.Database
@model Marechai.Database.Models.Machine
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>Machine</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="Company" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
</div>
<div class="form-group">
<label asp-for="Family" class="control-label">
</label>
<select asp-for="FamilyId" class="form-control" asp-items="ViewBag.FamilyId">
</select>
</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">
<label asp-for="Model" class="control-label">
</label>
<input asp-for="Model" class="form-control" />
<span asp-validation-for="Model" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Introduced" class="control-label">
</label>
<input asp-for="Introduced" class="form-control" />
<span asp-validation-for="Introduced" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Type" class="control-label">
</label>
<select asp-for="Type" class="form-control" asp-items="Html.GetEnumSelectList<MachineType>().OrderBy(s => s.Text)">
</select>
<span asp-validation-for="Type" 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

@@ -1,103 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Index.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view index
//
// --[ 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 IEnumerable<Marechai.Areas.Admin.Models.MachineViewModel>
@{
ViewData["Title"] = "Machines (Admin)";
}
<h2>Machines</h2>
<p>
<a asp-action="Create" class="btn btn-primary">
Create new
</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Company)
</th>
<th>
@Html.DisplayNameFor(model => model.Family)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Model)
</th>
<th>
@Html.DisplayNameFor(model => model.Introduced)
</th>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Company)
</td>
<td>
@Html.DisplayFor(modelItem => item.Family)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model)
</td>
<td>
@Html.DisplayFor(modelItem => item.IntroducedView)
</td>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">
Details
</a>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">
Edit
</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">
Delete
</a>
</td>
</tr>
}
</tbody>
</table>

View File

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

View File

@@ -31,11 +31,13 @@
}
@page "/admin/machines/details/{Id:int}"
@page "/admin/machines/edit/{Id:int}"
@using Marechai.Database
@inherits OwningComponentBase<MachinesService>
@inject IStringLocalizer<MachinesService> L
@inject CompaniesService CompaniesService
@inject MachineFamiliesService MachineFamiliesService
@inject NavigationManager NavigationManager
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Machine details"]</h3>
<hr />
@@ -50,7 +52,7 @@
<div>
<Field>
<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)
{
<SelectItem TValue="int" Value="@company.Id">@company.Name</SelectItem>
@@ -59,52 +61,96 @@
</Field>
<Field>
<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>
</Field>
<Field>
<FieldLabel>@L["Type"]</FieldLabel>
<Select Disabled="!_editable" TValue="int" @bind-SelectedValue="@Type">
<Select Disabled="!_editing" TValue="int" @bind-SelectedValue="@Type">
@foreach (int type in Enum.GetValues(typeof(MachineType)))
{
<SelectItem TValue="int" Value="@type">@(((MachineType)type).ToString())</SelectItem>
}
</Select>
</Field>
@if (_editable || _model.Model != null)
@if (_editing || _model.Model != null)
{
<Field>
<FieldLabel>@L["Model"]</FieldLabel>
<TextEdit ReadOnly="!_editable" @bind-Text="@_model.Model"/>
<FieldLabel>@L["Model code"]</FieldLabel>
@if (_editing)
{
<Check TValue="bool" @bind-Checked="@_unknownModel">@L["Unknown (model)"]</Check>
}
@if (!_editing ||
!_unknownModel)
{
<Validation Validator="@ValidateModel">
<TextEdit Disabled="!_editing" @bind-Text="@_model.Model">
<Feedback>
<ValidationError>@L["Please enter a valid model."]</ValidationError>
</Feedback>
</TextEdit>
</Validation>
}
</Field>
}
@if(_editable || _model.Introduced.HasValue)
@if (_editing || _model.Introduced.HasValue)
{
<Field>
<FieldLabel>@L["Introduced"]</FieldLabel>
@if(_model.Introduced?.Year == 1000)
@if (_editing)
{
<TextEdit ReadOnly="true">@L["PROTOTYPE"]</TextEdit>
<Check TValue="bool" Disabled="_prototype" @bind-Checked="@_unknownIntroduced">@L["Unknown (introduction date)"]</Check>
<Check TValue="bool" Disabled="_unknownIntroduced" @bind-Checked="@_prototype">@L["Prototype"]</Check>
}
else
@if (!_editing ||
(!_prototype && !_unknownIntroduced))
{
<DateEdit ReadOnly="!_editable" TValue="DateTime?" @bind-Date="@_model.Introduced"/>
<Validation Validator="@ValidateIntroduced">
<DateEdit Disabled="!_editing" TValue="DateTime?" @bind-Date="@_model.Introduced">
<Feedback>
<ValidationError>@L["Please enter an introduction date."]</ValidationError>
</Feedback>
</DateEdit>
</Validation>
}
</Field>
</Field>
}
@if (_editable || _model.FamilyId.HasValue)
@if (_editing || _model.FamilyId != null)
{
<Field>
<FieldLabel>@L["Family"]</FieldLabel>
<Select Disabled="!_editable" TValue="int?" @bind-SelectedValue="@_model.FamilyId">
@foreach (MachineFamilyViewModel family in _families)
{
<SelectItem TValue="int?" Value="@family.Id">@family.Name</SelectItem>
}
</Select>
@if (_editing)
{
<Check TValue="bool" @bind-Checked="@_noFamily">@L["No family"]</Check>
}
@if (!_editing ||
!_noFamily)
{
<Select Disabled="!_editing" TValue="int?" @bind-SelectedValue="@_model.FamilyId">
@foreach (MachineFamilyViewModel family in _families)
{
<SelectItem TValue="int?" Value="@family.Id">@family.Name</SelectItem>
}
</Select>
}
</Field>
}
</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/machines" class="btn btn-secondary">@L["Back to list"]</a>
</div>

View File

@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Blazorise;
using Marechai.Database;
using Marechai.Shared;
using Marechai.ViewModels;
using Microsoft.AspNetCore.Components;
@@ -9,10 +12,14 @@ namespace Marechai.Pages.Admin.Details
public partial class Machine
{
List<CompanyViewModel> _companies;
bool _editable;
bool _editing;
List<MachineFamilyViewModel> _families;
bool _loaded;
Database.Models.Machine _model;
MachineViewModel _model;
bool _noFamily;
bool _prototype;
bool _unknownIntroduced;
bool _unknownModel;
[Parameter]
public int Id { get; set; }
@@ -36,7 +43,70 @@ namespace Marechai.Pages.Admin.Details
_families = await MachineFamiliesService.GetAsync();
_model = await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machines/edit/", StringComparison.InvariantCulture);
if(_editing)
SetCheckboxes();
StateHasChanged();
}
void SetCheckboxes()
{
_noFamily = !_model.FamilyId.HasValue;
_prototype = _model.Introduced?.Year == 1000;
_unknownIntroduced = !_model.Introduced.HasValue;
_unknownModel = string.IsNullOrWhiteSpace(_model.Model);
}
void OnEditClicked()
{
_editing = true;
SetCheckboxes();
StateHasChanged();
}
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
}
async void OnSaveClicked()
{
if(_noFamily)
_model.FamilyId = null;
else if(_model.FamilyId < 0)
return;
if(_unknownIntroduced)
_model.Introduced = null;
else if(_prototype)
_model.Introduced = new DateTime(1000, 1, 1);
else if(_model.Introduced?.Date >= DateTime.UtcNow.Date)
return;
if(_unknownModel)
_model.Model = null;
else if(string.IsNullOrWhiteSpace(_model.Model))
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
}
void ValidateName(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Name must contain less than 255 characters."], 255);
void ValidateModel(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Model must contain less than 50 characters."], 50);
void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateIntroducedDate(e);
}
}

View File

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

View File

@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- ReSharper disable MarkupTextTypo -->
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Unknown (model)" xml:space="preserve">
<value>Unknown</value>
<comment>Unknown, referring to a model</comment>
</data>
<data name="Unknown (introduction date)" xml:space="preserve">
<value>Unknown</value>
<comment>Unknown, referring to an introduction date</comment>
</data>
<data name="None (family)" xml:space="preserve">
<value>None</value>
<comment>None, referring to a machine family</comment>
</data>
</root>

View File

@@ -482,12 +482,40 @@
<value>Volver a la lista</value>
<comment>Back to list</comment>
</data>
<data name="Model" xml:space="preserve">
<value>Modelo</value>
<comment>Model</comment>
<data name="Save" xml:space="preserve">
<value>Guardar</value>
<comment>Save</comment>
</data>
<data name="Family" xml:space="preserve">
<value>Familia</value>
<comment>Family</comment>
<data name="Unknown (model)" xml:space="preserve">
<value>Desconocido</value>
<comment>Unknown, referring to a model</comment>
</data>
<data name="Unknown (introduction date)" xml:space="preserve">
<value>Desconocida</value>
<comment>Unknown, referring to an introduction date</comment>
</data>
<data name="None (family)" xml:space="preserve">
<value>Ninguna</value>
<comment>None, referring to a machine family</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="Please enter a valid model." xml:space="preserve">
<value>Por favor introduce un modelo válido.</value>
<comment>Please enter a valid model.</comment>
</data>
<data name="Please enter an introduction date." xml:space="preserve">
<value>Por favor introduce una fecha válida.</value>
<comment>Please enter an introduction date.</comment>
</data>
<data name="Name must contain less than 255 characters." xml:space="preserve">
<value>El nombre debe contener menos de 255 caracteres.</value>
<comment>Name must contain less than 255 characters.</comment>
</data>
<data name="Model must contain less than 50 characters." xml:space="preserve">
<value>El modelo debe contener menos de 50 caracteres.</value>
<comment>Model must contain less than 50 characters.</comment>
</data>
</root>

View File

@@ -35,7 +35,31 @@ namespace Marechai.Services
Introduced = m.Introduced, Type = m.Type, Family = m.Family.Name
}).ToListAsync();
public async Task<Machine> GetAsync(int id) => await _context.Machines.FindAsync(id);
public async Task<MachineViewModel> GetAsync(int id) => await _context.Machines.Where(m => m.Id == id).
Select(m => new MachineViewModel
{
Id = m.Id, CompanyId = m.CompanyId,
Name = m.Name, Model = m.Model,
Introduced = m.Introduced,
Type = m.Type, FamilyId = m.FamilyId
}).FirstOrDefaultAsync();
public async Task UpdateAsync(MachineViewModel viewModel)
{
Machine model = await _context.Machines.FindAsync(viewModel.Id);
if(model is null)
return;
model.CompanyId = viewModel.CompanyId;
model.Name = viewModel.Name;
model.Model = viewModel.Model;
model.Introduced = viewModel.Introduced;
model.Type = viewModel.Type;
model.FamilyId = viewModel.FamilyId;
await _context.SaveChangesAsync();
}
public async Task<MachineViewModel> GetMachine(int id)
{