diff --git a/Marechai/Pages/Admin/Details/Document.razor b/Marechai/Pages/Admin/Details/Document.razor index d54a768e..51db87ab 100644 --- a/Marechai/Pages/Admin/Details/Document.razor +++ b/Marechai/Pages/Admin/Details/Document.razor @@ -33,6 +33,9 @@ @inherits OwningComponentBase @inject IStringLocalizer L @inject Iso31661NumericService CountriesService +@inject DocumentCompaniesService CompaniesService +@inject DocumentRolesService DocumentRolesService +@inject CompaniesByDocumentService CompaniesByDocumentService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -136,3 +139,85 @@ } @L["Back to list"] +@if (!_editing) +{ +
+

@L["Companies involved in this document"]

+ + @if (_addingCompany) + { +
+ + @L["Company"] + + + + @L["Role"] + + + + +
+ } + @if (_documentCompanies?.Count > 0) + { +
+ + + + + + + + + + @foreach (var item in _documentCompanies) + { + + + + + + } + +
+ @L["Company"] + + @L["Role"] +
+ @item.Company + + @item.Role + + +
+
+ } + + + + + + @_deleteTitle + + + + @_deleteText + + + + + + + +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Document.razor.cs b/Marechai/Pages/Admin/Details/Document.razor.cs index 54b37310..34761469 100644 --- a/Marechai/Pages/Admin/Details/Document.razor.cs +++ b/Marechai/Pages/Admin/Details/Document.razor.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Blazorise; using Marechai.Database.Models; @@ -37,15 +38,28 @@ namespace Marechai.Pages.Admin.Details { public partial class Document { - AuthenticationState _authState; - List _countries; - bool _creating; - bool _editing; - bool _loaded; - DocumentViewModel _model; - bool _unknownCountry; - bool _unknownNativeTitle; - bool _unknownPublished; + 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; [Parameter] public long Id { get; set; } @@ -64,9 +78,12 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _countries = await CountriesService.GetAsync(); - _model = _creating ? new DocumentViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _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; _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/documents/edit/", @@ -147,5 +164,103 @@ namespace Marechai.Pages.Admin.Details void ValidateNativeTitle(ValidatorEventArgs e) => Validators.ValidateString(e, L["Native title must be smaller than 256 characters."], 256); + + void OnAddCompanyClick() + { + _addingCompany = true; + _savingCompany = false; + _addingCompanyId = _companies.First().Id; + } + + void CancelAddCpu() + { + _addingCompany = false; + _savingCompany = false; + _addingCompanyId = null; + } + + async Task ConfirmAddCpu() + { + if(_addingCompanyId is null || + _addingCompanyId <= 0) + { + CancelAddCpu(); + + return; + } + + _savingCompany = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesByDocumentService.CreateAsync(_addingCompanyId.Value, Id, _addingCompanyRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _documentCompanies = await CompaniesByDocumentService.GetByDocument(Id); + + _addingCompany = false; + _savingCompany = false; + _addingCompanyId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowCpuDeleteModal(long itemId) + { + _currentCompanyByDocument = _documentCompanies.FirstOrDefault(n => n.Id == itemId); + _deletingCompanyByDocument = true; + _deleteTitle = L["Delete company from this document"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the company {0} with role {1} from this document?"], + _currentCompanyByDocument?.Company, _currentCompanyByDocument?.Role); + + _frmDelete.Show(); + } + + void ModalClosing(ModalClosingEventArgs obj) + { + _deleteInProgress = false; + _deletingCompanyByDocument = false; + _currentCompanyByDocument = null; + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_deletingCompanyByDocument) + await ConfirmDeleteCpuByMachine(); + } + + async Task ConfirmDeleteCpuByMachine() + { + if(_currentCompanyByDocument is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesByDocumentService.DeleteAsync(_currentCompanyByDocument.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _documentCompanies = await CompaniesByDocumentService.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 a756bc5a..c0e80ac9 100644 --- a/Marechai/Resources/Services/DocumentsService.en.resx +++ b/Marechai/Resources/Services/DocumentsService.en.resx @@ -92,4 +92,25 @@ Unknown + + Companies involved in this document + + + Add new + + + Company + + + Role + + + Add + + + Delete company from this document + + + Are you sure you want to delete the company {0} with role {1} from this document? + \ No newline at end of file diff --git a/Marechai/Resources/Services/DocumentsService.es.resx b/Marechai/Resources/Services/DocumentsService.es.resx index dae1cc0b..8882ac6a 100644 --- a/Marechai/Resources/Services/DocumentsService.es.resx +++ b/Marechai/Resources/Services/DocumentsService.es.resx @@ -147,7 +147,7 @@ Delete document - ¿Está seguro de que desea borrar el libro {0}? + ¿Estás seguro de eliminar el libro {0}? {0} document name @@ -208,4 +208,25 @@ El título nativo debe contener menos de 256 caracteres. + + Añadir + + + Añadir nueva + + + ¿Estás seguro de eliminar la compañía {0} con rol {1} de este documento? + + + Compañías involucradas en este documento + + + Compañía + + + Eliminar compañía de este documento + + + Rol + \ No newline at end of file diff --git a/Marechai/Services/CompaniesByDocumentService.cs b/Marechai/Services/CompaniesByDocumentService.cs new file mode 100644 index 00000000..a55ad562 --- /dev/null +++ b/Marechai/Services/CompaniesByDocumentService.cs @@ -0,0 +1,80 @@ +/****************************************************************************** +// 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 CompaniesByDocumentService + { + readonly MarechaiContext _context; + + public CompaniesByDocumentService(MarechaiContext context) => _context = context; + + public async Task> GetByDocument(long documentId) => + await _context.CompaniesByDocuments.Where(p => p.DocumentId == documentId). + Select(p => new CompanyByDocumentViewModel + { + Id = p.Id, + Company = p.Company.Name, + CompanyId = p.CompanyId, + RoleId = p.RoleId, + Role = p.Role.Name, + DocumentId = p.DocumentId + }).OrderBy(p => p.Company).ThenBy(p => p.Role).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + CompaniesByDocument item = await _context.CompaniesByDocuments.FindAsync(id); + + if(item is null) + return; + + _context.CompaniesByDocuments.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int companyId, long documentId, string roleId, string userId) + { + var item = new CompaniesByDocument + { + CompanyId = companyId, + DocumentId = documentId, + RoleId = roleId + }; + + await _context.CompaniesByDocuments.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/CompanyByDocumentViewModel.cs b/Marechai/ViewModels/CompanyByDocumentViewModel.cs new file mode 100644 index 00000000..e6ef37a1 --- /dev/null +++ b/Marechai/ViewModels/CompanyByDocumentViewModel.cs @@ -0,0 +1,36 @@ +/****************************************************************************** +// 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 CompanyByDocumentViewModel : BaseViewModel + { + public int CompanyId { get; set; } + public long DocumentId { get; set; } + public string RoleId { get; set; } + public string Company { get; set; } + public string Role { get; set; } + } +} \ No newline at end of file