diff --git a/Marechai/Pages/Admin/Details/Document.razor b/Marechai/Pages/Admin/Details/Document.razor index 51db87ab..b849c07e 100644 --- a/Marechai/Pages/Admin/Details/Document.razor +++ b/Marechai/Pages/Admin/Details/Document.razor @@ -36,6 +36,8 @@ @inject DocumentCompaniesService CompaniesService @inject DocumentRolesService DocumentRolesService @inject CompaniesByDocumentService CompaniesByDocumentService +@inject MachineFamiliesService MachineFamiliesService +@inject DocumentsByMachineFamilyService DocumentsByMachineFamilyService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -165,8 +167,8 @@ } - - + + } @if (_documentCompanies?.Count > 0) @@ -195,7 +197,55 @@ @item.Role - + + + + } + + + + } + +
+

@L["Machine families this document talks about"]

+ + @if (_addingMachineFamily) + { +
+ + @L["Family"] + + + + +
+ } + @if (_documentMachineFamilies?.Count > 0) + { +
+ + + + + + + + + @foreach (var item in _documentMachineFamilies) + { + + + } diff --git a/Marechai/Pages/Admin/Details/Document.razor.cs b/Marechai/Pages/Admin/Details/Document.razor.cs index c968e38f..daed6fe5 100644 --- a/Marechai/Pages/Admin/Details/Document.razor.cs +++ b/Marechai/Pages/Admin/Details/Document.razor.cs @@ -38,28 +38,35 @@ namespace Marechai.Pages.Admin.Details { public partial class Document { - bool _addingCompany; - int? _addingCompanyId; - string _addingCompanyRoleId; - AuthenticationState _authState; - List _companies; - List _countries; - bool _creating; - CompanyByDocumentViewModel _currentCompanyByDocument; - bool _deleteInProgress; - string _deleteText; - string _deleteTitle; - bool _deletingCompanyByDocument; - List _documentCompanies; - bool _editing; - Modal _frmDelete; - bool _loaded; - DocumentViewModel _model; - List _roles; - bool _savingCompany; - bool _unknownCountry; - bool _unknownNativeTitle; - bool _unknownPublished; + bool _addingCompany; + int? _addingCompanyId; + string _addingCompanyRoleId; + bool _addingMachineFamily; + int? _addingMachineFamilyId; + AuthenticationState _authState; + List _companies; + List _countries; + bool _creating; + CompanyByDocumentViewModel _currentCompanyByDocument; + DocumentByMachineFamilyViewModel _currentDocumentByMachineFamily; + bool _deleteInProgress; + string _deleteText; + string _deleteTitle; + bool _deletingCompanyByDocument; + bool _deletingDocumentByMachineFamily; + List _documentCompanies; + List _documentMachineFamilies; + bool _editing; + Modal _frmDelete; + bool _loaded; + List _machineFamilies; + DocumentViewModel _model; + List _roles; + bool _savingCompany; + bool _savingMachineFamily; + bool _unknownCountry; + bool _unknownNativeTitle; + bool _unknownPublished; [Parameter] public long Id { get; set; } @@ -78,13 +85,16 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _countries = await CountriesService.GetAsync(); - _companies = await CompaniesService.GetAsync(); - _roles = await DocumentRolesService.GetEnabledAsync(); - _model = _creating ? new DocumentViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - _addingCompanyRoleId = _roles.First().Id; - _documentCompanies = await CompaniesByDocumentService.GetByDocument(Id); + _countries = await CountriesService.GetAsync(); + _companies = await CompaniesService.GetAsync(); + _roles = await DocumentRolesService.GetEnabledAsync(); + _machineFamilies = await MachineFamiliesService.GetAsync(); + _model = _creating ? new DocumentViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingCompanyRoleId = _roles.First().Id; + _documentCompanies = await CompaniesByDocumentService.GetByDocument(Id); + _addingMachineFamilyId = _machineFamilies.First().Id; + _documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/documents/edit/", @@ -173,19 +183,19 @@ namespace Marechai.Pages.Admin.Details _addingCompanyId = _companies.First().Id; } - void CancelAddCpu() + void CancelAddCompany() { _addingCompany = false; _savingCompany = false; _addingCompanyId = null; } - async Task ConfirmAddCpu() + async Task ConfirmAddCompany() { if(_addingCompanyId is null || _addingCompanyId <= 0) { - CancelAddCpu(); + CancelAddCompany(); return; } @@ -211,7 +221,7 @@ namespace Marechai.Pages.Admin.Details StateHasChanged(); } - void ShowCpuDeleteModal(long itemId) + void ShowCompanyDeleteModal(long itemId) { _currentCompanyByDocument = _documentCompanies.FirstOrDefault(n => n.Id == itemId); _deletingCompanyByDocument = true; @@ -236,10 +246,12 @@ namespace Marechai.Pages.Admin.Details async void ConfirmDelete() { if(_deletingCompanyByDocument) - await ConfirmDeleteCpuByMachine(); + await ConfirmDeleteCompanyByMachine(); + else if(_deletingDocumentByMachineFamily) + await ConfirmDeleteDocumentByMachineFamily(); } - async Task ConfirmDeleteCpuByMachine() + async Task ConfirmDeleteCompanyByMachine() { if(_currentCompanyByDocument is null) return; @@ -263,5 +275,87 @@ namespace Marechai.Pages.Admin.Details // Tell we finished loading StateHasChanged(); } + + 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 DocumentsByMachineFamilyService.CreateAsync(_addingMachineFamilyId.Value, Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(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) + { + _currentDocumentByMachineFamily = _documentMachineFamilies.FirstOrDefault(n => n.Id == itemId); + _deletingDocumentByMachineFamily = true; + _deleteTitle = L["Delete machine family from this document"]; + + _deleteText = string.Format(L["Are you sure you want to delete the machine family {0} from this document?"], + _currentDocumentByMachineFamily?.MachineFamily); + + _frmDelete.Show(); + } + + async Task ConfirmDeleteDocumentByMachineFamily() + { + if(_currentDocumentByMachineFamily is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await DocumentsByMachineFamilyService.DeleteAsync(_currentDocumentByMachineFamily.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _documentMachineFamilies = await DocumentsByMachineFamilyService.GetByDocument(Id); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } } } \ No newline at end of file diff --git a/Marechai/Resources/Services/DocumentsService.en.resx b/Marechai/Resources/Services/DocumentsService.en.resx index c0e80ac9..31718fe9 100644 --- a/Marechai/Resources/Services/DocumentsService.en.resx +++ b/Marechai/Resources/Services/DocumentsService.en.resx @@ -113,4 +113,13 @@ Are you sure you want to delete the company {0} with role {1} from this document? + + Machine families this document talks about + + + Add new + + + Family + \ No newline at end of file diff --git a/Marechai/Resources/Services/DocumentsService.es.resx b/Marechai/Resources/Services/DocumentsService.es.resx index 8882ac6a..b216652c 100644 --- a/Marechai/Resources/Services/DocumentsService.es.resx +++ b/Marechai/Resources/Services/DocumentsService.es.resx @@ -229,4 +229,13 @@ Rol + + Añadir nueva + + + Familia + + + Familias de máquinas sobre las que habla este documento + \ No newline at end of file diff --git a/Marechai/Services/DocumentsByMachineFamilyService.cs b/Marechai/Services/DocumentsByMachineFamilyService.cs new file mode 100644 index 00000000..48f590bb --- /dev/null +++ b/Marechai/Services/DocumentsByMachineFamilyService.cs @@ -0,0 +1,77 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// 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 DocumentsByMachineFamilyService + { + readonly MarechaiContext _context; + + public DocumentsByMachineFamilyService(MarechaiContext context) => _context = context; + + public async Task> GetByDocument(long bookId) => + await _context.DocumentsByMachineFamilies.Where(p => p.DocumentId == bookId). + Select(p => new DocumentByMachineFamilyViewModel + { + Id = p.Id, + DocumentId = p.DocumentId, + MachineFamilyId = p.MachineFamilyId, + MachineFamily = p.MachineFamily.Name + }).OrderBy(p => p.MachineFamily).ThenBy(p => p.Document).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + DocumentsByMachineFamily item = await _context.DocumentsByMachineFamilies.FindAsync(id); + + if(item is null) + return; + + _context.DocumentsByMachineFamilies.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int machineFamilyId, long bookId, string userId) + { + var item = new DocumentsByMachineFamily + { + MachineFamilyId = machineFamilyId, + DocumentId = bookId + }; + + await _context.DocumentsByMachineFamilies.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/Services/Register.cs b/Marechai/Services/Register.cs index e71808b0..4c491d85 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -86,6 +86,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/DocumentByMachineFamilyViewModel.cs b/Marechai/ViewModels/DocumentByMachineFamilyViewModel.cs new file mode 100644 index 00000000..e95d9143 --- /dev/null +++ b/Marechai/ViewModels/DocumentByMachineFamilyViewModel.cs @@ -0,0 +1,35 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2020 Natalia Portillo +*******************************************************************************/ + +namespace Marechai.ViewModels +{ + public class DocumentByMachineFamilyViewModel : BaseViewModel + { + public long DocumentId { get; set; } + public string Document { get; set; } + public int MachineFamilyId { get; set; } + public string MachineFamily { get; set; } + } +} \ No newline at end of file
+ @L["Family"] +
+ @item.MachineFamily + +