From 9930ccaf0e9c5fe82d585b6c6be8dae56122e738 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 10 Aug 2020 03:16:29 +0100 Subject: [PATCH] Add people to book admin page. --- Marechai/Pages/Admin/Details/Book.razor | 67 ++++++++++- Marechai/Pages/Admin/Details/Book.razor.cs | 112 ++++++++++++++++-- .../Resources/Services/BooksService.en.resx | 15 +++ .../Resources/Services/BooksService.es.resx | 15 +++ Marechai/Services/PeopleByBookService.cs | 82 +++++++++++++ Marechai/Services/Register.cs | 1 + .../ViewModels/DocumentPersonViewModel.cs | 2 + Marechai/ViewModels/PersonByBookViewModel.cs | 41 +++++++ 8 files changed, 327 insertions(+), 8 deletions(-) create mode 100644 Marechai/Services/PeopleByBookService.cs create mode 100644 Marechai/ViewModels/PersonByBookViewModel.cs diff --git a/Marechai/Pages/Admin/Details/Book.razor b/Marechai/Pages/Admin/Details/Book.razor index 3076b838..3a9e611c 100644 --- a/Marechai/Pages/Admin/Details/Book.razor +++ b/Marechai/Pages/Admin/Details/Book.razor @@ -40,6 +40,8 @@ @inject BooksByMachineFamilyService BooksByMachineFamilyService @inject MachinesService MachinesService @inject BooksByMachineService BooksByMachineService +@inject DocumentPeopleService PeopleService +@inject PeopleByBookService PeopleByBookService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -282,6 +284,69 @@ } +
+

@L["People involved in this book"]

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

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

