Add people to document admin page.

This commit is contained in:
2020-08-10 03:17:24 +01:00
parent 9930ccaf0e
commit 35e000a02f
8 changed files with 326 additions and 8 deletions

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>4.0.0.1821</Version>
<Version>4.0.0.1822</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>

View File

@@ -40,6 +40,8 @@
@inject DocumentsByMachineFamilyService DocumentsByMachineFamilyService
@inject MachinesService MachinesService
@inject DocumentsByMachineService DocumentsByMachineService
@inject DocumentPeopleService PeopleService
@inject PeopleByDocumentService PeopleByDocumentService
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment Host
@inject IJSRuntime JSRuntime
@@ -208,6 +210,69 @@
</div>
}
<hr />
<h3>@L["People involved in this document"]</h3>
<Button Color="Color.Success" Clicked="OnAddPersonClick" Disabled="_addingPerson">@L["Add new (person)"]</Button>
@if (_addingPerson)
{
<div>
<Field>
<FieldLabel>@L["Person"]</FieldLabel>
<Select Disabled="_savingPerson" TValue="int?" @bind-SelectedValue="@_addingPersonId">
@foreach (var person in _people)
{
<SelectItem TValue="int?" Value="@person.Id">@person.FullName</SelectItem>
}
</Select>
</Field>
<Field>
<FieldLabel>@L["Role"]</FieldLabel>
<Select Disabled="!_editing" TValue="string" @bind-SelectedValue="@_addingPersonRoleId">
@foreach (var role in _roles)
{
<SelectItem TValue="string" Value="@role.Id">@role.Name</SelectItem>
}
</Select>
</Field>
<Button Color="Color.Primary" Clicked="@CancelAddPerson" Disabled="@_savingPerson">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddPerson" Disabled="@_savingPerson">@L["Add"]</Button>
</div>
}
@if (_documentPeople?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Person"]
</th>
<th>
@L["Role"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _documentPeople)
{
<tr>
<td>
@item.FullName
</td>
<td>
@item.Role
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowPersonDeleteModal(item.Id);}" Disabled="@_addingPerson">@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>

View File

@@ -38,13 +38,17 @@ namespace Marechai.Pages.Admin.Details
{
public partial class Document
{
bool _addingCompany;
int? _addingCompanyId;
string _addingCompanyRoleId;
bool _addingMachine;
bool _addingMachineFamily;
int? _addingMachineFamilyId;
int? _addingMachineId;
bool _addingCompany;
int? _addingCompanyId;
string _addingCompanyRoleId;
bool _addingMachine;
bool _addingMachineFamily;
int? _addingMachineFamilyId;
int? _addingMachineId;
bool _addingPerson;
int? _addingPersonId;
string _addingPersonRoleId;
AuthenticationState _authState;
List<DocumentCompanyViewModel> _companies;
List<Iso31661Numeric> _countries;
@@ -52,25 +56,30 @@ namespace Marechai.Pages.Admin.Details
CompanyByDocumentViewModel _currentCompanyByDocument;
DocumentByMachineViewModel _currentDocumentByMachine;
DocumentByMachineFamilyViewModel _currentDocumentByMachineFamily;
PersonByDocumentViewModel _currentPersonByDocument;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingCompanyByDocument;
bool _deletingDocumentByMachine;
bool _deletingDocumentByMachineFamily;
bool _deletingPersonByDocument;
List<CompanyByDocumentViewModel> _documentCompanies;
List<DocumentByMachineFamilyViewModel> _documentMachineFamilies;
List<DocumentByMachineViewModel> _documentMachines;
List<PersonByDocumentViewModel> _documentPeople;
bool _editing;
Modal _frmDelete;
bool _loaded;
List<MachineFamilyViewModel> _machineFamilies;
List<MachineViewModel> _machines;
DocumentViewModel _model;
List<DocumentPersonViewModel> _people;
List<DocumentRoleViewModel> _roles;
bool _savingCompany;
bool _savingMachine;
bool _savingMachineFamily;
bool _savingPerson;
bool _unknownCountry;
bool _unknownNativeTitle;
bool _unknownPublished;
@@ -105,6 +114,8 @@ namespace Marechai.Pages.Admin.Details
_documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(Id);
_addingMachineId = _machines.First().Id;
_documentMachines = await DocumentsByMachineService.GetByDocument(Id);
_addingPersonRoleId = _roles.First().Id;
_documentPeople = await PeopleByDocumentService.GetByDocument(Id);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/documents/edit/",
@@ -253,6 +264,8 @@ namespace Marechai.Pages.Admin.Details
_currentDocumentByMachineFamily = null;
_deletingDocumentByMachine = false;
_currentDocumentByMachine = null;
_deletingPersonByDocument = false;
_currentPersonByDocument = null;
}
void HideModal() => _frmDelete.Hide();
@@ -265,6 +278,8 @@ namespace Marechai.Pages.Admin.Details
await ConfirmDeleteDocumentByMachineFamily();
else if(_deletingDocumentByMachine)
await ConfirmDeleteDocumentByMachine();
else if(_deletingPersonByDocument)
await ConfirmDeletePersonByDocument();
}
async Task ConfirmDeleteCompanyByMachine()
@@ -455,5 +470,88 @@ namespace Marechai.Pages.Admin.Details
// Tell we finished loading
StateHasChanged();
}
void OnAddPersonClick()
{
_addingPerson = true;
_savingPerson = false;
_addingPersonId = _people.First().Id;
}
void CancelAddPerson()
{
_addingPerson = false;
_savingPerson = false;
_addingPersonId = null;
}
async Task ConfirmAddPerson()
{
if(_addingPersonId is null ||
_addingPersonId <= 0)
{
CancelAddPerson();
return;
}
_savingPerson = true;
// Yield thread to let UI to update
await Task.Yield();
await PeopleByDocumentService.CreateAsync(_addingPersonId.Value, Id, _addingPersonRoleId,
(await UserManager.GetUserAsync(_authState.User)).Id);
_documentPeople = await PeopleByDocumentService.GetByDocument(Id);
_addingPerson = false;
_savingPerson = false;
_addingPersonId = null;
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
void ShowPersonDeleteModal(long itemId)
{
_currentPersonByDocument = _documentPeople.FirstOrDefault(n => n.Id == itemId);
_deletingPersonByDocument = true;
_deleteTitle = L["Delete person from this document"];
_deleteText =
string.Format(L["Are you sure you want to delete the person {0} with role {1} from this document?"],
_currentPersonByDocument?.FullName, _currentPersonByDocument?.Role);
_frmDelete.Show();
}
async Task ConfirmDeletePersonByDocument()
{
if(_currentPersonByDocument is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await PeopleByDocumentService.DeleteAsync(_currentPersonByDocument.Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_documentPeople = await PeopleByDocumentService.GetByDocument(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
}
}

View File

@@ -143,4 +143,19 @@
<data name="Machines this document talks about" xml:space="preserve">
<value>Machines this document talks about</value>
</data>
<data name="People involved in this document" xml:space="preserve">
<value>People involved in this document</value>
</data>
<data name="Add new (person)" xml:space="preserve">
<value>Add new</value>
</data>
<data name="Person" xml:space="preserve">
<value>Person</value>
</data>
<data name="Delete person from this document" xml:space="preserve">
<value>Delete person from this document</value>
</data>
<data name="Are you sure you want to delete the person {0} with role {1} from this document?" xml:space="preserve">
<value>Are you sure you want to delete the person {0} with role {1} from this document?</value>
</data>
</root>

View File

@@ -259,4 +259,19 @@
<data name="Machines this document talks about" xml:space="preserve">
<value>Máquinas sobre las que habla este documento</value>
</data>
<data name="Add new (person)" xml:space="preserve">
<value>Añadir nueva</value>
</data>
<data name="Are you sure you want to delete the person {0} with role {1} from this document?" xml:space="preserve">
<value>¿Estás seguro de eliminar la persona {0} con el rol {1} de este documento?</value>
</data>
<data name="Delete person from this document" xml:space="preserve">
<value>Eliminar persona de este documento</value>
</data>
<data name="People involved in this document" xml:space="preserve">
<value>Personas involucradas en este documento</value>
</data>
<data name="Person" xml:space="preserve">
<value>Persona</value>
</data>
</root>

View File

@@ -0,0 +1,83 @@
/******************************************************************************
// 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 PeopleByDocumentService
{
readonly MarechaiContext _context;
public PeopleByDocumentService(MarechaiContext context) => _context = context;
public async Task<List<PersonByDocumentViewModel>> GetByDocument(long documentId) =>
(await _context.PeopleByDocuments.Where(p => p.DocumentId == documentId).
Select(p => new PersonByDocumentViewModel
{
Id = p.Id,
Name = p.Person.Name,
Surname = p.Person.Surname,
Alias = p.Person.Alias,
DisplayName = p.Person.DisplayName,
PersonId = p.PersonId,
RoleId = p.RoleId,
Role = p.Role.Name,
DocumentId = p.DocumentId
}).ToListAsync()).OrderBy(p => p.FullName).ThenBy(p => p.Role).ToList();
public async Task DeleteAsync(long id, string userId)
{
PeopleByDocument item = await _context.PeopleByDocuments.FindAsync(id);
if(item is null)
return;
_context.PeopleByDocuments.Remove(item);
await _context.SaveChangesWithUserAsync(userId);
}
public async Task<long> CreateAsync(int personId, long documentId, string roleId, string userId)
{
var item = new PeopleByDocument
{
PersonId = personId,
DocumentId = documentId,
RoleId = roleId
};
await _context.PeopleByDocuments.AddAsync(item);
await _context.SaveChangesWithUserAsync(userId);
return item.Id;
}
}
}

View File

@@ -92,6 +92,7 @@ namespace Marechai.Services
services.AddScoped<DocumentsByMachineService>();
services.AddScoped<MagazinesByMachineService>();
services.AddScoped<PeopleByBookService>();
services.AddScoped<PeopleByDocumentService>();
}
}
}

View File

@@ -0,0 +1,41 @@
/******************************************************************************
// 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 PersonByDocumentViewModel : BaseViewModel<long>
{
public int PersonId { get; set; }
public long DocumentId { get; set; }
public string RoleId { get; set; }
public string Role { get; set; }
public string Name { get; set; }
public string Alias { get; set; }
public string Surname { get; set; }
public string DisplayName { get; set; }
public string FullName => DisplayName ?? Alias ?? $"{Name} {Surname}";
}
}