From 8febbfa860070cc954218af86c365dda4a32634e Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 27 May 2020 19:01:11 +0100 Subject: [PATCH] Add GPU editing in admin view. --- Marechai/Areas/Admin/Views/Gpus/Edit.cshtml | 122 ------------ Marechai/Marechai.csproj | 2 +- Marechai/Pages/Admin/Details/Gpu.razor | 182 +++++++++++++++--- Marechai/Pages/Admin/Details/Gpu.razor.cs | 119 +++++++++++- Marechai/Pages/Admin/Gpus.razor | 4 +- .../Resources/Services/GpusService.en.resx | 153 +++++++++++++++ .../Resources/Services/GpusService.es.resx | 72 +++++++ Marechai/Services/GpusService.cs | 28 ++- 8 files changed, 522 insertions(+), 160 deletions(-) delete mode 100644 Marechai/Areas/Admin/Views/Gpus/Edit.cshtml create mode 100644 Marechai/Resources/Services/GpusService.en.resx diff --git a/Marechai/Areas/Admin/Views/Gpus/Edit.cshtml b/Marechai/Areas/Admin/Views/Gpus/Edit.cshtml deleted file mode 100644 index 528d5521..00000000 --- a/Marechai/Areas/Admin/Views/Gpus/Edit.cshtml +++ /dev/null @@ -1,122 +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 -*******************************************************************************/ -} -@model Marechai.Database.Models.Gpu - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

GPU

-
-
-
-
-
-
- -
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 3c931fcb..5ed3d755 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1229 + 3.0.99.1230 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Admin/Details/Gpu.razor b/Marechai/Pages/Admin/Details/Gpu.razor index cc0727a4..bf7a2c9e 100644 --- a/Marechai/Pages/Admin/Details/Gpu.razor +++ b/Marechai/Pages/Admin/Details/Gpu.razor @@ -31,9 +31,11 @@ } @page "/admin/gpus/details/{Id:int}" +@page "/admin/gpus/edit/{Id:int}" @inherits OwningComponentBase @inject IStringLocalizer L @inject CompaniesService CompaniesService +@inject NavigationManager NavigationManager @attribute [Authorize(Roles = "UberAdmin, Admin")]

@L["Graphical processing unit details"]


