Add machine families to magazine issue admin page.

This commit is contained in:
2020-08-09 20:19:18 +01:00
parent b8171d8c84
commit 139618f41f
7 changed files with 327 additions and 14 deletions

View File

@@ -33,6 +33,8 @@
@inherits OwningComponentBase<MagazineIssuesService>
@inject IStringLocalizer<MagazineIssuesService> L
@inject MagazinesService MagazinesService
@inject MachineFamiliesService MachineFamiliesService
@inject MagazinesByMachineFamilyService MagazinesByMachineFamilyService
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment Host
@inject IJSRuntime JSRuntime
@@ -189,3 +191,70 @@
}
<a href="/admin/magazine_issues" class="btn btn-secondary">@L["Back to list"]</a>
</div>
@if (!_editing)
{
<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>
@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 (_magazineMachineFamilies?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Family"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _magazineMachineFamilies)
{
<tr>
<td>
@item.MachineFamily
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowMachineFamilyDeleteModal(item.Id);}" Disabled="@_addingMachineFamily">@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.Shared;
@@ -36,17 +37,28 @@ namespace Marechai.Pages.Admin.Details
{
public partial class MagazineIssue
{
AuthenticationState _authState;
bool _creating;
bool _editing;
bool _loaded;
List<MagazineViewModel> _magazines;
MagazineIssueViewModel _model;
bool _unknownIssueNumber;
bool _unknownNativeCaption;
bool _unknownPages;
bool _unknownProductCode;
bool _unknownPublished;
bool _addingMachineFamily;
int? _addingMachineFamilyId;
AuthenticationState _authState;
bool _creating;
MagazineByMachineFamilyViewModel _currentMagazineByMachineFamily;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingMagazineByMachineFamily;
bool _editing;
Modal _frmDelete;
bool _loaded;
List<MachineFamilyViewModel> _machineFamilies;
List<MagazineByMachineFamilyViewModel> _magazineMachineFamilies;
List<MagazineViewModel> _magazines;
MagazineIssueViewModel _model;
bool _savingMachineFamily;
bool _unknownIssueNumber;
bool _unknownNativeCaption;
bool _unknownPages;
bool _unknownProductCode;
bool _unknownPublished;
[Parameter]
public long Id { get; set; }
@@ -65,9 +77,12 @@ namespace Marechai.Pages.Admin.Details
!_creating)
return;
_magazines = await MagazinesService.GetTitlesAsync();
_model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id);
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_magazines = await MagazinesService.GetTitlesAsync();
_machineFamilies = await MachineFamiliesService.GetAsync();
_model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id);
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
_addingMachineFamilyId = _machineFamilies.First().Id;
_magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/magazine_issues/edit/",
@@ -168,5 +183,103 @@ namespace Marechai.Pages.Admin.Details
void ValidateProductCode(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Product code must be smaller than 18 characters."], 18);
void ModalClosing(ModalClosingEventArgs obj)
{
_deleteInProgress = false;
_deletingMagazineByMachineFamily = false;
_currentMagazineByMachineFamily = null;
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_deletingMagazineByMachineFamily)
await ConfirmDeleteMagazineByMachineFamily();
}
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 MagazinesByMachineFamilyService.CreateAsync(_addingMachineFamilyId.Value, Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(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)
{
_currentMagazineByMachineFamily = _magazineMachineFamilies.FirstOrDefault(n => n.Id == itemId);
_deletingMagazineByMachineFamily = true;
_deleteTitle = L["Delete machine family from this magazine"];
_deleteText =
string.Format(L["Are you sure you want to delete the machine family {0} from this magazine issue?"],
_currentMagazineByMachineFamily?.MachineFamily);
_frmDelete.Show();
}
async Task ConfirmDeleteMagazineByMachineFamily()
{
if(_currentMagazineByMachineFamily is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await MagazinesByMachineFamilyService.DeleteAsync(_currentMagazineByMachineFamily.Id,
(await UserManager.GetUserAsync(_authState.User)).Id);
_magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
}
}

View File

@@ -119,4 +119,13 @@
<data name="Native caption must be smaller than 256 characters." xml:space="preserve">
<value>Native caption must be smaller than 256 characters.</value>
</data>
<data name="Machine families this magazine issue talks about" xml:space="preserve">
<value>Machine families this magazine issue 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

@@ -226,4 +226,13 @@
<data name="Product code must be smaller than 18 characters." xml:space="preserve">
<value>El código de producto debe contener menos de 18 caracteres.</value>
</data>
<data name="Machine families this magazine issue talks about" xml:space="preserve">
<value>Familias de máquinas sobre las que habla este número de revista</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>
</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 MagazinesByMachineFamilyService
{
readonly MarechaiContext _context;
public MagazinesByMachineFamilyService(MarechaiContext context) => _context = context;
public async Task<List<MagazineByMachineFamilyViewModel>> GetByMagazine(long bookId) =>
await _context.MagazinesByMachinesFamilies.Where(p => p.MagazineId == bookId).
Select(p => new MagazineByMachineFamilyViewModel
{
Id = p.Id,
MagazineId = p.MagazineId,
MachineFamilyId = p.MachineFamilyId,
MachineFamily = p.MachineFamily.Name
}).OrderBy(p => p.MachineFamily).ThenBy(p => p.Magazine).ToListAsync();
public async Task DeleteAsync(long id, string userId)
{
MagazinesByMachineFamily item = await _context.MagazinesByMachinesFamilies.FindAsync(id);
if(item is null)
return;
_context.MagazinesByMachinesFamilies.Remove(item);
await _context.SaveChangesWithUserAsync(userId);
}
public async Task<long> CreateAsync(int machineFamilyId, long bookId, string userId)
{
var item = new MagazinesByMachineFamily
{
MachineFamilyId = machineFamilyId,
MagazineId = bookId
};
await _context.MagazinesByMachinesFamilies.AddAsync(item);
await _context.SaveChangesWithUserAsync(userId);
return item.Id;
}
}
}

View File

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

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 MagazineByMachineFamilyViewModel : BaseViewModel<long>
{
public long MagazineId { get; set; }
public string Magazine { get; set; }
public int MachineFamilyId { get; set; }
public string MachineFamily { get; set; }
}
}