From b9cf22e7a1ce922b54014949d959dfcfc8918f32 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 10 Aug 2020 01:21:43 +0100 Subject: [PATCH] Add machines to book admin page. --- Marechai/Pages/Admin/Details/Book.razor | 50 ++++++++++ Marechai/Pages/Admin/Details/Book.razor.cs | 98 ++++++++++++++++++- .../Resources/Services/BooksService.en.resx | 21 ++++ .../Resources/Services/BooksService.es.resx | 21 ++++ Marechai/Services/BooksByMachineService.cs | 76 ++++++++++++++ Marechai/Services/Register.cs | 1 + Marechai/ViewModels/BookByMachineViewModel.cs | 35 +++++++ 7 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 Marechai/Services/BooksByMachineService.cs create mode 100644 Marechai/ViewModels/BookByMachineViewModel.cs diff --git a/Marechai/Pages/Admin/Details/Book.razor b/Marechai/Pages/Admin/Details/Book.razor index c45a49d3..3076b838 100644 --- a/Marechai/Pages/Admin/Details/Book.razor +++ b/Marechai/Pages/Admin/Details/Book.razor @@ -38,6 +38,8 @@ @inject CompaniesByBookService CompaniesByBookService @inject MachineFamiliesService MachineFamiliesService @inject BooksByMachineFamilyService BooksByMachineFamilyService +@inject MachinesService MachinesService +@inject BooksByMachineService BooksByMachineService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -328,6 +330,54 @@ } +
+

@L["Machines this book talks about"]

