Add machine families to document admin page.

This commit is contained in:
2020-08-09 20:16:38 +01:00
parent c78c34c799
commit b8171d8c84
7 changed files with 313 additions and 38 deletions

View File

@@ -36,6 +36,8 @@
@inject DocumentCompaniesService CompaniesService
@inject DocumentRolesService DocumentRolesService
@inject CompaniesByDocumentService CompaniesByDocumentService
@inject MachineFamiliesService MachineFamiliesService
@inject DocumentsByMachineFamilyService DocumentsByMachineFamilyService
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment Host
@inject IJSRuntime JSRuntime
@@ -165,8 +167,8 @@
}
</Select>
</Field>
<Button Color="Color.Primary" Clicked="@CancelAddCpu" Disabled="@_savingCompany">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddCpu" Disabled="@_savingCompany">@L["Add"]</Button>
<Button Color="Color.Primary" Clicked="@CancelAddCompany" Disabled="@_savingCompany">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddCompany" Disabled="@_savingCompany">@L["Add"]</Button>
</div>
}
@if (_documentCompanies?.Count > 0)
@@ -195,7 +197,55 @@
@item.Role
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowCpuDeleteModal(item.Id);}" Disabled="@_addingCompany">@L["Delete"]</Button>
<Button Color="Color.Danger" Clicked="() => {ShowCompanyDeleteModal(item.Id);}" Disabled="@_addingCompany">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<hr />
<h3>@L["Machine families this document talks about"]</h3>
<Button Color="Color.Success" Clicked="OnAddFamilyClick" Disabled="_addingMachineFamily">@L["Add new (machine family)"]</Button>
@if (_addingMachineFamily)
{
<div>
<Field>
<FieldLabel>@L["Family"]</FieldLabel>
<Select Disabled="_savingMachineFamily" TValue="int?" @bind-SelectedValue="@_addingMachineFamilyId">
@foreach (var family in _machineFamilies)
{
<SelectItem TValue="int?" Value="@family.Id">@family.Name</SelectItem>
}
</Select>
</Field>
<Button Color="Color.Primary" Clicked="@CancelAddFamily" Disabled="@_savingMachineFamily">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddFamily" Disabled="@_savingMachineFamily">@L["Add"]</Button>
</div>
}
@if (_documentMachineFamilies?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Family"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _documentMachineFamilies)
{
<tr>
<td>
@item.MachineFamily
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowMachineFamilyDeleteModal(item.Id);}" Disabled="@_addingMachineFamily">@L["Delete"]</Button>
</td>
</tr>
}

View File

