Add companies to book admin page.

This commit is contained in:
2020-08-09 17:03:42 +01:00
parent 7bded12245
commit eadedfde30
8 changed files with 474 additions and 16 deletions

View File

@@ -33,6 +33,9 @@
@inherits OwningComponentBase<BooksService>
@inject IStringLocalizer<BooksService> 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)
{
<hr />
<h3>@L["Companies involved in this book"]</h3>
<Button Color="Color.Success" Clicked="OnAddCompanyClick" Disabled="_addingCompany">@L["Add new (company)"]</Button>
@if (_addingCompany)
{
<div>
<Field>
<FieldLabel>@L["Company"]</FieldLabel>
<Select Disabled="_savingCompany" TValue="int?" @bind-SelectedValue="@_addingCompanyId">
@foreach (var company in _companies)
{
<SelectItem TValue="int?" Value="@company.Id">@company.Name</SelectItem>
}
</Select>
</Field>
<Field>
<FieldLabel>@L["Role"]</FieldLabel>
<Select Disabled="!_editing" TValue="string" @bind-SelectedValue="@_addingCompanyRoleId">
@foreach (var role in _roles)
{
<SelectItem TValue="string" Value="@role.Id">@role.Name</SelectItem>
}
</Select>
</Field>
<Button Color="Color.Primary" Clicked="@CancelAddCpu" Disabled="@_savingCompany">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddCpu" Disabled="@_savingCompany">@L["Add"]</Button>
</div>
}
@if (_bookCompanies?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Company"]
</th>
<th>
@L["Role"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _bookCompanies)
{
<tr>
<td>
@item.Company
</td>
<td>
@item.Role
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowCpuDeleteModal(item.Id);}" Disabled="@_addingCompany">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop />
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@_deleteTitle</ModalTitle>
<CloseButton Clicked="@HideModal" />
</ModalHeader>
<ModalBody>
<Text>@_deleteText</Text>
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
</ModalFooter>
</ModalContent>
</Modal>
}

View File

@@ -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<Iso31661Numeric> _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<CompanyByBookViewModel> _bookCompanies;
List<DocumentCompanyViewModel> _companies;
List<Iso31661Numeric> _countries;
bool _creating;
CompanyByBookViewModel _currentCompanyByBook;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingCompanyByBook;
bool _editing;
Modal _frmDelete;
bool _loaded;
BookViewModel _model;
List<DocumentRoleViewModel> _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();
}
}
}

View File

@@ -116,4 +116,25 @@
<data name="Unknown (country of publication)" xml:space="preserve">
<value>Unknown</value>
</data>
<data name="Companies involved in this book" xml:space="preserve">
<value>Companies involved in this book</value>
</data>
<data name="Add new (company)" xml:space="preserve">
<value>Add new</value>
</data>
<data name="Company" xml:space="preserve">
<value>Company</value>
</data>
<data name="Role" xml:space="preserve">
<value>Role</value>
</data>
<data name="Add" xml:space="preserve">
<value>Add</value>
</data>
<data name="Delete company from this book" xml:space="preserve">
<value>Delete company from this book</value>
</data>
<data name="Are you sure you want to delete the company {0} with role {1} from this book?" xml:space="preserve">
<value>Are you sure you want to delete the company {0} with role {1} from this book?</value>
</data>
</root>

View File

@@ -147,7 +147,7 @@
<comment>Delete book</comment>
</data>
<data name="Are you sure you want to delete the book {0}?" xml:space="preserve">
<value>¿Está seguro de que desea borrar el libro {0}?</value>
<value>¿Estás seguro de borrar el libro {0}?</value>
<comment>{0} book name</comment>
</data>
<data name="Cancel" xml:space="preserve">
@@ -232,4 +232,25 @@
<data name="Native title must be smaller than 256 characters." xml:space="preserve">
<value>El título nativo debe contener menos de 256 caracteres.</value>
</data>
<data name="Companies involved in this book" xml:space="preserve">
<value>Compañías involucradas en este libro</value>
</data>
<data name="Add new (company)" xml:space="preserve">
<value>Añadir nueva</value>
</data>
<data name="Company" xml:space="preserve">
<value>Compañía</value>
</data>
<data name="Role" xml:space="preserve">
<value>Rol</value>
</data>
<data name="Add" xml:space="preserve">
<value>Añadir</value>
</data>
<data name="Delete company from this book" xml:space="preserve">
<value>Eliminar compañía de este libro</value>
</data>
<data name="Are you sure you want to delete the company {0} with role {1} from this book?" xml:space="preserve">
<value>¿Estás seguro de eliminar la compañía {0} con el rol {1} de este libro?</value>
</data>
</root>

View File

@@ -0,0 +1,79 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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<List<CompanyByBookViewModel>> 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<long> 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;
}
}
}

View File

@@ -0,0 +1,67 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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<List<DocumentRoleViewModel>> 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<List<DocumentRoleViewModel>> 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<DocumentRoleViewModel> 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();
}
}

View File

@@ -0,0 +1,36 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
namespace Marechai.ViewModels
{
public class CompanyByBookViewModel : BaseViewModel<long>
{
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; }
}
}

View File

@@ -0,0 +1,33 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
namespace Marechai.ViewModels
{
public class DocumentRoleViewModel : BaseViewModel<string>
{
public string Name { get; set; }
public bool Enabled { get; set; }
}
}