diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index dd3b3e58..be071379 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 4.0.0.1821 + 4.0.0.1822 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Admin/Details/Document.razor b/Marechai/Pages/Admin/Details/Document.razor index e08f3c5f..37a2b865 100644 --- a/Marechai/Pages/Admin/Details/Document.razor +++ b/Marechai/Pages/Admin/Details/Document.razor @@ -40,6 +40,8 @@ @inject DocumentsByMachineFamilyService DocumentsByMachineFamilyService @inject MachinesService MachinesService @inject DocumentsByMachineService DocumentsByMachineService +@inject DocumentPeopleService PeopleService +@inject PeopleByDocumentService PeopleByDocumentService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -208,6 +210,69 @@ } +
+

@L["People involved in this document"]

+ + @if (_addingPerson) + { +
+ + @L["Person"] + + + + @L["Role"] + + + + +
+ } + @if (_documentPeople?.Count > 0) + { +
+ + + + + + + + + + @foreach (var item in _documentPeople) + { + + + + + + } + +
+ @L["Person"] + + @L["Role"] +
+ @item.FullName + + @item.Role + + +
+
+ } +

@L["Machine families this document talks about"]

diff --git a/Marechai/Pages/Admin/Details/Document.razor.cs b/Marechai/Pages/Admin/Details/Document.razor.cs index cf5235d5..f4b611ba 100644 --- a/Marechai/Pages/Admin/Details/Document.razor.cs +++ b/Marechai/Pages/Admin/Details/Document.razor.cs @@ -38,13 +38,17 @@ namespace Marechai.Pages.Admin.Details { public partial class Document { - bool _addingCompany; - int? _addingCompanyId; - string _addingCompanyRoleId; - bool _addingMachine; - bool _addingMachineFamily; - int? _addingMachineFamilyId; - int? _addingMachineId; + bool _addingCompany; + int? _addingCompanyId; + string _addingCompanyRoleId; + bool _addingMachine; + bool _addingMachineFamily; + int? _addingMachineFamilyId; + int? _addingMachineId; + + bool _addingPerson; + int? _addingPersonId; + string _addingPersonRoleId; AuthenticationState _authState; List _companies; List _countries; @@ -52,25 +56,30 @@ namespace Marechai.Pages.Admin.Details CompanyByDocumentViewModel _currentCompanyByDocument; DocumentByMachineViewModel _currentDocumentByMachine; DocumentByMachineFamilyViewModel _currentDocumentByMachineFamily; + PersonByDocumentViewModel _currentPersonByDocument; bool _deleteInProgress; string _deleteText; string _deleteTitle; bool _deletingCompanyByDocument; bool _deletingDocumentByMachine; bool _deletingDocumentByMachineFamily; + bool _deletingPersonByDocument; List _documentCompanies; List _documentMachineFamilies; List _documentMachines; + List _documentPeople; bool _editing; Modal _frmDelete; bool _loaded; List _machineFamilies; List _machines; DocumentViewModel _model; + List _people; List _roles; bool _savingCompany; bool _savingMachine; bool _savingMachineFamily; + bool _savingPerson; bool _unknownCountry; bool _unknownNativeTitle; bool _unknownPublished; @@ -105,6 +114,8 @@ namespace Marechai.Pages.Admin.Details _documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(Id); _addingMachineId = _machines.First().Id; _documentMachines = await DocumentsByMachineService.GetByDocument(Id); + _addingPersonRoleId = _roles.First().Id; + _documentPeople = await PeopleByDocumentService.GetByDocument(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/documents/edit/", @@ -253,6 +264,8 @@ namespace Marechai.Pages.Admin.Details _currentDocumentByMachineFamily = null; _deletingDocumentByMachine = false; _currentDocumentByMachine = null; + _deletingPersonByDocument = false; + _currentPersonByDocument = null; } void HideModal() => _frmDelete.Hide(); @@ -265,6 +278,8 @@ namespace Marechai.Pages.Admin.Details await ConfirmDeleteDocumentByMachineFamily(); else if(_deletingDocumentByMachine) await ConfirmDeleteDocumentByMachine(); + else if(_deletingPersonByDocument) + await ConfirmDeletePersonByDocument(); } async Task ConfirmDeleteCompanyByMachine() @@ -455,5 +470,88 @@ namespace Marechai.Pages.Admin.Details // Tell we finished loading StateHasChanged(); } + + void OnAddPersonClick() + { + _addingPerson = true; + _savingPerson = false; + _addingPersonId = _people.First().Id; + } + + void CancelAddPerson() + { + _addingPerson = false; + _savingPerson = false; + _addingPersonId = null; + } + + async Task ConfirmAddPerson() + { + if(_addingPersonId is null || + _addingPersonId <= 0) + { + CancelAddPerson(); + + return; + } + + _savingPerson = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await PeopleByDocumentService.CreateAsync(_addingPersonId.Value, Id, _addingPersonRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _documentPeople = await PeopleByDocumentService.GetByDocument(Id); + + _addingPerson = false; + _savingPerson = false; + _addingPersonId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowPersonDeleteModal(long itemId) + { + _currentPersonByDocument = _documentPeople.FirstOrDefault(n => n.Id == itemId); + _deletingPersonByDocument = true; + _deleteTitle = L["Delete person from this document"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the person {0} with role {1} from this document?"], + _currentPersonByDocument?.FullName, _currentPersonByDocument?.Role); + + _frmDelete.Show(); + } + + async Task ConfirmDeletePersonByDocument() + { + if(_currentPersonByDocument is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await PeopleByDocumentService.DeleteAsync(_currentPersonByDocument.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _documentPeople = await PeopleByDocumentService.GetByDocument(Id); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } } } \ No newline at end of file diff --git a/Marechai/Resources/Services/DocumentsService.en.resx b/Marechai/Resources/Services/DocumentsService.en.resx index 999dbe02..2bd15b1c 100644 --- a/Marechai/Resources/Services/DocumentsService.en.resx +++ b/Marechai/Resources/Services/DocumentsService.en.resx @@ -143,4 +143,19 @@ Machines this document talks about + + People involved in this document + + + Add new + + + Person + + + Delete person from this document + + + Are you sure you want to delete the person {0} with role {1} from this document? + \ No newline at end of file diff --git a/Marechai/Resources/Services/DocumentsService.es.resx b/Marechai/Resources/Services/DocumentsService.es.resx index 8a797a82..f4f3893a 100644 --- a/Marechai/Resources/Services/DocumentsService.es.resx +++ b/Marechai/Resources/Services/DocumentsService.es.resx @@ -259,4 +259,19 @@ Máquinas sobre las que habla este documento + + Añadir nueva + + + ¿Estás seguro de eliminar la persona {0} con el rol {1} de este documento? + + + Eliminar persona de este documento + + + Personas involucradas en este documento + + + Persona + \ No newline at end of file diff --git a/Marechai/Services/PeopleByDocumentService.cs b/Marechai/Services/PeopleByDocumentService.cs new file mode 100644 index 00000000..15bc4a31 --- /dev/null +++ b/Marechai/Services/PeopleByDocumentService.cs @@ -0,0 +1,83 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ 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 System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Marechai.Database.Models; +using Marechai.ViewModels; +using Microsoft.EntityFrameworkCore; + +namespace Marechai.Services +{ + public class PeopleByDocumentService + { + readonly MarechaiContext _context; + + public PeopleByDocumentService(MarechaiContext context) => _context = context; + + public async Task> GetByDocument(long documentId) => + (await _context.PeopleByDocuments.Where(p => p.DocumentId == documentId). + Select(p => new PersonByDocumentViewModel + { + Id = p.Id, + Name = p.Person.Name, + Surname = p.Person.Surname, + Alias = p.Person.Alias, + DisplayName = p.Person.DisplayName, + PersonId = p.PersonId, + RoleId = p.RoleId, + Role = p.Role.Name, + DocumentId = p.DocumentId + }).ToListAsync()).OrderBy(p => p.FullName).ThenBy(p => p.Role).ToList(); + + public async Task DeleteAsync(long id, string userId) + { + PeopleByDocument item = await _context.PeopleByDocuments.FindAsync(id); + + if(item is null) + return; + + _context.PeopleByDocuments.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int personId, long documentId, string roleId, string userId) + { + var item = new PeopleByDocument + { + PersonId = personId, + DocumentId = documentId, + RoleId = roleId + }; + + await _context.PeopleByDocuments.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/Services/Register.cs b/Marechai/Services/Register.cs index d945b3b8..79d674d7 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -92,6 +92,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/PersonByDocumentViewModel.cs b/Marechai/ViewModels/PersonByDocumentViewModel.cs new file mode 100644 index 00000000..6d90b071 --- /dev/null +++ b/Marechai/ViewModels/PersonByDocumentViewModel.cs @@ -0,0 +1,41 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ 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 +*******************************************************************************/ + +namespace Marechai.ViewModels +{ + public class PersonByDocumentViewModel : BaseViewModel + { + public int PersonId { get; set; } + public long DocumentId { get; set; } + public string RoleId { get; set; } + public string Role { get; set; } + public string Name { get; set; } + public string Alias { get; set; } + public string Surname { get; set; } + public string DisplayName { get; set; } + + public string FullName => DisplayName ?? Alias ?? $"{Name} {Surname}"; + } +} \ No newline at end of file