@@ -38,28 +38,35 @@ namespace Marechai.Pages.Admin.Details
{
public partial class Document
{
bool _addingCompany;
int? _addingCompanyId;
string _addingCompanyRoleId;
AuthenticationState _authState;
List<DocumentCompanyViewModel> _companies;
List<Iso31661Numeric> _countries;
bool _creating;
CompanyByDocumentViewModel _currentCompanyByDocument;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingCompanyByDocument;
List<CompanyByDocumentViewModel> _documentCompanies;
bool _editing;
Modal _frmDelete;
bool _loaded;
DocumentViewModel _model;
List<DocumentRoleViewModel> _roles;
bool _savingCompany;
bool _unknownCountry;
bool _unknownNativeTitle;
bool _unknownPublished;
bool _addingCompany;
int? _addingCompanyId;
string _addingCompanyRoleId;
bool _addingMachineFamily;
int? _addingMachineFamilyId;
AuthenticationState _authState;
List<DocumentCompanyViewModel> _companies;
List<Iso31661Numeric> _countries;
bool _creating;
CompanyByDocumentViewModel _currentCompanyByDocument;
DocumentByMachineFamilyViewModel _currentDocumentByMachineFamily;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingCompanyByDocument;
bool _deletingDocumentByMachineFamily;
List<CompanyByDocumentViewModel> _documentCompanies;
List<DocumentByMachineFamilyViewModel> _documentMachineFamilies;
bool _editing;
Modal _frmDelete;
bool _loaded;
List<MachineFamilyViewModel> _machineFamilies;
DocumentViewModel _model;
List<DocumentRoleViewModel> _roles;
bool _savingCompany;
bool _savingMachineFamily;
bool _unknownCountry;
bool _unknownNativeTitle;
bool _unknownPublished;
[Parameter]
public long Id { get; set; }
@@ -78,13 +85,16 @@ namespace Marechai.Pages.Admin.Details
!_creating)
return;
_countries = await CountriesService.GetAsync();
_companies = await CompaniesService.GetAsync();
_roles = await DocumentRolesService.GetEnabledAsync();
_model = _creating ? new DocumentViewModel() : await Service.GetAsync(Id);
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_addingCompanyRoleId = _roles.First().Id;
_documentCompanies = await CompaniesByDocumentService.GetByDocument(Id);
_countries = await CountriesService.GetAsync();
_companies = await CompaniesService.GetAsync();
_roles = await DocumentRolesService.GetEnabledAsync();
_machineFamilies = await MachineFamiliesService.GetAsync();
_model = _creating ? new DocumentViewModel() : await Service.GetAsync(Id);
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_addingCompanyRoleId = _roles.First().Id;
_documentCompanies = await CompaniesByDocumentService.GetByDocument(Id);
_addingMachineFamilyId = _machineFamilies.First().Id;
_documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(Id);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/documents/edit/",
@@ -173,19 +183,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;
}
@@ -211,7 +221,7 @@ namespace Marechai.Pages.Admin.Details
StateHasChanged();
}
void ShowCpuDeleteModal(long itemId)
void ShowCompanyDeleteModal(long itemId)
{
_currentCompanyByDocument = _documentCompanies.FirstOrDefault(n => n.Id == itemId);
_deletingCompanyByDocument = true;
@@ -236,10 +246,12 @@ namespace Marechai.Pages.Admin.Details
async void ConfirmDelete()
{
if(_deletingCompanyByDocument)
await ConfirmDeleteCpuByMachine();
await ConfirmDeleteCompanyByMachine();
else if(_deletingDocumentByMachineFamily)
await ConfirmDeleteDocumentByMachineFamily();
}
async Task ConfirmDeleteCpuByMachine()
async Task ConfirmDeleteCompanyByMachine()
{
if(_currentCompanyByDocument is null)
return;
@@ -263,5 +275,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 DocumentsByMachineFamilyService.CreateAsync(_addingMachineFamilyId.Value, Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(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)
{
_currentDocumentByMachineFamily = _documentMachineFamilies.FirstOrDefault(n => n.Id == itemId);
_deletingDocumentByMachineFamily = true;
_deleteTitle = L["Delete machine family from this document"];
_deleteText = string.Format(L["Are you sure you want to delete the machine family {0} from this document?"],
_currentDocumentByMachineFamily?.MachineFamily);
_frmDelete.Show();
}
async Task ConfirmDeleteDocumentByMachineFamily()
{
if(_currentDocumentByMachineFamily is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await DocumentsByMachineFamilyService.DeleteAsync(_currentDocumentByMachineFamily.Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
}
}

View File

@@ -113,4 +113,13 @@
<data name="Are you sure you want to delete the company {0} with role {1} from this document?" xml:space="preserve">
<value>Are you sure you want to delete the company {0} with role {1} from this document?</value>
</data>
<data name="Machine families this document talks about" xml:space="preserve">
<value>Machine families this document talks about</value>
</data>
<data name="Add new (machine family)" xml:space="preserve">
<value>Add new</value>
</data>
<data name="Family" xml:space="preserve">
<value>Family</value>
</data>
</root>

View File

@@ -229,4 +229,13 @@
<data name="Role" xml:space="preserve">
<value>Rol</value>
</data>
<data name="Add new (machine family)" xml:space="preserve">
<value>Añadir nueva</value>
</data>
<data name="Family" xml:space="preserve">
<value>Familia</value>
</data>
<data name="Machine families this document talks about" xml:space="preserve">
<value>Familias de máquinas sobre las que habla este documento</value>
</data>
</root>

View File

@@ -0,0 +1,77 @@
/******************************************************************************
// 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 DocumentsByMachineFamilyService
{
readonly MarechaiContext _context;
public DocumentsByMachineFamilyService(MarechaiContext context) => _context = context;
public async Task<List<DocumentByMachineFamilyViewModel>> GetByDocument(long bookId) =>
await _context.DocumentsByMachineFamilies.Where(p => p.DocumentId == bookId).
Select(p => new DocumentByMachineFamilyViewModel
{
Id = p.Id,
DocumentId = p.DocumentId,
MachineFamilyId = p.MachineFamilyId,
MachineFamily = p.MachineFamily.Name
}).OrderBy(p => p.MachineFamily).ThenBy(p => p.Document).ToListAsync();
public async Task DeleteAsync(long id, string userId)
{
DocumentsByMachineFamily item = await _context.DocumentsByMachineFamilies.FindAsync(id);
if(item is null)
return;
_context.DocumentsByMachineFamilies.Remove(item);
await _context.SaveChangesWithUserAsync(userId);
}
public async Task<long> CreateAsync(int machineFamilyId, long bookId, string userId)
{
var item = new DocumentsByMachineFamily
{
MachineFamilyId = machineFamilyId,
DocumentId = bookId
};
await _context.DocumentsByMachineFamilies.AddAsync(item);
await _context.SaveChangesWithUserAsync(userId);
return item.Id;
}
}
}

View File

@@ -86,6 +86,7 @@ namespace Marechai.Services
services.AddScoped<CompaniesBySoftwareVersionService>();
services.AddScoped<DocumentRolesService>();
services.AddScoped<BooksByMachineFamilyService>();
services.AddScoped<DocumentsByMachineFamilyService>();
}
}
}

View File

@@ -0,0 +1,35 @@
/******************************************************************************
// 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 DocumentByMachineFamilyViewModel : BaseViewModel<long>
{
public long DocumentId { get; set; }
public string Document { get; set; }
public int MachineFamilyId { get; set; }
public string MachineFamily { get; set; }
}
}