@@ -46,76 +48,194 @@ }
- @if (_editable || _model.CompanyId != null) + @if (_editing || _model.CompanyId != null) { @L["Company"] - + @if (_editing) + { + @L["Unknown (company)"] + } + @if (!_editing || + !_unknownCompany) + { + + } } - @if (_editable || _model.Name != null) - { - - @L["Name"] - - - } - @if (_editable || _model.ModelCode != null) + + @L["Name"] + + + + @L["Please enter a valid name."] + + + + + @if (_editing || _model.ModelCode != null) { @L["Model code"] - + @if (_editing) + { + @L["Unknown (model code)"] + } + @if (!_editing || + !_unknownModelCode) + { + + + + @L["Please enter a valid model code."] + + + + } } - @if (_editable || _model.Introduced != null) + @if (_editing || _model.Introduced.HasValue) { @L["Introduced"] - - + @if (_editing) + { + @L["Unknown (introduction date)"] + @L["Prototype"] + } + @if (!_editing || + (!_prototype && !_unknownIntroduced)) + { + + + + @L["Please enter an introduction date."] + + + + } + } - @if (_editable || _model.Package != null) + @if (_editing || _model.Package != null) { @L["Package"] - + @if (_editing) + { + @L["Unknown (package)"] + } + @if (!_editing || + !_unknownPackage) + { + + + + @L["Please enter a valid package."] + + + + } } - @if (_editable || _model.Process != null) + @if (_editing || _model.Process != null) { @L["Process"] - + @if (_editing) + { + @L["Unknown (process)"] + } + @if (!_editing || + !_unknownProcess) + { + + + + @L["Please enter a valid process."] + + + + } } - @if (_editable || _model.ProcessNm != null) + @if (_editing || _model.ProcessNm.HasValue) { @L["Process (nm)"] - + @if (_editing) + { + @L["Unknown (process size)"] + } + @if (!_editing || + !_unknownProcessNm) + { + + + + @L["Please enter a valid process size in nanometers."] + + + + } } - @if (_editable || _model.DieSize != null) + @if (_editing || _model.DieSize.HasValue) { @L["Die size (mm²)"] - + @if (_editing) + { + @L["Unknown (die size)"] + } + @if (!_editing || + !_unknownDieSize) + { + + + + @L["Please enter a valid die size in square millimeters."] + + + + } } - @if (_editable || _model.Transistors != null) + @if (_editing || _model.Transistors.HasValue) { @L["Transistors"] - + @if (_editing) + { + @L["Unknown (transistors)"] + } + @if (!_editing || + !_unknownTransistors) + { + + + + @L["Please enter a valid number of transistors."] + + + + } }
- @L["Edit"] - @L["Back to list"] + @if (!_editing) + { + + } + else + { + + + } + @L["Back to list"]
\ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Gpu.razor.cs b/Marechai/Pages/Admin/Details/Gpu.razor.cs index 8d063e2a..379fea5c 100644 --- a/Marechai/Pages/Admin/Details/Gpu.razor.cs +++ b/Marechai/Pages/Admin/Details/Gpu.razor.cs @@ -1,5 +1,8 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; +using Blazorise; +using Marechai.Shared; using Marechai.ViewModels; using Microsoft.AspNetCore.Components; @@ -8,9 +11,18 @@ namespace Marechai.Pages.Admin.Details public partial class Gpu { List _companies; - bool _editable; + bool _editing; bool _loaded; - Database.Models.Gpu _model; + GpuViewModel _model; + bool _prototype; + bool _unknownCompany; + bool _unknownDieSize; + bool _unknownIntroduced; + bool _unknownModelCode; + bool _unknownPackage; + bool _unknownProcess; + bool _unknownProcessNm; + bool _unknownTransistors; [Parameter] public int Id { get; set; } @@ -27,7 +39,110 @@ namespace Marechai.Pages.Admin.Details _companies = await CompaniesService.GetAsync(); _model = await Service.GetAsync(Id); + _editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/gpus/edit/", StringComparison.InvariantCulture); + + if(_editing) + SetCheckboxes(); + StateHasChanged(); } + + void SetCheckboxes() + { + _unknownCompany = !_model.CompanyId.HasValue; + _unknownDieSize = !_model.DieSize.HasValue; + _unknownIntroduced = !_model.Introduced.HasValue; + _unknownModelCode = string.IsNullOrWhiteSpace(_model.ModelCode); + _unknownPackage = string.IsNullOrWhiteSpace(_model.Package); + _unknownProcess = string.IsNullOrWhiteSpace(_model.Process); + _unknownProcessNm = !_model.ProcessNm.HasValue; + _unknownTransistors = !_model.Transistors.HasValue; + _prototype = _model.Introduced?.Year == 1000; + } + + void OnEditClicked() + { + _editing = true; + SetCheckboxes(); + StateHasChanged(); + } + + async void OnCancelClicked() + { + _editing = false; + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + async void OnSaveClicked() + { + if(_unknownCompany) + _model.CompanyId = null; + else if(_model.CompanyId < 0) + return; + + if(_unknownModelCode) + _model.ModelCode = null; + else if(string.IsNullOrWhiteSpace(_model.ModelCode)) + return; + + if(_unknownIntroduced) + _model.Introduced = null; + else if(_prototype) + _model.Introduced = new DateTime(1000, 1, 1); + else if(_model.Introduced >= DateTime.UtcNow.Date) + return; + + if(_unknownPackage) + _model.Package = null; + else if(string.IsNullOrWhiteSpace(_model.Package)) + return; + + if(_unknownProcess) + _model.Process = null; + else if(string.IsNullOrWhiteSpace(_model.Process)) + return; + + if(_unknownProcessNm) + _model.ProcessNm = null; + else if(_model.ProcessNm < 1) + return; + + if(_unknownDieSize) + _model.DieSize = null; + else if(_model.DieSize < 1) + return; + + if(_unknownTransistors) + _model.Transistors = null; + else if(_model.Transistors < 0) + return; + + _editing = false; + await Service.UpdateAsync(_model); + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + void ValidateName(ValidatorEventArgs e) => + Validators.ValidateString(e, L["Name must be 50 characters or less."], 50); + + void ValidateModelCode(ValidatorEventArgs e) => + Validators.ValidateString(e, L["Model code must be 45 characters or less."], 45); + + void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateIntroducedDate(e); + + void ValidatePackage(ValidatorEventArgs e) => + Validators.ValidateString(e, L["Package must be 45 characters or less."], 45); + + void ValidateLongBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateLong(e); + + void ValidateFloatBiggerThanOne(ValidatorEventArgs e) => Validators.ValidateFloat(e, 1); + + void ValidateProcess(ValidatorEventArgs e) => + Validators.ValidateString(e, L["Process must be 45 characters or less."], 45); } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Gpus.razor b/Marechai/Pages/Admin/Gpus.razor index 7ab0c55c..3f564daf 100644 --- a/Marechai/Pages/Admin/Gpus.razor +++ b/Marechai/Pages/Admin/Gpus.razor @@ -82,9 +82,7 @@ @L["Details"] - - @L["Edit"] - + @L["Edit"] diff --git a/Marechai/Resources/Services/GpusService.en.resx b/Marechai/Resources/Services/GpusService.en.resx new file mode 100644 index 00000000..42f4ebe8 --- /dev/null +++ b/Marechai/Resources/Services/GpusService.en.resx @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 company + + + Unknown + Unknown, referring to a model code + + + Unknown + Unknown, referring to an introduction date + + + Unknown + Unknown, referring to a chip package + + + Unknown + Unknown, referring to the chip process + + + Unknown + Unknown, referring to the chip process size in nanometers + + + Unknown + Unknown, referring to the die size in square millimeters + + + Unknown + Unknown, referring to the number of transistors + + \ No newline at end of file diff --git a/Marechai/Resources/Services/GpusService.es.resx b/Marechai/Resources/Services/GpusService.es.resx index 3489ef45..f277b6c4 100644 --- a/Marechai/Resources/Services/GpusService.es.resx +++ b/Marechai/Resources/Services/GpusService.es.resx @@ -198,4 +198,76 @@ Transistores Transistors + + Guardar + Save + + + Desconocida + Unknown, referring to a company + + + Desconocido + Unknown, referring to a model code + + + Desconocida + Unknown, referring to an introduction date + + + Desconocido + Unknown, referring to a chip package + + + Desconocido + Unknown, referring to the chip process + + + Desconocido + Unknown, referring to the chip process size in nanometers + + + Desconocida + Unknown, referring to the die size in square millimeters + + + Desconocidos + Unknown, referring to the number of transistors + + + Por favor introduce un nombre válido. + Please enter a valid name. + + + Por favor introduce un modelo válido. + Please enter a valid model code. + + + Prototipo + Prototype + + + Por favor elige una fecha válida. + Please enter an introduction date. + + + Por favor introduce un empaquetado válido. + Please enter a valid package. + + + Por favor introduce un proceso válido. + Please enter a valid process. + + + Por favor introduce un tamaño de proceso en nanómetros válido. + Please enter a valid process size in nanometers. + + + Por favor introduce un área del chip en milímetros cuadrados válida. + Please enter a valid die size in square millimeters. + + + Por favor introduce un número válido de transistores. + Please enter a valid number of transistors. + \ No newline at end of file diff --git a/Marechai/Services/GpusService.cs b/Marechai/Services/GpusService.cs index 2d52a441..eca8b3d5 100644 --- a/Marechai/Services/GpusService.cs +++ b/Marechai/Services/GpusService.cs @@ -31,7 +31,33 @@ namespace Marechai.Services Transistors = g.Transistors }).ToListAsync(); - public async Task GetAsync(int id) => await _context.Gpus.FindAsync(id); + public async Task GetAsync(int id) => + await _context.Gpus.Where(g => g.Id == id).Select(g => new GpuViewModel + { + Id = g.Id, Name = g.Name, CompanyId = g.Company.Id, ModelCode = g.ModelCode, + Introduced = g.Introduced, Package = g.Package, Process = g.Process, ProcessNm = g.ProcessNm, + DieSize = g.DieSize, Transistors = g.Transistors + }).FirstOrDefaultAsync(); + + public async Task UpdateAsync(GpuViewModel viewModel) + { + Gpu model = await _context.Gpus.FindAsync(viewModel.Id); + + if(model is null) + return; + + model.Name = viewModel.Name; + model.CompanyId = viewModel.CompanyId; + model.ModelCode = viewModel.ModelCode; + model.Introduced = viewModel.Introduced; + model.Package = viewModel.Package; + model.Process = viewModel.Process; + model.ProcessNm = viewModel.ProcessNm; + model.DieSize = viewModel.DieSize; + model.Transistors = viewModel.Transistors; + + await _context.SaveChangesAsync(); + } public async Task DeleteAsync(int id) {