Add machines to magazine issue admin page.

This commit is contained in:
2020-08-10 01:23:07 +01:00
parent e871010eec
commit 381fb5d717
7 changed files with 301 additions and 0 deletions

View File

@@ -35,6 +35,8 @@
@inject MagazinesService MagazinesService @inject MagazinesService MagazinesService
@inject MachineFamiliesService MachineFamiliesService @inject MachineFamiliesService MachineFamiliesService
@inject MagazinesByMachineFamilyService MagazinesByMachineFamilyService @inject MagazinesByMachineFamilyService MagazinesByMachineFamilyService
@inject MachinesService MachinesService
@inject MagazinesByMachineService MagazinesByMachineService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IWebHostEnvironment Host @inject IWebHostEnvironment Host
@inject IJSRuntime JSRuntime @inject IJSRuntime JSRuntime
@@ -241,6 +243,54 @@
</div> </div>
} }
<hr />
<h3>@L["Machines this magazine talks about"]</h3>
<Button Color="Color.Success" Clicked="OnAddMachineClick" Disabled="_addingMachine">@L["Add new (machine)"]</Button>
@if (_addingMachine)
{
<div>
<Field>
<FieldLabel>@L[""]</FieldLabel>
<Select Disabled="_savingMachine" TValue="int?" @bind-SelectedValue="@_addingMachineId">
@foreach (var machine in _machines)
{
<SelectItem TValue="int?" Value="@machine.Id">@machine.Name</SelectItem>
}
</Select>
</Field>
<Button Color="Color.Primary" Clicked="@CancelAddMachine" Disabled="@_savingMachine">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddMachine" Disabled="@_savingMachine">@L["Add"]</Button>
</div>
}
@if (_magazineMachines?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Machine"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _magazineMachines)
{
<tr>
<td>
@item.Machine
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowMachineDeleteModal(item.Id);}" Disabled="@_addingMachine">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing"> <Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop /> <ModalBackdrop />
<ModalContent Centered="true"> <ModalContent Centered="true">

View File

@@ -37,22 +37,29 @@ namespace Marechai.Pages.Admin.Details
{ {
public partial class MagazineIssue public partial class MagazineIssue
{ {
bool _addingMachine;
bool _addingMachineFamily; bool _addingMachineFamily;
int? _addingMachineFamilyId; int? _addingMachineFamilyId;
int? _addingMachineId;
AuthenticationState _authState; AuthenticationState _authState;
bool _creating; bool _creating;
MagazineByMachineViewModel _currentMagazineByMachine;
MagazineByMachineFamilyViewModel _currentMagazineByMachineFamily; MagazineByMachineFamilyViewModel _currentMagazineByMachineFamily;
bool _deleteInProgress; bool _deleteInProgress;
string _deleteText; string _deleteText;
string _deleteTitle; string _deleteTitle;
bool _deletingMagazineByMachine;
bool _deletingMagazineByMachineFamily; bool _deletingMagazineByMachineFamily;
bool _editing; bool _editing;
Modal _frmDelete; Modal _frmDelete;
bool _loaded; bool _loaded;
List<MachineFamilyViewModel> _machineFamilies; List<MachineFamilyViewModel> _machineFamilies;
List<MachineViewModel> _machines;
List<MagazineByMachineFamilyViewModel> _magazineMachineFamilies; List<MagazineByMachineFamilyViewModel> _magazineMachineFamilies;
List<MagazineByMachineViewModel> _magazineMachines;
List<MagazineViewModel> _magazines; List<MagazineViewModel> _magazines;
MagazineIssueViewModel _model; MagazineIssueViewModel _model;
bool _savingMachine;
bool _savingMachineFamily; bool _savingMachineFamily;
bool _unknownIssueNumber; bool _unknownIssueNumber;
bool _unknownNativeCaption; bool _unknownNativeCaption;
@@ -79,10 +86,13 @@ namespace Marechai.Pages.Admin.Details
_magazines = await MagazinesService.GetTitlesAsync(); _magazines = await MagazinesService.GetTitlesAsync();
_machineFamilies = await MachineFamiliesService.GetAsync(); _machineFamilies = await MachineFamiliesService.GetAsync();
_machines = await MachinesService.GetAsync();
_model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id); _model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id);
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_addingMachineFamilyId = _machineFamilies.First().Id; _addingMachineFamilyId = _machineFamilies.First().Id;
_magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id); _magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id);
_addingMachineId = _machines.First().Id;
_magazineMachines = await MagazinesByMachineService.GetByMagazine(Id);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/magazine_issues/edit/", StartsWith("admin/magazine_issues/edit/",
@@ -189,6 +199,8 @@ namespace Marechai.Pages.Admin.Details
_deleteInProgress = false; _deleteInProgress = false;
_deletingMagazineByMachineFamily = false; _deletingMagazineByMachineFamily = false;
_currentMagazineByMachineFamily = null; _currentMagazineByMachineFamily = null;
_deletingMagazineByMachine = false;
_currentMagazineByMachine = null;
} }
void HideModal() => _frmDelete.Hide(); void HideModal() => _frmDelete.Hide();
@@ -197,6 +209,8 @@ namespace Marechai.Pages.Admin.Details
{ {
if(_deletingMagazineByMachineFamily) if(_deletingMagazineByMachineFamily)
await ConfirmDeleteMagazineByMachineFamily(); await ConfirmDeleteMagazineByMachineFamily();
else if(_deletingMagazineByMachine)
await ConfirmDeleteMagazineByMachine();
} }
void OnAddFamilyClick() void OnAddFamilyClick()
@@ -281,5 +295,87 @@ namespace Marechai.Pages.Admin.Details
// Tell we finished loading // Tell we finished loading
StateHasChanged(); StateHasChanged();
} }
void OnAddMachineClick()
{
_addingMachine = true;
_savingMachine = false;
_addingMachineId = _machines.First().Id;
}
void CancelAddMachine()
{
_addingMachine = false;
_savingMachine = false;
_addingMachineId = null;
}
async Task ConfirmAddMachine()
{
if(_addingMachineId is null ||
_addingMachineId <= 0)
{
CancelAddMachine();
return;
}
_savingMachine = true;
// Yield thread to let UI to update
await Task.Yield();
await MagazinesByMachineService.CreateAsync(_addingMachineId.Value, Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_magazineMachines = await MagazinesByMachineService.GetByMagazine(Id);
_addingMachine = false;
_savingMachine = false;
_addingMachineId = null;
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
void ShowMachineDeleteModal(long itemId)
{
_currentMagazineByMachine = _magazineMachines.FirstOrDefault(n => n.Id == itemId);
_deletingMagazineByMachine = true;
_deleteTitle = L["Delete machine from this magazine"];
_deleteText = string.Format(L["Are you sure you want to delete the machine {0} from this magazine issue?"],
_currentMagazineByMachine?.Machine);
_frmDelete.Show();
}
async Task ConfirmDeleteMagazineByMachine()
{
if(_currentMagazineByMachine is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await MagazinesByMachineService.DeleteAsync(_currentMagazineByMachine.Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_magazineMachines = await MagazinesByMachineService.GetByMagazine(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
} }
} }

View File

@@ -128,4 +128,25 @@
<data name="Family" xml:space="preserve"> <data name="Family" xml:space="preserve">
<value>Family</value> <value>Family</value>
</data> </data>
<data name="Delete machine from this magazine" xml:space="preserve">
<value>Delete machine from this magazine</value>
</data>
<data name="Are you sure you want to delete the machine {0} from this magazine issue?" xml:space="preserve">
<value>Are you sure you want to delete the machine {0} from this magazine issue?</value>
</data>
<data name="Machine" xml:space="preserve">
<value>Machine</value>
</data>
<data name="Add new (machine)" xml:space="preserve">
<value>Add new</value>
</data>
<data name="Machines this magazine issue talks about" xml:space="preserve">
<value>Machines this magazine issue talks about</value>
</data>
<data name="Delete machine family from this magazine" xml:space="preserve">
<value>Delete machine family from this magazine</value>
</data>
<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>
</root> </root>

View File

@@ -235,4 +235,25 @@
<data name="Family" xml:space="preserve"> <data name="Family" xml:space="preserve">
<value>Familia</value> <value>Familia</value>
</data> </data>
<data name="Machines this magazine issue talks about" xml:space="preserve">
<value>Máquinas sobre las que habla esta revista</value>
</data>
<data name="Machine" xml:space="preserve">
<value>Máquina</value>
</data>
<data name="Delete machine from this magazine" xml:space="preserve">
<value>Eliminar máquina de esta revista</value>
</data>
<data name="Add new (machine)" xml:space="preserve">
<value>Añadir nueva</value>
</data>
<data name="Are you sure you want to delete the machine {0} from this magazine issue?" xml:space="preserve">
<value>¿Estás seguro de eliminar la máquina {0} de esta revista?</value>
</data>
<data name="Delete machine family from this magazine" xml:space="preserve">
<value>Eliminar familia de máquina de esta revista</value>
</data>
<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>
</root> </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 MagazinesByMachineService
{
readonly MarechaiContext _context;
public MagazinesByMachineService(MarechaiContext context) => _context = context;
public async Task<List<MagazineByMachineViewModel>> GetByMagazine(long bookId) =>
await _context.MagazinesByMachines.Where(p => p.MagazineId == bookId).
Select(p => new MagazineByMachineViewModel
{
Id = p.Id,
MagazineId = p.MagazineId,
MachineId = p.MachineId,
Machine = p.Machine.Name
}).OrderBy(p => p.Machine).ThenBy(p => p.Magazine).ToListAsync();
public async Task DeleteAsync(long id, string userId)
{
MagazinesByMachine item = await _context.MagazinesByMachines.FindAsync(id);
if(item is null)
return;
_context.MagazinesByMachines.Remove(item);
await _context.SaveChangesWithUserAsync(userId);
}
public async Task<long> CreateAsync(int machineId, long bookId, string userId)
{
var item = new MagazinesByMachine
{
MachineId = machineId,
MagazineId = bookId
};
await _context.MagazinesByMachines.AddAsync(item);
await _context.SaveChangesWithUserAsync(userId);
return item.Id;
}
}
}

View File

@@ -90,6 +90,7 @@ namespace Marechai.Services
services.AddScoped<MagazinesByMachineFamilyService>(); services.AddScoped<MagazinesByMachineFamilyService>();
services.AddScoped<BooksByMachineService>(); services.AddScoped<BooksByMachineService>();
services.AddScoped<DocumentsByMachineService>(); services.AddScoped<DocumentsByMachineService>();
services.AddScoped<MagazinesByMachineService>();
} }
} }
} }

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 MagazineByMachineViewModel : BaseViewModel<long>
{
public long MagazineId { get; set; }
public string Magazine { get; set; }
public int MachineId { get; set; }
public string Machine { get; set; }
}
}