diff --git a/Marechai/Areas/Admin/Views/Machines/Edit.cshtml b/Marechai/Areas/Admin/Views/Machines/Edit.cshtml deleted file mode 100644 index ec1e3225..00000000 --- a/Marechai/Areas/Admin/Views/Machines/Edit.cshtml +++ /dev/null @@ -1,100 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Edit.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@using Marechai.Database -@model Marechai.Database.Models.Machine - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

Machine

-
-
-
-
-
-
- -
- - -
-
- - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/Machines/Index.cshtml b/Marechai/Areas/Admin/Views/Machines/Index.cshtml deleted file mode 100644 index 24484355..00000000 --- a/Marechai/Areas/Admin/Views/Machines/Index.cshtml +++ /dev/null @@ -1,103 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Index.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model IEnumerable - -@{ - ViewData["Title"] = "Machines (Admin)"; -} -

Machines

-

- - Create new - -

- - - - - - - - - - - - - - @foreach (var item in Model) - { - - - - - - - - - - } - -
- @Html.DisplayNameFor(model => model.Company) - - @Html.DisplayNameFor(model => model.Family) - - @Html.DisplayNameFor(model => model.Name) - - @Html.DisplayNameFor(model => model.Model) - - @Html.DisplayNameFor(model => model.Introduced) - - @Html.DisplayNameFor(model => model.Type) -
- @Html.DisplayFor(modelItem => item.Company) - - @Html.DisplayFor(modelItem => item.Family) - - @Html.DisplayFor(modelItem => item.Name) - - @Html.DisplayFor(modelItem => item.Model) - - @Html.DisplayFor(modelItem => item.IntroducedView) - - @Html.DisplayFor(modelItem => item.Type) - - - Details - - - Edit - - - Delete - -
\ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index b1fd1f4e..af0a12ab 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1225 + 3.0.99.1226 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Admin/Details/Machine.razor b/Marechai/Pages/Admin/Details/Machine.razor index 8f99adab..b6ba8c0c 100644 --- a/Marechai/Pages/Admin/Details/Machine.razor +++ b/Marechai/Pages/Admin/Details/Machine.razor @@ -31,11 +31,13 @@ } @page "/admin/machines/details/{Id:int}" +@page "/admin/machines/edit/{Id:int}" @using Marechai.Database @inherits OwningComponentBase @inject IStringLocalizer L @inject CompaniesService CompaniesService @inject MachineFamiliesService MachineFamiliesService +@inject NavigationManager NavigationManager @attribute [Authorize(Roles = "UberAdmin, Admin")]

@L["Machine details"]


@@ -50,7 +52,7 @@
@L["Company"] - @foreach (var company in _companies) { @company.Name @@ -59,52 +61,96 @@ @L["Name"] - + + + + @L["Please enter a valid name."] + + + @L["Type"] - @foreach (int type in Enum.GetValues(typeof(MachineType))) { @(((MachineType)type).ToString()) } - @if (_editable || _model.Model != null) + @if (_editing || _model.Model != null) { - @L["Model"] - + @L["Model code"] + @if (_editing) + { + @L["Unknown (model)"] + } + @if (!_editing || + !_unknownModel) + { + + + + @L["Please enter a valid model."] + + + + } } - @if(_editable || _model.Introduced.HasValue) + @if (_editing || _model.Introduced.HasValue) { @L["Introduced"] - @if(_model.Introduced?.Year == 1000) + @if (_editing) { - @L["PROTOTYPE"] + @L["Unknown (introduction date)"] + @L["Prototype"] } - else + @if (!_editing || + (!_prototype && !_unknownIntroduced)) { - + + + + @L["Please enter an introduction date."] + + + } - + } - @if (_editable || _model.FamilyId.HasValue) + @if (_editing || _model.FamilyId != null) { @L["Family"] - + @if (_editing) + { + @L["No family"] + } + @if (!_editing || + !_noFamily) + { + + } }
- @L["Edit"] + @if (!_editing) + { + + } + else + { + + + } @L["Back to list"]
\ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Machine.razor.cs b/Marechai/Pages/Admin/Details/Machine.razor.cs index e9e39788..02f4736e 100644 --- a/Marechai/Pages/Admin/Details/Machine.razor.cs +++ b/Marechai/Pages/Admin/Details/Machine.razor.cs @@ -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 _companies; - bool _editable; + bool _editing; List _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); } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Machines.razor b/Marechai/Pages/Admin/Machines.razor index ae86fa3b..f6c6e2e3 100644 --- a/Marechai/Pages/Admin/Machines.razor +++ b/Marechai/Pages/Admin/Machines.razor @@ -94,9 +94,7 @@ @L["Details"] - - @L["Edit"] - + @L["Edit"] diff --git a/Marechai/Resources/Services/MachinesService.en.resx b/Marechai/Resources/Services/MachinesService.en.resx new file mode 100644 index 00000000..a46724cf --- /dev/null +++ b/Marechai/Resources/Services/MachinesService.en.resx @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Unknown + Unknown, referring to a model + + + Unknown + Unknown, referring to an introduction date + + + None + None, referring to a machine family + + \ No newline at end of file diff --git a/Marechai/Resources/Services/MachinesService.es.resx b/Marechai/Resources/Services/MachinesService.es.resx index b6cc37ec..bf4bfd22 100644 --- a/Marechai/Resources/Services/MachinesService.es.resx +++ b/Marechai/Resources/Services/MachinesService.es.resx @@ -482,12 +482,40 @@ Volver a la lista Back to list - - Modelo - Model + + Guardar + Save - - Familia - Family + + Desconocido + Unknown, referring to a model + + + Desconocida + Unknown, referring to an introduction date + + + Ninguna + None, referring to a machine family + + + Por favor introduce un nombre válido. + Please enter a valid name. + + + Por favor introduce un modelo válido. + Please enter a valid model. + + + Por favor introduce una fecha válida. + Please enter an introduction date. + + + El nombre debe contener menos de 255 caracteres. + Name must contain less than 255 characters. + + + El modelo debe contener menos de 50 caracteres. + Model must contain less than 50 characters. \ No newline at end of file diff --git a/Marechai/Services/MachinesService.cs b/Marechai/Services/MachinesService.cs index 6f22a870..de8c637e 100644 --- a/Marechai/Services/MachinesService.cs +++ b/Marechai/Services/MachinesService.cs @@ -35,7 +35,31 @@ namespace Marechai.Services Introduced = m.Introduced, Type = m.Type, Family = m.Family.Name }).ToListAsync(); - public async Task GetAsync(int id) => await _context.Machines.FindAsync(id); + public async Task 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 GetMachine(int id) {