From eadedfde30c585b6a82ae891a8365b3240929969 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 9 Aug 2020 17:03:42 +0100 Subject: [PATCH] Add companies to book admin page. --- Marechai/Pages/Admin/Details/Book.razor | 86 +++++++++++ Marechai/Pages/Admin/Details/Book.razor.cs | 145 ++++++++++++++++-- .../Resources/Services/BooksService.en.resx | 21 +++ .../Resources/Services/BooksService.es.resx | 23 ++- Marechai/Services/CompaniesByBookService.cs | 79 ++++++++++ Marechai/Services/DocumentRolesService.cs | 67 ++++++++ Marechai/ViewModels/CompanyByBookViewModel.cs | 36 +++++ Marechai/ViewModels/DocumentRoleViewModel.cs | 33 ++++ 8 files changed, 474 insertions(+), 16 deletions(-) create mode 100644 Marechai/Services/CompaniesByBookService.cs create mode 100644 Marechai/Services/DocumentRolesService.cs create mode 100644 Marechai/ViewModels/CompanyByBookViewModel.cs create mode 100644 Marechai/ViewModels/DocumentRoleViewModel.cs diff --git a/Marechai/Pages/Admin/Details/Book.razor b/Marechai/Pages/Admin/Details/Book.razor index 9d696847..a611d1a3 100644 --- a/Marechai/Pages/Admin/Details/Book.razor +++ b/Marechai/Pages/Admin/Details/Book.razor @@ -33,6 +33,9 @@ @inherits OwningComponentBase @inject IStringLocalizer L @inject Iso31661NumericService CountriesService +@inject DocumentCompaniesService CompaniesService +@inject DocumentRolesService DocumentRolesService +@inject CompaniesByBookService CompaniesByBookService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -209,3 +212,86 @@ @{ // TODO: Book synopsis } + +@if (!_editing) +{ +
+

@L["Companies involved in this book"]

+ + @if (_addingCompany) + { +
+ + @L["Company"] + + + + @L["Role"] + + + + +
+ } + @if (_bookCompanies?.Count > 0) + { +
+ + + + + + + + + + @foreach (var item in _bookCompanies) + { + + + + + + } + +
+ @L["Company"] + + @L["Role"] +
+ @item.Company + + @item.Role + + +
+
+ } + + + + + + @_deleteTitle + + + + @_deleteText + + + + + + + +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Book.razor.cs b/Marechai/Pages/Admin/Details/Book.razor.cs index d3be0611..bf2e6625 100644 --- a/Marechai/Pages/Admin/Details/Book.razor.cs +++ b/Marechai/Pages/Admin/Details/Book.razor.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Blazorise; using Marechai.Database.Models; @@ -37,18 +38,31 @@ namespace Marechai.Pages.Admin.Details { public partial class Book { - AuthenticationState _authState; - List _countries; - bool _creating; - bool _editing; - bool _loaded; - BookViewModel _model; - bool _unknownCountry; - bool _unknownEdition; - bool _unknownIsbn; - bool _unknownNativeTitle; - bool _unknownPages; - bool _unknownPublished; + 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; [Parameter] public long Id { get; set; } @@ -67,9 +81,12 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _countries = await CountriesService.GetAsync(); - _model = _creating ? new BookViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _countries = await CountriesService.GetAsync(); + _companies = await CompaniesService.GetAsync(); + _roles = await DocumentRolesService.GetEnabledAsync(); + _model = _creating ? new BookViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingCompanyRoleId = _roles.First().Id; _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/books/edit/", @@ -198,5 +215,103 @@ namespace Marechai.Pages.Admin.Details void ValidateEdition(ValidatorEventArgs e) => Validators.ValidateInteger(e, 1); void ValidateIsbn(ValidatorEventArgs e) => Validators.ValidateIsbn(e); + + void OnAddCompanyClick() + { + _addingCompany = true; + _savingCompany = false; + _addingCompanyId = _companies.First().Id; + } + + void CancelAddCpu() + { + _addingCompany = false; + _savingCompany = false; + _addingCompanyId = null; + } + + async Task ConfirmAddCpu() + { + if(_addingCompanyId is null || + _addingCompanyId <= 0) + { + CancelAddCpu(); + + return; + } + + _savingCompany = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesByBookService.CreateAsync(_addingCompanyId.Value, Id, _addingCompanyRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookCompanies = await CompaniesByBookService.GetByBook(Id); + + _addingCompany = false; + _savingCompany = false; + _addingCompanyId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowCpuDeleteModal(long itemId) + { + _currentCompanyByBook = _bookCompanies.FirstOrDefault(n => n.Id == itemId); + _deletingCompanyByBook = true; + _deleteTitle = L["Delete company from this book"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the company {0} with role {1} from this book?"], + _currentCompanyByBook?.Company, _currentCompanyByBook?.Role); + + _frmDelete.Show(); + } + + void ModalClosing(ModalClosingEventArgs obj) + { + _deleteInProgress = false; + _deletingCompanyByBook = false; + _currentCompanyByBook = null; + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_deletingCompanyByBook) + await ConfirmDeleteCpuByMachine(); + } + + async Task ConfirmDeleteCpuByMachine() + { + if(_currentCompanyByBook is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesByBookService.DeleteAsync(_currentCompanyByBook.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _bookCompanies = await CompaniesByBookService.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 91128741..73d6b26f 100644 --- a/Marechai/Resources/Services/BooksService.en.resx +++ b/Marechai/Resources/Services/BooksService.en.resx @@ -116,4 +116,25 @@ Unknown + + Companies involved in this book + + + Add new + + + Company + + + Role + + + Add + + + Delete company from this book + + + Are you sure you want to delete the company {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 32e3be0d..effd419e 100644 --- a/Marechai/Resources/Services/BooksService.es.resx +++ b/Marechai/Resources/Services/BooksService.es.resx @@ -147,7 +147,7 @@ Delete book - ¿Está seguro de que desea borrar el libro {0}? + ¿Estás seguro de borrar el libro {0}? {0} book name @@ -232,4 +232,25 @@ El título nativo debe contener menos de 256 caracteres. + + Compañías involucradas en este libro + + + Añadir nueva + + + Compañía + + + Rol + + + Añadir + + + Eliminar compañía de este libro + + + ¿Estás seguro de eliminar la compañía {0} con el rol {1} de este libro? + \ No newline at end of file diff --git a/Marechai/Services/CompaniesByBookService.cs b/Marechai/Services/CompaniesByBookService.cs new file mode 100644 index 00000000..b1e1da1b --- /dev/null +++ b/Marechai/Services/CompaniesByBookService.cs @@ -0,0 +1,79 @@ +/****************************************************************************** +// 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 CompaniesByBookService + { + readonly MarechaiContext _context; + + public CompaniesByBookService(MarechaiContext context) => _context = context; + + public async Task> GetByBook(long bookId) => + await _context.CompaniesByBooks.Where(p => p.BookId == bookId).Select(p => new CompanyByBookViewModel + { + Id = p.Id, + Company = p.Company.Name, + CompanyId = p.CompanyId, + RoleId = p.RoleId, + Role = p.Role.Name, + BookId = p.BookId + }).OrderBy(p => p.Company).ThenBy(p => p.Role).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + CompaniesByBook item = await _context.CompaniesByBooks.FindAsync(id); + + if(item is null) + return; + + _context.CompaniesByBooks.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int companyId, long bookId, string roleId, string userId) + { + var item = new CompaniesByBook + { + CompanyId = companyId, + BookId = bookId, + RoleId = roleId + }; + + await _context.CompaniesByBooks.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/Services/DocumentRolesService.cs b/Marechai/Services/DocumentRolesService.cs new file mode 100644 index 00000000..0c9a02bd --- /dev/null +++ b/Marechai/Services/DocumentRolesService.cs @@ -0,0 +1,67 @@ +/****************************************************************************** +// 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 DocumentRolesService + { + readonly MarechaiContext _context; + + public DocumentRolesService(MarechaiContext context) => _context = context; + + public async Task> GetAsync() => await _context.DocumentRoles. + OrderBy(c => c.Name). + Select(c => new DocumentRoleViewModel + { + Id = c.Id, + Name = c.Name, + Enabled = c.Enabled + }).ToListAsync(); + + public async Task> GetEnabledAsync() => + await _context.DocumentRoles.Where(c => c.Enabled).OrderBy(c => c.Name). + Select(c => new DocumentRoleViewModel + { + Id = c.Id, + Name = c.Name, + Enabled = c.Enabled + }).ToListAsync(); + + public async Task GetAsync(string id) => + await _context.DocumentRoles.Where(c => c.Id == id).Select(c => new DocumentRoleViewModel + { + Id = c.Id, + Name = c.Name, + Enabled = c.Enabled + }).FirstOrDefaultAsync(); + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/CompanyByBookViewModel.cs b/Marechai/ViewModels/CompanyByBookViewModel.cs new file mode 100644 index 00000000..3e787bc3 --- /dev/null +++ b/Marechai/ViewModels/CompanyByBookViewModel.cs @@ -0,0 +1,36 @@ +/****************************************************************************** +// 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 CompanyByBookViewModel : BaseViewModel + { + public int CompanyId { get; set; } + public long BookId { get; set; } + public string RoleId { get; set; } + public string Company { get; set; } + public string Role { get; set; } + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/DocumentRoleViewModel.cs b/Marechai/ViewModels/DocumentRoleViewModel.cs new file mode 100644 index 00000000..67dee70b --- /dev/null +++ b/Marechai/ViewModels/DocumentRoleViewModel.cs @@ -0,0 +1,33 @@ +/****************************************************************************** +// 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 DocumentRoleViewModel : BaseViewModel + { + public string Name { get; set; } + public bool Enabled { get; set; } + } +} \ No newline at end of file