diff --git a/Marechai/Areas/Admin/Views/Screens/Edit.cshtml b/Marechai/Areas/Admin/Views/Screens/Edit.cshtml deleted file mode 100644 index 849219e0..00000000 --- a/Marechai/Areas/Admin/Views/Screens/Edit.cshtml +++ /dev/null @@ -1,70 +0,0 @@ -@model Marechai.Database.Models.Screen - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

Screen

-
-
-
-
-
-
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- - -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Screen.razor b/Marechai/Pages/Admin/Details/Screen.razor index 64b10114..f647ab50 100644 --- a/Marechai/Pages/Admin/Details/Screen.razor +++ b/Marechai/Pages/Admin/Details/Screen.razor @@ -31,9 +31,11 @@ } @page "/admin/screens/details/{Id:int}" +@page "/admin/screens/edit/{Id:int}" @inherits OwningComponentBase @inject IStringLocalizer L @inject ResolutionsService ResolutionsService +@inject NavigationManager NavigationManager @attribute [Authorize(Roles = "UberAdmin, Admin")]

@L["Screen details"]


@@ -46,41 +48,104 @@ }
- @if (_editable || _model.Width.HasValue) + @if (_editing || _model.Width.HasValue) { @L["Width (mm)"] - + @if (_editing) + { + @L["Unknown (width)"] + } + @if (!_editing || + !_unknownWidth) + { + + + + @L["Please enter a valid width in millimeters."] + + + + } } - @if (_editable || _model.Height.HasValue) + @if (_editing || _model.Height.HasValue) { @L["Height (mm)"] - + @if (_editing) + { + @L["Unknown (height)"] + } + @if (!_editing || + !_unknownHeight) + { + + + + @L["Please enter a valid height in millimeters."] + + + + } + } @L["Diagonal (inches)"] - + + + + @L["Please enter a correct diagonal size in inches."] + + + - @if (_editable || _model.EffectiveColors.HasValue) + @if (_editing || _model.EffectiveColors.HasValue) { @L["Effective colors"] - + @if (_editing) + { + @L["Unknown (effective colors)"] + } + @if (!_editing || + !_unknownColors) + { + + + + @L["Please enter a number of effective colors."] + + + + } } - @if (_editable || _model.Type != null) + @if (_editing || _model.Type != null) { @L["Type"] - + @if (_editing) + { + @L["Unknown (type)"] + } + @if (!_editing || + !_unknownType) + { + + + + @L["Please enter a valid screen type."] + + + + } } @L["Native resolution"] - @foreach (var resolution in _resolutions) { @resolution.ToString() @@ -89,6 +154,14 @@
- @L["Edit"] + @if (!_editing) + { + + } + else + { + + + } @L["Back to list"]
\ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Screen.razor.cs b/Marechai/Pages/Admin/Details/Screen.razor.cs index 55a87370..04f6d5c6 100644 --- a/Marechai/Pages/Admin/Details/Screen.razor.cs +++ b/Marechai/Pages/Admin/Details/Screen.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; @@ -7,10 +10,14 @@ namespace Marechai.Pages.Admin.Details { public partial class Screen { - bool _editable; + bool _editing; bool _loaded; - Database.Models.Screen _model; + ScreenViewModel _model; List _resolutions; + bool _unknownColors; + bool _unknownHeight; + bool _unknownType; + bool _unknownWidth; [Parameter] public int Id { get; set; } @@ -27,7 +34,73 @@ namespace Marechai.Pages.Admin.Details _resolutions = await ResolutionsService.GetAsync(); _model = await Service.GetAsync(Id); + _editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/screens/edit/", StringComparison.InvariantCulture); + + if(_editing) + SetCheckboxes(); + StateHasChanged(); } + + void SetCheckboxes() + { + _unknownWidth = !_model.Width.HasValue; + _unknownType = string.IsNullOrWhiteSpace(_model.Type); + _unknownHeight = !_model.Height.HasValue; + _unknownColors = !_model.Colors.HasValue; + } + + void OnEditClicked() + { + _editing = true; + SetCheckboxes(); + StateHasChanged(); + } + + async void OnCancelClicked() + { + _editing = false; + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + async void OnSaveClicked() + { + if(_unknownWidth) + _model.Width = null; + else if(_model.Width < 0) + return; + + if(_unknownHeight) + _model.Height = null; + else if(_model.Height < 0) + return; + + if(_unknownType) + _model.Type = null; + else if(string.IsNullOrWhiteSpace(_model.Type)) + return; + + if(_unknownColors) + _model.EffectiveColors = null; + else if(_model.EffectiveColors < 0) + return; + + _editing = false; + await Service.UpdateAsync(_model); + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + void ValidateDoubleBiggerThanZero(ValidatorEventArgs e) => + Validators.ValidateDoubleBiggerThanZero(e, 1, 131072); + + void ValidateLongBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateLongBiggerThanZero(e, 2); + + void ValidateType(ValidatorEventArgs e) => + Validators.ValidateStringWithMaxLength(e, L["Screen type cannot be bigger than 256 characters."], 256); } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/SoundSynth.razor b/Marechai/Pages/Admin/Details/SoundSynth.razor index 26d01696..75806992 100644 --- a/Marechai/Pages/Admin/Details/SoundSynth.razor +++ b/Marechai/Pages/Admin/Details/SoundSynth.razor @@ -126,7 +126,7 @@ @if (!_editing || !_unknownVoices) { - + @L["Please enter a valid number of voices."] @@ -147,7 +147,7 @@ @if (!_editing || !_unknownSampleRate) { - + @L["Please enter a valid sample rate."] @@ -168,7 +168,7 @@ @if (!_editing || !_unknownSampleResolution) { - + @L["Please enter a valid number of bits for sample resolution."] @@ -189,7 +189,7 @@ @if (!_editing || !_unknownSquareWaveChannels) { - + @L["Please enter a valid number of square wave channels."] @@ -210,7 +210,7 @@ @if (!_editing || !_unknownWhiteNoiseChannels) { - + @L["Please enter a valid number of white noise channels."] @@ -231,7 +231,7 @@ @if (!_editing || !_unknownType) { - + @L["Please enter a valid sound synthesizer type."] diff --git a/Marechai/Pages/Admin/Details/SoundSynth.razor.cs b/Marechai/Pages/Admin/Details/SoundSynth.razor.cs index e8daebc2..ae756bfd 100644 --- a/Marechai/Pages/Admin/Details/SoundSynth.razor.cs +++ b/Marechai/Pages/Admin/Details/SoundSynth.razor.cs @@ -139,10 +139,8 @@ namespace Marechai.Pages.Admin.Details void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateIntroducedDate(e); - void ValidateNullableIntegerBiggerThanZero(ValidatorEventArgs e) => - Validators.ValidateNullableIntegerBiggerThanZero(e); + void ValidateIntegerBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateIntegerBiggerThanZero(e); - void ValidateNullableDoubleBiggerThanZero(ValidatorEventArgs e) => - Validators.ValidateNullableDoubleBiggerThanZero(e); + void ValidateDoubleBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateDoubleBiggerThanZero(e); } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Screens.razor b/Marechai/Pages/Admin/Screens.razor index 09c8b805..665ed5f0 100644 --- a/Marechai/Pages/Admin/Screens.razor +++ b/Marechai/Pages/Admin/Screens.razor @@ -82,9 +82,7 @@ @L["Details"] - - @L["Edit"] - + @L["Edit"] diff --git a/Marechai/Resources/Services/ScreensService.en.resx b/Marechai/Resources/Services/ScreensService.en.resx new file mode 100644 index 00000000..a976b945 --- /dev/null +++ b/Marechai/Resources/Services/ScreensService.en.resx @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 screen width in millimeters + + + Unknown + Unknown, referring to a screen height in millimeters + + + Unknown + Unknown, referring to a screen effective colors + + + Unknown + Unknown, referring to a screen type + + \ No newline at end of file diff --git a/Marechai/Resources/Services/ScreensService.es.resx b/Marechai/Resources/Services/ScreensService.es.resx index 499c8786..dc29872e 100644 --- a/Marechai/Resources/Services/ScreensService.es.resx +++ b/Marechai/Resources/Services/ScreensService.es.resx @@ -194,4 +194,48 @@ Diagonal (pulgadas) Diagonal (inches) + + Guardar + Save + + + Desconocido + Unknown, referring to a screen width in millimeters + + + Desconocido + Unknown, referring to a screen height in millimeters + + + Desconocidos + Unknown, referring to a screen effective colors + + + Desconocido + Unknown, referring to a screen type + + + Por favor introduce un ancho válido en milímetros + Please enter a valid width in millimeters. + + + Por favor introduce una altura válida en milímetros + Please enter a valid height in millimeters. + + + Por favor introduce una diagonal válida en pulgadas + Please enter a correct diagonal size in inches. + + + Por favor introduce un número válido de colores efectivos + Please enter a number of effective colors. + + + Por favor introduce un tipo de pantalla válido + Please enter a valid screen type. + + + El tipo de pantalla no puede tener más de 256 caracteres. + Screen type cannot be bigger than 256 characters. + \ No newline at end of file diff --git a/Marechai/Services/ScreensService.cs b/Marechai/Services/ScreensService.cs index 0a739da2..3dc86249 100644 --- a/Marechai/Services/ScreensService.cs +++ b/Marechai/Services/ScreensService.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Marechai.Database.Models; +using Marechai.ViewModels; +using Microsoft.EntityFrameworkCore; namespace Marechai.Services { @@ -15,7 +17,40 @@ namespace Marechai.Services ThenBy(s => s.EffectiveColors).ThenBy(s => s.NativeResolution.ToString()). ThenBy(s => s.Type).ThenBy(s => s.Size).ToList(); - public async Task GetAsync(int id) => await _context.Screens.FindAsync(id); + public async Task GetAsync(int id) => + await _context.Screens.Where(s => s.Id == id).Select(s => new ScreenViewModel + { + Diagonal = s.Diagonal, EffectiveColors = s.EffectiveColors, Height = s.Height, Id = s.Id, + NativeResolution = new ResolutionViewModel + { + Chars = s.NativeResolution.Chars, Colors = s.NativeResolution.Colors, + Grayscale = s.NativeResolution.Grayscale, Height = s.NativeResolution.Height, + Id = s.NativeResolution.Id, Palette = s.NativeResolution.Palette, Width = s.NativeResolution.Width + }, + NativeResolutionId = s.NativeResolutionId, Type = s.Type, Width = s.Width + }).FirstOrDefaultAsync(); + + public async Task UpdateAsync(ScreenViewModel viewModel) + { + Screen model = await _context.Screens.FindAsync(viewModel.Id); + + if(model is null) + return; + + Resolution nativeResolution = await _context.Resolutions.FindAsync(viewModel.NativeResolutionId); + + if(nativeResolution is null) + return; + + model.Diagonal = viewModel.Diagonal; + model.EffectiveColors = viewModel.EffectiveColors; + model.Height = viewModel.Height; + model.NativeResolutionId = viewModel.NativeResolutionId; + model.Type = viewModel.Type; + model.Width = viewModel.Width; + + await _context.SaveChangesAsync(); + } public async Task DeleteAsync(int id) { diff --git a/Marechai/Shared/Validators.cs b/Marechai/Shared/Validators.cs index d82c43fc..d736d548 100644 --- a/Marechai/Shared/Validators.cs +++ b/Marechai/Shared/Validators.cs @@ -28,19 +28,28 @@ namespace Marechai.Shared e.Status = ValidationStatus.Success; } - public static void ValidateNullableDoubleBiggerThanZero(ValidatorEventArgs e) + public static void ValidateDoubleBiggerThanZero(ValidatorEventArgs e, double minValue = 0, double maxValue = double.MaxValue) { if(!(e.Value is double item) || - item < 0) + item < minValue || item > maxValue) e.Status = ValidationStatus.Error; else e.Status = ValidationStatus.Success; } - public static void ValidateNullableIntegerBiggerThanZero(ValidatorEventArgs e) + public static void ValidateIntegerBiggerThanZero(ValidatorEventArgs e, int minValue = 0, int maxValue = int.MaxValue) { if(!(e.Value is int item) || - item < 0) + item < minValue || item > maxValue) + e.Status = ValidationStatus.Error; + else + e.Status = ValidationStatus.Success; + } + + public static void ValidateLongBiggerThanZero(ValidatorEventArgs e, long minValue = 0, long maxValue = long.MaxValue) + { + if(!(e.Value is long item) || + item < minValue || item > maxValue) e.Status = ValidationStatus.Error; else e.Status = ValidationStatus.Success; diff --git a/Marechai/ViewModels/ScreenViewModel.cs b/Marechai/ViewModels/ScreenViewModel.cs new file mode 100644 index 00000000..a7fa135e --- /dev/null +++ b/Marechai/ViewModels/ScreenViewModel.cs @@ -0,0 +1,31 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace Marechai.ViewModels +{ + public class ScreenViewModel : BaseViewModel + { + public double? Width { get; set; } + public double? Height { get; set; } + public double Diagonal { get; set; } + public int NativeResolutionId { get; set; } + public ResolutionViewModel NativeResolution { get; set; } + public long? EffectiveColors { get; set; } + public string Type { get; set; } + + [NotMapped] + public long? Colors => EffectiveColors ?? NativeResolution.Colors; + + [NotMapped] + public string Size + { + get + { + if(Width != null && + Height != null) + return$"{Width}x{Height} mm"; + + return"Unknown"; + } + } + } +} \ No newline at end of file