mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 11:04:25 +00:00
Add machines to book admin page.
This commit is contained in:
@@ -38,6 +38,8 @@
|
||||
@inject CompaniesByBookService CompaniesByBookService
|
||||
@inject MachineFamiliesService MachineFamiliesService
|
||||
@inject BooksByMachineFamilyService BooksByMachineFamilyService
|
||||
@inject MachinesService MachinesService
|
||||
@inject BooksByMachineService BooksByMachineService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject IWebHostEnvironment Host
|
||||
@inject IJSRuntime JSRuntime
|
||||
@@ -328,6 +330,54 @@
|
||||
</div>
|
||||
}
|
||||
|
||||
<hr />
|
||||
<h3>@L["Machines this book 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 (_bookMachines?.Count > 0)
|
||||
{
|
||||
<div>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
@L["Machine"]
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in _bookMachines)
|
||||
{
|
||||
<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">
|
||||
<ModalBackdrop />
|
||||
<ModalContent Centered="true">
|
||||
|
||||
@@ -41,28 +41,35 @@ namespace Marechai.Pages.Admin.Details
|
||||
bool _addingCompany;
|
||||
int? _addingCompanyId;
|
||||
string _addingCompanyRoleId;
|
||||
bool _addingMachine;
|
||||
bool _addingMachineFamily;
|
||||
int? _addingMachineFamilyId;
|
||||
int? _addingMachineId;
|
||||
AuthenticationState _authState;
|
||||
List<CompanyByBookViewModel> _bookCompanies;
|
||||
List<BookByMachineFamilyViewModel> _bookMachineFamilies;
|
||||
List<BookByMachineViewModel> _bookMachines;
|
||||
List<DocumentCompanyViewModel> _companies;
|
||||
List<Iso31661Numeric> _countries;
|
||||
bool _creating;
|
||||
BookByMachineViewModel _currentBookByMachine;
|
||||
BookByMachineFamilyViewModel _currentBookByMachineFamily;
|
||||
CompanyByBookViewModel _currentCompanyByBook;
|
||||
bool _deleteInProgress;
|
||||
string _deleteText;
|
||||
string _deleteTitle;
|
||||
bool _deletingBookByMachine;
|
||||
bool _deletingBookByMachineFamily;
|
||||
bool _deletingCompanyByBook;
|
||||
bool _editing;
|
||||
Modal _frmDelete;
|
||||
bool _loaded;
|
||||
List<MachineFamilyViewModel> _machineFamilies;
|
||||
List<MachineViewModel> _machines;
|
||||
BookViewModel _model;
|
||||
List<DocumentRoleViewModel> _roles;
|
||||
bool _savingCompany;
|
||||
bool _savingMachine;
|
||||
bool _savingMachineFamily;
|
||||
bool _unknownCountry;
|
||||
bool _unknownEdition;
|
||||
@@ -92,12 +99,15 @@ namespace Marechai.Pages.Admin.Details
|
||||
_companies = await CompaniesService.GetAsync();
|
||||
_roles = await DocumentRolesService.GetEnabledAsync();
|
||||
_machineFamilies = await MachineFamiliesService.GetAsync();
|
||||
_machines = await MachinesService.GetAsync();
|
||||
_model = _creating ? new BookViewModel() : await Service.GetAsync(Id);
|
||||
_authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
_addingCompanyRoleId = _roles.First().Id;
|
||||
_bookCompanies = await CompaniesByBookService.GetByBook(Id);
|
||||
_addingMachineFamilyId = _machineFamilies.First().Id;
|
||||
_bookMachineFamilies = await BooksByMachineFamilyService.GetByBook(Id);
|
||||
_addingMachineId = _machines.First().Id;
|
||||
_bookMachines = await BooksByMachineService.GetByBook(Id);
|
||||
|
||||
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/books/edit/",
|
||||
@@ -291,7 +301,9 @@ namespace Marechai.Pages.Admin.Details
|
||||
_deletingCompanyByBook = false;
|
||||
_currentCompanyByBook = null;
|
||||
_deletingBookByMachineFamily = false;
|
||||
_currentCompanyByBook = null;
|
||||
_currentBookByMachineFamily = null;
|
||||
_deletingBookByMachine = false;
|
||||
_currentBookByMachine = null;
|
||||
}
|
||||
|
||||
void HideModal() => _frmDelete.Hide();
|
||||
@@ -302,6 +314,8 @@ namespace Marechai.Pages.Admin.Details
|
||||
await ConfirmDeleteCompanyByBook();
|
||||
else if(_deletingBookByMachineFamily)
|
||||
await ConfirmDeleteBookByMachineFamily();
|
||||
else if(_deletingBookByMachine)
|
||||
await ConfirmDeleteBookByMachine();
|
||||
}
|
||||
|
||||
async Task ConfirmDeleteCompanyByBook()
|
||||
@@ -410,5 +424,87 @@ namespace Marechai.Pages.Admin.Details
|
||||
// Tell we finished loading
|
||||
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 BooksByMachineService.CreateAsync(_addingMachineId.Value, Id,
|
||||
(await UserManager.GetUserAsync(_authState.User)).Id);
|
||||
|
||||
_bookMachines = await BooksByMachineService.GetByBook(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)
|
||||
{
|
||||
_currentBookByMachine = _bookMachines.FirstOrDefault(n => n.Id == itemId);
|
||||
_deletingBookByMachine = true;
|
||||
_deleteTitle = L["Delete machine from this book"];
|
||||
|
||||
_deleteText = string.Format(L["Are you sure you want to delete the machine {0} from this book?"],
|
||||
_currentBookByMachine?.Machine);
|
||||
|
||||
_frmDelete.Show();
|
||||
}
|
||||
|
||||
async Task ConfirmDeleteBookByMachine()
|
||||
{
|
||||
if(_currentBookByMachine is null)
|
||||
return;
|
||||
|
||||
_deleteInProgress = true;
|
||||
|
||||
// Yield thread to let UI to update
|
||||
await Task.Yield();
|
||||
|
||||
await BooksByMachineService.DeleteAsync(_currentBookByMachine.Id,
|
||||
(await UserManager.GetUserAsync(_authState.User)).Id);
|
||||
|
||||
_bookMachines = await BooksByMachineService.GetByBook(Id);
|
||||
|
||||
_deleteInProgress = false;
|
||||
_frmDelete.Hide();
|
||||
|
||||
// Yield thread to let UI to update
|
||||
await Task.Yield();
|
||||
|
||||
// Tell we finished loading
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,4 +146,25 @@
|
||||
<data name="Family" xml:space="preserve">
|
||||
<value>Family</value>
|
||||
</data>
|
||||
<data name="Delete machine family from this book" xml:space="preserve">
|
||||
<value>Delete machine family from this book</value>
|
||||
</data>
|
||||
<data name="Are you sure you want to delete the machine family {0} from this book?" xml:space="preserve">
|
||||
<value>Are you sure you want to delete the machine family {0} from this book?</value>
|
||||
</data>
|
||||
<data name="Delete machine from this book" xml:space="preserve">
|
||||
<value>Delete machine from this book</value>
|
||||
</data>
|
||||
<data name="Are you sure you want to delete the machine {0} from this book?" xml:space="preserve">
|
||||
<value>Are you sure you want to delete the machine {0} from this book?</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 book talks about" xml:space="preserve">
|
||||
<value>Machines this book talks about</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -262,4 +262,25 @@
|
||||
<data name="Family" xml:space="preserve">
|
||||
<value>Familia</value>
|
||||
</data>
|
||||
<data name="Delete machine family from this book" xml:space="preserve">
|
||||
<value>Eliminar familia de máquinas de este libro</value>
|
||||
</data>
|
||||
<data name="Are you sure you want to delete the machine family {0} from this book?" xml:space="preserve">
|
||||
<value>¿Estás seguro de eliminar la familia de máquinas {0} de este libro?</value>
|
||||
</data>
|
||||
<data name="Delete machine from this book" xml:space="preserve">
|
||||
<value>Eliminar máquina de este libro</value>
|
||||
</data>
|
||||
<data name="Are you sure you want to delete the machine {0} from this book?" xml:space="preserve">
|
||||
<value>¿Estás seguro de eliminar la máquina {0} de este libro?</value>
|
||||
</data>
|
||||
<data name="Machine" xml:space="preserve">
|
||||
<value>Máquina</value>
|
||||
</data>
|
||||
<data name="Add new (machine)" xml:space="preserve">
|
||||
<value>Añadir nueva</value>
|
||||
</data>
|
||||
<data name="Machines this book talks about" xml:space="preserve">
|
||||
<value>Máquinas sobre las que habla este libro</value>
|
||||
</data>
|
||||
</root>
|
||||
76
Marechai/Services/BooksByMachineService.cs
Normal file
76
Marechai/Services/BooksByMachineService.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
/******************************************************************************
|
||||
// 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 BooksByMachineService
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public BooksByMachineService(MarechaiContext context) => _context = context;
|
||||
|
||||
public async Task<List<BookByMachineViewModel>> GetByBook(long bookId) =>
|
||||
await _context.BooksByMachines.Where(p => p.BookId == bookId).Select(p => new BookByMachineViewModel
|
||||
{
|
||||
Id = p.Id,
|
||||
BookId = p.BookId,
|
||||
MachineId = p.MachineId,
|
||||
Machine = p.Machine.Name
|
||||
}).OrderBy(p => p.Machine).ThenBy(p => p.Book).ToListAsync();
|
||||
|
||||
public async Task DeleteAsync(long id, string userId)
|
||||
{
|
||||
BooksByMachine item = await _context.BooksByMachines.FindAsync(id);
|
||||
|
||||
if(item is null)
|
||||
return;
|
||||
|
||||
_context.BooksByMachines.Remove(item);
|
||||
|
||||
await _context.SaveChangesWithUserAsync(userId);
|
||||
}
|
||||
|
||||
public async Task<long> CreateAsync(int machineId, long bookId, string userId)
|
||||
{
|
||||
var item = new BooksByMachine
|
||||
{
|
||||
MachineId = machineId,
|
||||
BookId = bookId
|
||||
};
|
||||
|
||||
await _context.BooksByMachines.AddAsync(item);
|
||||
await _context.SaveChangesWithUserAsync(userId);
|
||||
|
||||
return item.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,7 @@ namespace Marechai.Services
|
||||
services.AddScoped<BooksByMachineFamilyService>();
|
||||
services.AddScoped<DocumentsByMachineFamilyService>();
|
||||
services.AddScoped<MagazinesByMachineFamilyService>();
|
||||
services.AddScoped<BooksByMachineService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Marechai/ViewModels/BookByMachineViewModel.cs
Normal file
35
Marechai/ViewModels/BookByMachineViewModel.cs
Normal 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 BookByMachineViewModel : BaseViewModel<long>
|
||||
{
|
||||
public long BookId { get; set; }
|
||||
public string Book { get; set; }
|
||||
public int MachineId { get; set; }
|
||||
public string Machine { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user