diff --git a/Marechai/Pages/Admin/Details/Book.razor b/Marechai/Pages/Admin/Details/Book.razor index a611d1a3..c45a49d3 100644 --- a/Marechai/Pages/Admin/Details/Book.razor +++ b/Marechai/Pages/Admin/Details/Book.razor @@ -36,6 +36,8 @@ @inject DocumentCompaniesService CompaniesService @inject DocumentRolesService DocumentRolesService @inject CompaniesByBookService CompaniesByBookService +@inject MachineFamiliesService MachineFamiliesService +@inject BooksByMachineFamilyService BooksByMachineFamilyService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -239,8 +241,8 @@ } - - + + } @if (_bookCompanies?.Count > 0) @@ -269,7 +271,55 @@ @item.Role - + + + + } + + + + } + +
+

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

+ + @if (_addingMachineFamily) + { +
+ + @L["Family"] + + + + +
+ } + @if (_bookMachineFamilies?.Count > 0) + { +
+ + + + + + + + + @foreach (var item in _bookMachineFamilies) + { + + + } diff --git a/Marechai/Pages/Admin/Details/Book.razor.cs b/Marechai/Pages/Admin/Details/Book.razor.cs index 1f94f312..e79c86a3 100644 --- a/Marechai/Pages/Admin/Details/Book.razor.cs +++ b/Marechai/Pages/Admin/Details/Book.razor.cs @@ -38,31 +38,38 @@ namespace Marechai.Pages.Admin.Details { public partial class Book { - bool _addingCompany; - int? _addingCompanyId; - string _addingCompanyRoleId; - AuthenticationState _authState; - List _bookCompanies; - List _companies; - List _countries; - bool _creating; - CompanyByBookViewModel _currentCompanyByBook; - bool _deleteInProgress; - string _deleteText; - string _deleteTitle; - bool _deletingCompanyByBook; - bool _editing; - Modal _frmDelete; - bool _loaded; - BookViewModel _model; - List _roles; - bool _savingCompany; - bool _unknownCountry; - bool _unknownEdition; - bool _unknownIsbn; - bool _unknownNativeTitle; - bool _unknownPages; - bool _unknownPublished; + bool _addingCompany; + int? _addingCompanyId; + string _addingCompanyRoleId; + bool _addingMachineFamily; + int? _addingMachineFamilyId; + AuthenticationState _authState; + List _bookCompanies; + List _bookMachineFamilies; + List _companies; + List _countries; + bool _creating; + BookByMachineFamilyViewModel _currentBookByMachineFamily; + CompanyByBookViewModel _currentCompanyByBook; + bool _deleteInProgress; + string _deleteText; + string _deleteTitle; + bool _deletingBookByMachineFamily; + bool _deletingCompanyByBook; + bool _editing; + Modal _frmDelete; + bool _loaded; + List _machineFamilies; + BookViewModel _model; + List _roles; + bool _savingCompany; + bool _savingMachineFamily; + bool _unknownCountry; + bool _unknownEdition; + bool _unknownIsbn; + bool _unknownNativeTitle; + bool _unknownPages; + bool _unknownPublished; [Parameter] public long Id { get; set; } @@ -81,14 +88,16 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _countries = await CountriesService.GetAsync(); - _companies = await CompaniesService.GetAsync(); - _roles = await DocumentRolesService.GetEnabledAsync(); - _machineFamilies = await MachineFamiliesService.GetAsync(); - _model = _creating ? new BookViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - _addingCompanyRoleId = _roles.First().Id; - _bookCompanies = await CompaniesByBookService.GetByBook(Id); + _countries = await CountriesService.GetAsync(); + _companies = await CompaniesService.GetAsync(); + _roles = await DocumentRolesService.GetEnabledAsync(); + _machineFamilies = await MachineFamiliesService.GetAsync(); + _model = _creating ? new BookViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingCompanyRoleId = _roles.First().Id; + _bookCompanies = await CompaniesByBookService.GetByBook(Id); + _addingMachineFamilyId = _machineFamilies.First().Id; + _bookMachineFamilies = await BooksByMachineFamilyService.GetByBook(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/books/edit/", @@ -225,19 +234,19 @@ namespace Marechai.Pages.Admin.Details _addingCompanyId = _companies.First().Id; } - void CancelAddCpu() + void CancelAddCompany() { _addingCompany = false; _savingCompany = false; _addingCompanyId = null; } - async Task ConfirmAddCpu() + async Task ConfirmAddCompany() { if(_addingCompanyId is null || _addingCompanyId <= 0) { - CancelAddCpu(); + CancelAddCompany(); return; } @@ -263,7 +272,7 @@ namespace Marechai.Pages.Admin.Details StateHasChanged(); } - void ShowCpuDeleteModal(long itemId) + void ShowCompanyDeleteModal(long itemId) { _currentCompanyByBook = _bookCompanies.FirstOrDefault(n => n.Id == itemId); _deletingCompanyByBook = true; @@ -288,10 +297,12 @@ namespace Marechai.Pages.Admin.Details async void ConfirmDelete() { if(_deletingCompanyByBook) - await ConfirmDeleteCpuByMachine(); + await ConfirmDeleteCompanyByBook(); + else if(_deletingBookByMachineFamily) + await ConfirmDeleteBookByMachineFamily(); } - async Task ConfirmDeleteCpuByMachine() + async Task ConfirmDeleteCompanyByBook() { if(_currentCompanyByBook is null) return; @@ -315,5 +326,87 @@ namespace Marechai.Pages.Admin.Details // Tell we finished loading StateHasChanged(); } + + void OnAddFamilyClick() + { + _addingMachineFamily = true; + _savingMachineFamily = false; + _addingMachineFamilyId = _machineFamilies.First().Id; + } + + void CancelAddFamily() + { + _addingMachineFamily = false; + _savingMachineFamily = false; + _addingMachineFamilyId = null; + } + + async Task ConfirmAddFamily() + { + if(_addingMachineFamilyId is null || + _addingMachineFamilyId <= 0) + { + CancelAddFamily(); + + return; + } + + _savingMachineFamily = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await BooksByMachineFamilyService.CreateAsync(_addingMachineFamilyId.Value, Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookMachineFamilies = await BooksByMachineFamilyService.GetByBook(Id); + + _addingMachineFamily = false; + _savingMachineFamily = false; + _addingMachineFamilyId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowMachineFamilyDeleteModal(long itemId) + { + _currentBookByMachineFamily = _bookMachineFamilies.FirstOrDefault(n => n.Id == itemId); + _deletingBookByMachineFamily = true; + _deleteTitle = L["Delete machine family from this book"]; + + _deleteText = string.Format(L["Are you sure you want to delete the machine family {0} from this book?"], + _currentBookByMachineFamily?.MachineFamily); + + _frmDelete.Show(); + } + + async Task ConfirmDeleteBookByMachineFamily() + { + if(_currentBookByMachineFamily is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await BooksByMachineFamilyService.DeleteAsync(_currentBookByMachineFamily.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookMachineFamilies = await BooksByMachineFamilyService.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 73d6b26f..62e18950 100644 --- a/Marechai/Resources/Services/BooksService.en.resx +++ b/Marechai/Resources/Services/BooksService.en.resx @@ -137,4 +137,13 @@ Are you sure you want to delete the company {0} with role {1} from this book? + + Machine families this book talks about + + + Add new + + + Family + \ No newline at end of file diff --git a/Marechai/Resources/Services/BooksService.es.resx b/Marechai/Resources/Services/BooksService.es.resx index effd419e..08291a27 100644 --- a/Marechai/Resources/Services/BooksService.es.resx +++ b/Marechai/Resources/Services/BooksService.es.resx @@ -253,4 +253,13 @@ ¿Estás seguro de eliminar la compañía {0} con el rol {1} de este libro? + + Familias de máquinas sobre las que habla este libro + + + Añadir nueva + + + Familia + \ No newline at end of file diff --git a/Marechai/Services/BooksByMachineFamilyService.cs b/Marechai/Services/BooksByMachineFamilyService.cs new file mode 100644 index 00000000..13e639c3 --- /dev/null +++ b/Marechai/Services/BooksByMachineFamilyService.cs @@ -0,0 +1,77 @@ +/****************************************************************************** +// 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 BooksByMachineFamilyService + { + readonly MarechaiContext _context; + + public BooksByMachineFamilyService(MarechaiContext context) => _context = context; + + public async Task> GetByBook(long bookId) => + await _context.BooksByMachineFamilies.Where(p => p.BookId == bookId). + Select(p => new BookByMachineFamilyViewModel + { + Id = p.Id, + BookId = p.BookId, + MachineFamilyId = p.MachineFamilyId, + MachineFamily = p.MachineFamily.Name + }).OrderBy(p => p.MachineFamily).ThenBy(p => p.Book).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + BooksByMachineFamily item = await _context.BooksByMachineFamilies.FindAsync(id); + + if(item is null) + return; + + _context.BooksByMachineFamilies.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int machineFamilyId, long bookId, string userId) + { + var item = new BooksByMachineFamily + { + MachineFamilyId = machineFamilyId, + BookId = bookId + }; + + await _context.BooksByMachineFamilies.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 3f553794..e71808b0 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -85,6 +85,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/BookByMachineFamilyViewModel.cs b/Marechai/ViewModels/BookByMachineFamilyViewModel.cs new file mode 100644 index 00000000..8627c1f0 --- /dev/null +++ b/Marechai/ViewModels/BookByMachineFamilyViewModel.cs @@ -0,0 +1,35 @@ +/****************************************************************************** +// 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 BookByMachineFamilyViewModel : BaseViewModel + { + public long BookId { get; set; } + public string Book { get; set; } + public int MachineFamilyId { get; set; } + public string MachineFamily { get; set; } + } +} \ No newline at end of file
+ @L["Family"] +
+ @item.MachineFamily + +