@@ -377,7 +442,7 @@ } - + diff --git a/Marechai/Pages/Admin/Details/Book.razor.cs b/Marechai/Pages/Admin/Details/Book.razor.cs index 8110cd8e..405856ef 100644 --- a/Marechai/Pages/Admin/Details/Book.razor.cs +++ b/Marechai/Pages/Admin/Details/Book.razor.cs @@ -38,39 +38,48 @@ namespace Marechai.Pages.Admin.Details { public partial class Book { - 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 _bookCompanies; List _bookMachineFamilies; List _bookMachines; + List _bookPeople; List _companies; List _countries; bool _creating; BookByMachineViewModel _currentBookByMachine; BookByMachineFamilyViewModel _currentBookByMachineFamily; CompanyByBookViewModel _currentCompanyByBook; + PersonByBookViewModel _currentPersonByBook; bool _deleteInProgress; string _deleteText; string _deleteTitle; bool _deletingBookByMachine; bool _deletingBookByMachineFamily; bool _deletingCompanyByBook; + bool _deletingPersonByBook; bool _editing; Modal _frmDelete; bool _loaded; List _machineFamilies; List _machines; BookViewModel _model; + List _people; List _roles; bool _savingCompany; bool _savingMachine; bool _savingMachineFamily; + bool _savingPerson; bool _unknownCountry; bool _unknownEdition; bool _unknownIsbn; @@ -108,6 +117,8 @@ namespace Marechai.Pages.Admin.Details _bookMachineFamilies = await BooksByMachineFamilyService.GetByBook(Id); _addingMachineId = _machines.First().Id; _bookMachines = await BooksByMachineService.GetByBook(Id); + _addingPersonRoleId = _roles.First().Id; + _bookPeople = await PeopleByBookService.GetByBook(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/books/edit/", @@ -304,6 +315,8 @@ namespace Marechai.Pages.Admin.Details _currentBookByMachineFamily = null; _deletingBookByMachine = false; _currentBookByMachine = null; + _deletingPersonByBook = false; + _currentPersonByBook = null; } void HideModal() => _frmDelete.Hide(); @@ -316,6 +329,8 @@ namespace Marechai.Pages.Admin.Details await ConfirmDeleteBookByMachineFamily(); else if(_deletingBookByMachine) await ConfirmDeleteBookByMachine(); + else if(_deletingPersonByBook) + await ConfirmDeletePersonByBook(); } async Task ConfirmDeleteCompanyByBook() @@ -506,5 +521,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 PeopleByBookService.CreateAsync(_addingPersonId.Value, Id, _addingPersonRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookPeople = await PeopleByBookService.GetByBook(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) + { + _currentPersonByBook = _bookPeople.FirstOrDefault(n => n.Id == itemId); + _deletingPersonByBook = true; + _deleteTitle = L["Delete person from this book"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the person {0} with role {1} from this book?"], + _currentPersonByBook?.FullName, _currentPersonByBook?.Role); + + _frmDelete.Show(); + } + + async Task ConfirmDeletePersonByBook() + { + if(_currentPersonByBook is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await PeopleByBookService.DeleteAsync(_currentPersonByBook.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookPeople = await PeopleByBookService.GetByBook(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/BooksService.en.resx b/Marechai/Resources/Services/BooksService.en.resx index 01f4f100..cf15c3af 100644 --- a/Marechai/Resources/Services/BooksService.en.resx +++ b/Marechai/Resources/Services/BooksService.en.resx @@ -167,4 +167,19 @@ Machines this book talks about + + People involved in this book + + + Add new + + + Person + + + Delete person from this book + + + Are you sure you want to delete the person {0} with role {1} from this book? + \ No newline at end of file diff --git a/Marechai/Resources/Services/BooksService.es.resx b/Marechai/Resources/Services/BooksService.es.resx index 6f20173d..6c4cfe79 100644 --- a/Marechai/Resources/Services/BooksService.es.resx +++ b/Marechai/Resources/Services/BooksService.es.resx @@ -283,4 +283,19 @@ Máquinas sobre las que habla este libro + + Personas involucradas en este libro + + + Añadir nueva + + + Persona + + + Eliminar persona de este libro + + + ¿Estás seguro de eliminar la persona {0} con el rol {1} de este libro? + \ No newline at end of file diff --git a/Marechai/Services/PeopleByBookService.cs b/Marechai/Services/PeopleByBookService.cs new file mode 100644 index 00000000..531e99d1 --- /dev/null +++ b/Marechai/Services/PeopleByBookService.cs @@ -0,0 +1,82 @@ +/****************************************************************************** +// 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 PeopleByBookService + { + readonly MarechaiContext _context; + + public PeopleByBookService(MarechaiContext context) => _context = context; + + public async Task> GetByBook(long bookId) => + (await _context.PeopleByBooks.Where(p => p.BookId == bookId).Select(p => new PersonByBookViewModel + { + 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, + BookId = p.BookId + }).ToListAsync()).OrderBy(p => p.FullName).ThenBy(p => p.Role).ToList(); + + public async Task DeleteAsync(long id, string userId) + { + PeopleByBook item = await _context.PeopleByBooks.FindAsync(id); + + if(item is null) + return; + + _context.PeopleByBooks.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int personId, long bookId, string roleId, string userId) + { + var item = new PeopleByBook + { + PersonId = personId, + BookId = bookId, + RoleId = roleId + }; + + await _context.PeopleByBooks.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 20a3f626..d945b3b8 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -91,6 +91,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/DocumentPersonViewModel.cs b/Marechai/ViewModels/DocumentPersonViewModel.cs index 22b57abc..b41b5654 100644 --- a/Marechai/ViewModels/DocumentPersonViewModel.cs +++ b/Marechai/ViewModels/DocumentPersonViewModel.cs @@ -33,5 +33,7 @@ namespace Marechai.ViewModels 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 diff --git a/Marechai/ViewModels/PersonByBookViewModel.cs b/Marechai/ViewModels/PersonByBookViewModel.cs new file mode 100644 index 00000000..0ef0a7bf --- /dev/null +++ b/Marechai/ViewModels/PersonByBookViewModel.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 PersonByBookViewModel : BaseViewModel + { + public int PersonId { get; set; } + public long BookId { 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