diff --git a/Marechai/Areas/Admin/Views/DocumentPeople/Edit.cshtml b/Marechai/Areas/Admin/Views/DocumentPeople/Edit.cshtml deleted file mode 100644 index 42c0c4df..00000000 --- a/Marechai/Areas/Admin/Views/DocumentPeople/Edit.cshtml +++ /dev/null @@ -1,64 +0,0 @@ -@model Marechai.Database.Models.DocumentPerson - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

Document person

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

@L["Document person details"]


@@ -46,48 +48,120 @@ }
- @if (_editable || _model.Name != null) + @if (_editing || _model.Name != null) { @L["Name"] - + @if (_editing) + { + @L["Unknown (name)"] + } + @if (!_editing || + !_unknownName) + { + + + + @L["Please enter a valid name."] + + + + } } - @if (_editable || _model.Surname != null) + @if (_editing || _model.Surname != null) { @L["Surname"] - + @if (_editing) + { + @L["Unknown (surname)"] + } + @if (!_editing || + !_unknownSurname) + { + + + + @L["Please enter a valid surname."] + + + + } } - @if (_editable || _model.Alias != null) + @if (_editing || _model.Alias != null) { @L["Alias"] - + @if (_editing) + { + @L["Unknown (alias)"] + } + @if (!_editing || + !_unknownAlias) + { + + + + @L["Please enter a valid alias."] + + + + } } - @if (_editable || _model.DisplayName != null) + @if (_editing || _model.DisplayName != null) { @L["Display name"] - + @if (_editing) + { + @L["Unknown (display name)"] + } + @if (!_editing || + !_unknownDisplayName) + { + + + + @L["Please enter a valid display name."] + + + + } } - @if (_editable || _model.PersonId != null) + @if (_editing || _model.PersonId != null) { @L["Linked person"] - + @if (_editing) + { + @L["None (linked person)"] + } + @if (!_editing || + !_noLinkedPerson) + { + + } }
- @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/DocumentPerson.razor.cs b/Marechai/Pages/Admin/Details/DocumentPerson.razor.cs index 29b5f4ca..5e5d3a37 100644 --- a/Marechai/Pages/Admin/Details/DocumentPerson.razor.cs +++ b/Marechai/Pages/Admin/Details/DocumentPerson.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,16 @@ namespace Marechai.Pages.Admin.Details { public partial class DocumentPerson { - bool _editable; - bool _loaded; - Database.Models.DocumentPerson _model; - List _people; + bool _editing; + bool _loaded; + DocumentPersonViewModel _model; + bool _noLinkedPerson; + List _people; + bool _unknownAlias; + bool _unknownDisplayName; + bool _unknownName; + bool _unknownSurname; + [Parameter] public int Id { get; set; } @@ -27,7 +36,145 @@ namespace Marechai.Pages.Admin.Details _people = await PeopleService.GetAsync(); _model = await Service.GetAsync(Id); + _editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/document_people/edit/", StringComparison.InvariantCulture); + + if(_editing) + SetCheckboxes(); + StateHasChanged(); } + + void SetCheckboxes() + { + _unknownAlias = string.IsNullOrWhiteSpace(_model.Alias); + _noLinkedPerson = !_model.PersonId.HasValue; + _unknownDisplayName = string.IsNullOrWhiteSpace(_model.DisplayName); + _unknownName = string.IsNullOrWhiteSpace(_model.Name); + _unknownSurname = string.IsNullOrWhiteSpace(_model.Surname); + } + + void OnEditClicked() + { + _editing = true; + SetCheckboxes(); + StateHasChanged(); + } + + async void OnCancelClicked() + { + _editing = false; + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + async void OnSaveClicked() + { + if(_unknownAlias) + _model.Alias = null; + else if(string.IsNullOrWhiteSpace(_model.Alias)) + return; + + if(_noLinkedPerson) + _model.PersonId = null; + else if(_model.PersonId < 0) + return; + + if(_unknownAlias) + _model.Alias = null; + else if(string.IsNullOrWhiteSpace(_model.Alias)) + return; + + if(_unknownDisplayName) + _model.Alias = null; + else if(string.IsNullOrWhiteSpace(_model.Alias)) + return; + + if(_unknownName) + _model.Name = null; + else if(string.IsNullOrWhiteSpace(_model.Name)) + return; + + if(_unknownSurname) + _model.Surname = null; + else if(string.IsNullOrWhiteSpace(_model.Surname)) + return; + + if((_unknownName && !_unknownSurname) || + (!_unknownName && _unknownSurname)) + return; + + // TODO: Show error here + if(_unknownName && + _unknownSurname && + _unknownAlias && + _unknownDisplayName) + return; + + _editing = false; + await Service.UpdateAsync(_model); + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + void ValidateName(ValidatorEventArgs e) + { + if(!(e.Value is string name)) + { + e.Status = ValidationStatus.Error; + + return; + } + + if(name.Length < 1 || + name.Length > 256) + { + e.ErrorText = L["Name must be smaller than 256 characters."]; + e.Status = ValidationStatus.Error; + + return; + } + + if(!string.IsNullOrWhiteSpace(_model.Surname) && + !_unknownSurname) + return; + + e.ErrorText = L["Both name and surname must be known and filled, or both unknown."]; + e.Status = ValidationStatus.Error; + } + + void ValidateSurname(ValidatorEventArgs e) + { + if(!(e.Value is string surname)) + { + e.Status = ValidationStatus.Error; + + return; + } + + if(surname.Length < 1 || + surname.Length > 256) + { + e.ErrorText = L["Surname must be smaller than 256 characters."]; + e.Status = ValidationStatus.Error; + + return; + } + + if(!string.IsNullOrWhiteSpace(_model.Surname) && + !_unknownSurname) + return; + + e.ErrorText = L["Both name and surname must be known and filled, or both unknown."]; + e.Status = ValidationStatus.Error; + } + + void ValidateAlias(ValidatorEventArgs e) => + Validators.ValidateString(e, L["Alias must be smaller than 256 characters."], 256); + + void ValidateDisplayName(ValidatorEventArgs e) => + Validators.ValidateString(e, L["Display name must be smaller than 256 characters."], 256); } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/DocumentPeople.razor b/Marechai/Pages/Admin/DocumentPeople.razor index 2446f18f..855e5b7a 100644 --- a/Marechai/Pages/Admin/DocumentPeople.razor +++ b/Marechai/Pages/Admin/DocumentPeople.razor @@ -72,9 +72,7 @@ @L["Details"] - - @L["Edit"] - + @L["Edit"] diff --git a/Marechai/Resources/Services/DocumentPeopleService.en.resx b/Marechai/Resources/Services/DocumentPeopleService.en.resx new file mode 100644 index 00000000..105f9f52 --- /dev/null +++ b/Marechai/Resources/Services/DocumentPeopleService.en.resx @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 name + + + Unknown + Unknown, referring to one or more surnames + + + Unknown + Unknown, referring to an alias + + + Unknown + Unknown, referring to a display name + + + None + None, referring to a linked person + + \ No newline at end of file diff --git a/Marechai/Resources/Services/DocumentPeopleService.es.resx b/Marechai/Resources/Services/DocumentPeopleService.es.resx index e186a97e..eeef3352 100644 --- a/Marechai/Resources/Services/DocumentPeopleService.es.resx +++ b/Marechai/Resources/Services/DocumentPeopleService.es.resx @@ -182,4 +182,64 @@ Alias Alias + + Guardar + Save + + + Desconocido + Unknown, referring to a name + + + Desconocidos + Unknown, referring to one or more surnames + + + Desconocido + Unknown, referring to an alias + + + Desconocido + Unknown, referring to a display name + + + Ninguna + None, referring to a linked person + + + Por favor introduce un nombre válido. + Please enter a valid name. + + + Por favor introduce un apellido (o varios) válido. + Please enter a valid surname. + + + Por favor introduce un alias válido. + Please enter a valid alias. + + + Por favor introduce un nombre para mostrar válido. + Please enter a valid display name. + + + El nombre debe contener menos de 256 caracteres. + Name must be smaller than 256 characters. + + + Tanto el nombre como el/los apellido(s) deben rellenarse, or ser desconocidos. + Both name and surname must be known and filled, or both unknown. + + + El/los apellido(s) deben contener menos de 256 caracteres. + Surname must be smaller than 256 characters. + + + El alias debe contener menos de 256 caracteres. + Alias must be smaller than 256 characters. + + + El nombre para mostrar debe contener menos de 256 caracteres. + Display name must be smaller than 256 characters. + \ No newline at end of file diff --git a/Marechai/Services/DocumentPeopleService.cs b/Marechai/Services/DocumentPeopleService.cs index f0fbbd4b..93ac2a80 100644 --- a/Marechai/Services/DocumentPeopleService.cs +++ b/Marechai/Services/DocumentPeopleService.cs @@ -24,7 +24,28 @@ namespace Marechai.Services PersonId = d.PersonId }).ToListAsync(); - public async Task GetAsync(int id) => await _context.DocumentPeople.FindAsync(id); + public async Task GetAsync(int id) => + await _context.DocumentPeople.Where(p => p.Id == id).Select(d => new DocumentPersonViewModel + { + Id = d.Id, Alias = d.Alias, Name = d.Name, Surname = d.Surname, + DisplayName = d.DisplayName, PersonId = d.PersonId + }).FirstOrDefaultAsync(); + + public async Task UpdateAsync(DocumentPersonViewModel viewModel) + { + DocumentPerson model = await _context.DocumentPeople.FindAsync(viewModel.Id); + + if(model is null) + return; + + model.Alias = viewModel.Alias; + model.Name = viewModel.Name; + model.Surname = viewModel.Surname; + model.DisplayName = viewModel.DisplayName; + model.PersonId = viewModel.PersonId; + + await _context.SaveChangesAsync(); + } public async Task DeleteAsync(int id) { diff --git a/Marechai/ViewModels/DocumentPersonViewModel.cs b/Marechai/ViewModels/DocumentPersonViewModel.cs index d20fc495..8015d80a 100644 --- a/Marechai/ViewModels/DocumentPersonViewModel.cs +++ b/Marechai/ViewModels/DocumentPersonViewModel.cs @@ -7,5 +7,8 @@ namespace Marechai.ViewModels public string Name { get; set; } public string Person { get; set; } public int? PersonId { get; set; } + public string Alias { get; set; } + public string Surname { get; set; } + public string DisplayName { get; set; } } } \ No newline at end of file