+ + @if (_addingMachine) + { +
+ + @L[""] + + + + +
+ } + @if (_bookMachines?.Count > 0) + { +
+ + + + + + + + + @foreach (var item in _bookMachines) + { + + + + + } + +
+ @L["Machine"] +
+ @item.Machine + + +
+
+ } + diff --git a/Marechai/Pages/Admin/Details/Book.razor.cs b/Marechai/Pages/Admin/Details/Book.razor.cs index 60828c47..8110cd8e 100644 --- a/Marechai/Pages/Admin/Details/Book.razor.cs +++ b/Marechai/Pages/Admin/Details/Book.razor.cs @@ -41,28 +41,35 @@ namespace Marechai.Pages.Admin.Details bool _addingCompany; int? _addingCompanyId; string _addingCompanyRoleId; + bool _addingMachine; bool _addingMachineFamily; int? _addingMachineFamilyId; + int? _addingMachineId; AuthenticationState _authState; List _bookCompanies; List _bookMachineFamilies; + List _bookMachines; List _companies; List _countries; bool _creating; + BookByMachineViewModel _currentBookByMachine; BookByMachineFamilyViewModel _currentBookByMachineFamily; CompanyByBookViewModel _currentCompanyByBook; bool _deleteInProgress; string _deleteText; string _deleteTitle; + bool _deletingBookByMachine; bool _deletingBookByMachineFamily; bool _deletingCompanyByBook; bool _editing; Modal _frmDelete; bool _loaded; List _machineFamilies; + List _machines; BookViewModel _model; List _roles; bool _savingCompany; + bool _savingMachine; bool _savingMachineFamily; bool _unknownCountry; bool _unknownEdition; @@ -92,12 +99,15 @@ namespace Marechai.Pages.Admin.Details _companies = await CompaniesService.GetAsync(); _roles = await DocumentRolesService.GetEnabledAsync(); _machineFamilies = await MachineFamiliesService.GetAsync(); + _machines = await MachinesService.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); + _addingMachineId = _machines.First().Id; + _bookMachines = await BooksByMachineService.GetByBook(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/books/edit/", @@ -291,7 +301,9 @@ namespace Marechai.Pages.Admin.Details _deletingCompanyByBook = false; _currentCompanyByBook = null; _deletingBookByMachineFamily = false; - _currentCompanyByBook = null; + _currentBookByMachineFamily = null; + _deletingBookByMachine = false; + _currentBookByMachine = null; } void HideModal() => _frmDelete.Hide(); @@ -302,6 +314,8 @@ namespace Marechai.Pages.Admin.Details await ConfirmDeleteCompanyByBook(); else if(_deletingBookByMachineFamily) await ConfirmDeleteBookByMachineFamily(); + else if(_deletingBookByMachine) + await ConfirmDeleteBookByMachine(); } async Task ConfirmDeleteCompanyByBook() @@ -410,5 +424,87 @@ namespace Marechai.Pages.Admin.Details // Tell we finished loading StateHasChanged(); } + + void OnAddMachineClick() + { + _addingMachine = true; + _savingMachine = false; + _addingMachineId = _machines.First().Id; + } + + void CancelAddMachine() + { + _addingMachine = false; + _savingMachine = false; + _addingMachineId = null; + } + + async Task ConfirmAddMachine() + { + if(_addingMachineId is null || + _addingMachineId <= 0) + { + CancelAddMachine(); + + return; + } + + _savingMachine = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await BooksByMachineService.CreateAsync(_addingMachineId.Value, Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookMachines = await BooksByMachineService.GetByBook(Id); + + _addingMachine = false; + _savingMachine = false; + _addingMachineId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowMachineDeleteModal(long itemId) + { + _currentBookByMachine = _bookMachines.FirstOrDefault(n => n.Id == itemId); + _deletingBookByMachine = true; + _deleteTitle = L["Delete machine from this book"]; + + _deleteText = string.Format(L["Are you sure you want to delete the machine {0} from this book?"], + _currentBookByMachine?.Machine); + + _frmDelete.Show(); + } + + async Task ConfirmDeleteBookByMachine() + { + if(_currentBookByMachine is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await BooksByMachineService.DeleteAsync(_currentBookByMachine.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookMachines = await BooksByMachineService.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 62e18950..01f4f100 100644 --- a/Marechai/Resources/Services/BooksService.en.resx +++ b/Marechai/Resources/Services/BooksService.en.resx @@ -146,4 +146,25 @@ Family + + Delete machine family from this book + + + Are you sure you want to delete the machine family {0} from this book? + + + Delete machine from this book + + + Are you sure you want to delete the machine {0} from this book? + + + Machine + + + Add new + + + Machines this book talks about + \ No newline at end of file diff --git a/Marechai/Resources/Services/BooksService.es.resx b/Marechai/Resources/Services/BooksService.es.resx index 08291a27..6f20173d 100644 --- a/Marechai/Resources/Services/BooksService.es.resx +++ b/Marechai/Resources/Services/BooksService.es.resx @@ -262,4 +262,25 @@ Familia + + Eliminar familia de máquinas de este libro + + + ¿Estás seguro de eliminar la familia de máquinas {0} de este libro? + + + Eliminar máquina de este libro + + + ¿Estás seguro de eliminar la máquina {0} de este libro? + + + Máquina + + + Añadir nueva + + + Máquinas sobre las que habla este libro + \ No newline at end of file diff --git a/Marechai/Services/BooksByMachineService.cs b/Marechai/Services/BooksByMachineService.cs new file mode 100644 index 00000000..8aa945cf --- /dev/null +++ b/Marechai/Services/BooksByMachineService.cs @@ -0,0 +1,76 @@ +/****************************************************************************** +// 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 BooksByMachineService + { + readonly MarechaiContext _context; + + public BooksByMachineService(MarechaiContext context) => _context = context; + + public async Task> GetByBook(long bookId) => + await _context.BooksByMachines.Where(p => p.BookId == bookId).Select(p => new BookByMachineViewModel + { + Id = p.Id, + BookId = p.BookId, + MachineId = p.MachineId, + Machine = p.Machine.Name + }).OrderBy(p => p.Machine).ThenBy(p => p.Book).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + BooksByMachine item = await _context.BooksByMachines.FindAsync(id); + + if(item is null) + return; + + _context.BooksByMachines.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int machineId, long bookId, string userId) + { + var item = new BooksByMachine + { + MachineId = machineId, + BookId = bookId + }; + + await _context.BooksByMachines.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 f3d210db..857a5862 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -88,6 +88,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/BookByMachineViewModel.cs b/Marechai/ViewModels/BookByMachineViewModel.cs new file mode 100644 index 00000000..fb258d85 --- /dev/null +++ b/Marechai/ViewModels/BookByMachineViewModel.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 BookByMachineViewModel : BaseViewModel + { + public long BookId { get; set; } + public string Book { get; set; } + public int MachineId { get; set; } + public string Machine { get; set; } + } +} \ No newline at end of file