Add people to magazine admin page.

This commit is contained in:
2020-08-10 03:18:09 +01:00
parent 35e000a02f
commit 8cc6542980
7 changed files with 331 additions and 4 deletions

View File

@@ -37,6 +37,9 @@
@inject MagazinesByMachineFamilyService MagazinesByMachineFamilyService
@inject MachinesService MachinesService
@inject MagazinesByMachineService MagazinesByMachineService
@inject DocumentPeopleService PeopleService
@inject PeopleByMagazineService PeopleByMagazineService
@inject DocumentRolesService DocumentRolesService
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment Host
@inject IJSRuntime JSRuntime
@@ -195,6 +198,69 @@
</div>
@if (!_editing)
{
<hr />
<h3>@L["People involved in this magazine"]</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 (_magazinePeople?.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 _magazinePeople)
{
<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 magazine issue talks about"]</h3>
<Button Color="Color.Success" Clicked="OnAddFamilyClick" Disabled="_addingMachineFamily">@L["Add new (machine family)"]</Button>

View File

@@ -37,19 +37,25 @@ namespace Marechai.Pages.Admin.Details
{
public partial class MagazineIssue
{
bool _addingMachine;
bool _addingMachineFamily;
int? _addingMachineFamilyId;
int? _addingMachineId;
bool _addingMachine;
bool _addingMachineFamily;
int? _addingMachineFamilyId;
int? _addingMachineId;
bool _addingPerson;
int? _addingPersonId;
string _addingPersonRoleId;
AuthenticationState _authState;
bool _creating;
MagazineByMachineViewModel _currentMagazineByMachine;
MagazineByMachineFamilyViewModel _currentMagazineByMachineFamily;
PersonByMagazineViewModel _currentPersonByMagazine;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingMagazineByMachine;
bool _deletingMagazineByMachineFamily;
bool _deletingPersonByMagazine;
bool _editing;
Modal _frmDelete;
bool _loaded;
@@ -57,10 +63,14 @@ namespace Marechai.Pages.Admin.Details
List<MachineViewModel> _machines;
List<MagazineByMachineFamilyViewModel> _magazineMachineFamilies;
List<MagazineByMachineViewModel> _magazineMachines;
List<PersonByMagazineViewModel> _magazinePeople;
List<MagazineViewModel> _magazines;
MagazineIssueViewModel _model;
List<DocumentPersonViewModel> _people;
List<DocumentRoleViewModel> _roles;
bool _savingMachine;
bool _savingMachineFamily;
bool _savingPerson;
bool _unknownIssueNumber;
bool _unknownNativeCaption;
bool _unknownPages;
@@ -87,12 +97,15 @@ namespace Marechai.Pages.Admin.Details
_magazines = await MagazinesService.GetTitlesAsync();
_machineFamilies = await MachineFamiliesService.GetAsync();
_machines = await MachinesService.GetAsync();
_roles = await DocumentRolesService.GetEnabledAsync();
_model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id);
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_addingMachineFamilyId = _machineFamilies.First().Id;
_magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id);
_addingMachineId = _machines.First().Id;
_magazineMachines = await MagazinesByMachineService.GetByMagazine(Id);
_addingPersonRoleId = _roles.First().Id;
_magazinePeople = await PeopleByMagazineService.GetByMagazine(Id);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/magazine_issues/edit/",
@@ -201,6 +214,8 @@ namespace Marechai.Pages.Admin.Details
_currentMagazineByMachineFamily = null;
_deletingMagazineByMachine = false;
_currentMagazineByMachine = null;
_deletingPersonByMagazine = false;
_currentPersonByMagazine = null;
}
void HideModal() => _frmDelete.Hide();
@@ -211,6 +226,8 @@ namespace Marechai.Pages.Admin.Details
await ConfirmDeleteMagazineByMachineFamily();
else if(_deletingMagazineByMachine)
await ConfirmDeleteMagazineByMachine();
else if(_deletingPersonByMagazine)
await ConfirmDeletePersonByMagazine();
}
void OnAddFamilyClick()
@@ -377,5 +394,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 PeopleByMagazineService.CreateAsync(_addingPersonId.Value, Id, _addingPersonRoleId,
(await UserManager.GetUserAsync(_authState.User)).Id);
_magazinePeople = await PeopleByMagazineService.GetByMagazine(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)
{
_currentPersonByMagazine = _magazinePeople.FirstOrDefault(n => n.Id == itemId);
_deletingPersonByMagazine = true;
_deleteTitle = L["Delete person from this magazine"];
_deleteText =
string.Format(L["Are you sure you want to delete the person {0} with role {1} from this magazine?"],
_currentPersonByMagazine?.FullName, _currentPersonByMagazine?.Role);
_frmDelete.Show();
}
async Task ConfirmDeletePersonByMagazine()
{
if(_currentPersonByMagazine is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await PeopleByMagazineService.DeleteAsync(_currentPersonByMagazine.Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_magazinePeople = await PeopleByMagazineService.GetByMagazine(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
}
}

View File

@@ -149,4 +149,22 @@
<data name="Are you sure you want to delete the machine family {0} from this magazine issue?" xml:space="preserve">
<value>Are you sure you want to delete the machine family {0} from this magazine issue?</value>
</data>
<data name="People involved in this magazine" xml:space="preserve">
<value>People involved in this magazine</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="Role" xml:space="preserve">
<value>Role</value>
</data>
<data name="Delete person from this magazine" xml:space="preserve">
<value>Delete person from this magazine</value>
</data>
<data name="Are you sure you want to delete the person {0} with role {1} from this magazine?" xml:space="preserve">
<value>Are you sure you want to delete the person {0} with role {1} from this magazine?</value>
</data>
</root>

View File

@@ -256,4 +256,22 @@
<data name="Are you sure you want to delete the machine family {0} from this magazine issue?" xml:space="preserve">
<value>¿Estás seguro de eliminar la familia de máquinas {0} de esta revista?</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 magazine?" xml:space="preserve">
<value>¿Estás seguro de eliminar la persona {0} con rol {1} de esta revista?</value>
</data>
<data name="Delete person from this magazine" xml:space="preserve">
<value>Eliminar persona de esta revista</value>
</data>
<data name="People involved in this magazine" xml:space="preserve">
<value>Personas involucradas en esta revista</value>
</data>
<data name="Person" xml:space="preserve">
<value>Persona</value>
</data>
<data name="Role" xml:space="preserve">
<value>Rol</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 PeopleByMagazineService
{
readonly MarechaiContext _context;
public PeopleByMagazineService(MarechaiContext context) => _context = context;
public async Task<List<PersonByMagazineViewModel>> GetByMagazine(long magazineId) =>
(await _context.PeopleByMagazines.Where(p => p.MagazineId == magazineId).
Select(p => new PersonByMagazineViewModel
{
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,
MagazineId = p.MagazineId
}).ToListAsync()).OrderBy(p => p.FullName).ThenBy(p => p.Role).ToList();
public async Task DeleteAsync(long id, string userId)
{
PeopleByMagazine item = await _context.PeopleByMagazines.FindAsync(id);
if(item is null)
return;
_context.PeopleByMagazines.Remove(item);
await _context.SaveChangesWithUserAsync(userId);
}
public async Task<long> CreateAsync(int personId, long magazineId, string roleId, string userId)
{
var item = new PeopleByMagazine
{
PersonId = personId,
MagazineId = magazineId,
RoleId = roleId
};
await _context.PeopleByMagazines.AddAsync(item);
await _context.SaveChangesWithUserAsync(userId);
return item.Id;
}
}
}

View File

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

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 PersonByMagazineViewModel : BaseViewModel<long>
{
public int PersonId { get; set; }
public long MagazineId { 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}";
}
}