diff --git a/Marechai/Pages/Admin/Details/Magazine.razor b/Marechai/Pages/Admin/Details/Magazine.razor index 8167a4f2..cdbb06e1 100644 --- a/Marechai/Pages/Admin/Details/Magazine.razor +++ b/Marechai/Pages/Admin/Details/Magazine.razor @@ -33,6 +33,9 @@ @inherits OwningComponentBase @inject IStringLocalizer L @inject Iso31661NumericService CountriesService +@inject DocumentCompaniesService CompaniesService +@inject DocumentRolesService DocumentRolesService +@inject CompaniesByMagazineService CompaniesByMagazineService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -157,3 +160,85 @@ } @L["Back to list"] +@if (!_editing) +{ +
+

@L["Companies involved in this magazine"]

+ + @if (_addingCompany) + { +
+ + @L["Company"] + + + + @L["Role"] + + + + +
+ } + @if (_magazineCompanies?.Count > 0) + { +
+ + + + + + + + + + @foreach (var item in _magazineCompanies) + { + + + + + + } + +
+ @L["Company"] + + @L["Role"] +
+ @item.Company + + @item.Role + + +
+
+ } + + + + + + @_deleteTitle + + + + @_deleteText + + + + + + + +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Magazine.razor.cs b/Marechai/Pages/Admin/Details/Magazine.razor.cs index e1a269d7..4ba271bd 100644 --- a/Marechai/Pages/Admin/Details/Magazine.razor.cs +++ b/Marechai/Pages/Admin/Details/Magazine.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,16 +38,29 @@ namespace Marechai.Pages.Admin.Details { public partial class Magazine { - AuthenticationState _authState; - List _countries; - bool _creating; - bool _editing; - bool _loaded; - MagazineViewModel _model; - bool _unknownCountry; - bool _unknownFirstPublication; - bool _unknownIssn; - bool _unknownNativeTitle; + bool _addingCompany; + int? _addingCompanyId; + string _addingCompanyRoleId; + AuthenticationState _authState; + List _companies; + List _countries; + bool _creating; + CompanyByMagazineViewModel _currentCompanyByMagazine; + bool _deleteInProgress; + string _deleteText; + string _deleteTitle; + bool _deletingCompanyByMagazine; + bool _editing; + Modal _frmDelete; + bool _loaded; + List _magazineCompanies; + MagazineViewModel _model; + List _roles; + bool _savingCompany; + bool _unknownCountry; + bool _unknownFirstPublication; + bool _unknownIssn; + bool _unknownNativeTitle; [Parameter] public long Id { get; set; } @@ -65,9 +79,12 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _countries = await CountriesService.GetAsync(); - _model = _creating ? new MagazineViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _countries = await CountriesService.GetAsync(); + _companies = await CompaniesService.GetAsync(); + _roles = await DocumentRolesService.GetEnabledAsync(); + _model = _creating ? new MagazineViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingCompanyRoleId = _roles.First().Id; _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/magazines/edit/", @@ -156,5 +173,103 @@ namespace Marechai.Pages.Admin.Details Validators.ValidateString(e, L["Native title must be smaller than 256 characters."], 256); void ValidateIssn(ValidatorEventArgs e) => Validators.ValidateIssn(e); + + 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 CompaniesByMagazineService.CreateAsync(_addingCompanyId.Value, Id, _addingCompanyRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _magazineCompanies = await CompaniesByMagazineService.GetByMagazine(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) + { + _currentCompanyByMagazine = _magazineCompanies.FirstOrDefault(n => n.Id == itemId); + _deletingCompanyByMagazine = true; + _deleteTitle = L["Delete company from this magazine"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the company {0} with role {1} from this magazine?"], + _currentCompanyByMagazine?.Company, _currentCompanyByMagazine?.Role); + + _frmDelete.Show(); + } + + void ModalClosing(ModalClosingEventArgs obj) + { + _deleteInProgress = false; + _deletingCompanyByMagazine = false; + _currentCompanyByMagazine = null; + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_deletingCompanyByMagazine) + await ConfirmDeleteCpuByMachine(); + } + + async Task ConfirmDeleteCpuByMachine() + { + if(_currentCompanyByMagazine is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesByMagazineService.DeleteAsync(_currentCompanyByMagazine.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _magazineCompanies = await CompaniesByMagazineService.GetByMagazine(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/MagazinesService.en.resx b/Marechai/Resources/Services/MagazinesService.en.resx index 9701f7af..3eace302 100644 --- a/Marechai/Resources/Services/MagazinesService.en.resx +++ b/Marechai/Resources/Services/MagazinesService.en.resx @@ -101,4 +101,25 @@ Magazines + + Companies involved in this magazine + + + Add new + + + Company + + + Role + + + Add + + + Delete company from this magazine + + + Are you sure you want to delete the company {0} with role {1} from this magazine? + \ No newline at end of file diff --git a/Marechai/Resources/Services/MagazinesService.es.resx b/Marechai/Resources/Services/MagazinesService.es.resx index 00cffea7..f19c1a19 100644 --- a/Marechai/Resources/Services/MagazinesService.es.resx +++ b/Marechai/Resources/Services/MagazinesService.es.resx @@ -101,4 +101,25 @@ Desconocido + + Añadir + + + Añadir nueva + + + ¿Estás seguro de eliminar la compañía {0} con rol {1} de esta revista? + + + Compañías involucradas en esta revista + + + Compañía + + + Eliminar compañía de esta revista + + + Rol + \ No newline at end of file diff --git a/Marechai/Services/CompaniesByMagazineService.cs b/Marechai/Services/CompaniesByMagazineService.cs new file mode 100644 index 00000000..40b9c66e --- /dev/null +++ b/Marechai/Services/CompaniesByMagazineService.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 CompaniesByMagazineService + { + readonly MarechaiContext _context; + + public CompaniesByMagazineService(MarechaiContext context) => _context = context; + + public async Task> GetByMagazine(long magazineId) => + await _context.CompaniesByMagazines.Where(p => p.MagazineId == magazineId). + Select(p => new CompanyByMagazineViewModel + { + Id = p.Id, + Company = p.Company.Name, + CompanyId = p.CompanyId, + RoleId = p.RoleId, + Role = p.Role.Name, + MagazineId = p.MagazineId + }).OrderBy(p => p.Company).ThenBy(p => p.Role).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + CompaniesByMagazine item = await _context.CompaniesByMagazines.FindAsync(id); + + if(item is null) + return; + + _context.CompaniesByMagazines.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int companyId, long magazineId, string roleId, string userId) + { + var item = new CompaniesByMagazine + { + CompanyId = companyId, + MagazineId = magazineId, + RoleId = roleId + }; + + await _context.CompaniesByMagazines.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/CompanyByMagazineViewModel.cs b/Marechai/ViewModels/CompanyByMagazineViewModel.cs new file mode 100644 index 00000000..9dad35cb --- /dev/null +++ b/Marechai/ViewModels/CompanyByMagazineViewModel.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 CompanyByMagazineViewModel : BaseViewModel + { + public int CompanyId { get; set; } + public long MagazineId { get; set; } + public string RoleId { get; set; } + public string Company { get; set; } + public string Role { get; set; } + } +} \ No newline at end of file