From 5d8e34544743ee355f1b4408962320a9e0654cde Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 9 Aug 2020 17:05:44 +0100 Subject: [PATCH] Add companies to software variant admin page. --- .../Models/CompaniesBySoftwareVariant.cs | 4 + .../Pages/Admin/Details/SoftwareVariant.razor | 85 ++++++++++ .../Admin/Details/SoftwareVariant.razor.cs | 160 +++++++++++++++--- .../Services/SoftwareVariantsService.en.resx | 21 +++ .../Services/SoftwareVariantsService.es.resx | 21 +++ .../CompaniesBySoftwareVariantService.cs | 80 +++++++++ .../CompanyBySoftwareVariantViewModel.cs | 36 ++++ 7 files changed, 384 insertions(+), 23 deletions(-) create mode 100644 Marechai/Services/CompaniesBySoftwareVariantService.cs create mode 100644 Marechai/ViewModels/CompanyBySoftwareVariantViewModel.cs diff --git a/Marechai.Database/Models/CompaniesBySoftwareVariant.cs b/Marechai.Database/Models/CompaniesBySoftwareVariant.cs index 7ee8cd79..5c8a53be 100644 --- a/Marechai.Database/Models/CompaniesBySoftwareVariant.cs +++ b/Marechai.Database/Models/CompaniesBySoftwareVariant.cs @@ -35,5 +35,9 @@ namespace Marechai.Database.Models public virtual SoftwareVariant SoftwareVariant { get; set; } [Required] public virtual DocumentRole Role { get; set; } + + public string RoleId { get; set; } + public int CompanyId { get; set; } + public ulong SoftwareVariantId { get; set; } } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/SoftwareVariant.razor b/Marechai/Pages/Admin/Details/SoftwareVariant.razor index 73f4f5f0..6b5cb2a4 100644 --- a/Marechai/Pages/Admin/Details/SoftwareVariant.razor +++ b/Marechai/Pages/Admin/Details/SoftwareVariant.razor @@ -34,6 +34,9 @@ @inject IStringLocalizer L @inject SoftwareVersionsService SoftwareVersionsService @inject LicensesService LicensesService +@inject CompaniesService CompaniesService +@inject DocumentRolesService DocumentRolesService +@inject CompaniesBySoftwareVariantService CompaniesBySoftwareVariantService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -317,3 +320,85 @@ } @L["Back to list"] +@if (!_editing) +{ +
+

@L["Companies involved in this software variant"]

+ + @if (_addingCompany) + { +
+ + @L["Company"] + + + + @L["Role"] + + + + +
+ } + @if (_softwareVariantCompanies?.Count > 0) + { +
+ + + + + + + + + + @foreach (var item in _softwareVariantCompanies) + { + + + + + + } + +
+ @L["Company"] + + @L["Role"] +
+ @item.Company + + @item.Role + + +
+
+ } + + + + + + @_deleteTitle + + + + @_deleteText + + + + + + + +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/SoftwareVariant.razor.cs b/Marechai/Pages/Admin/Details/SoftwareVariant.razor.cs index 7b5dac01..7e85aa18 100644 --- a/Marechai/Pages/Admin/Details/SoftwareVariant.razor.cs +++ b/Marechai/Pages/Admin/Details/SoftwareVariant.razor.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Blazorise; using Marechai.Database; @@ -37,25 +38,37 @@ namespace Marechai.Pages.Admin.Details { public partial class SoftwareVariant { - AuthenticationState _authState; - bool _creating; - bool _editing; - bool _loaded; - SoftwareVariantViewModel _model; - List _softwareVariants; - List _softwareVersions; - bool _unknownCatalogueNumber; - bool _unknownIntroduced; - bool _unknownMinimumMemory; - bool _unknownName; - bool _unknownParent; - bool _unknownPartNumber; - bool _unknownProductCode; - bool _unknownRecommendedMemory; - bool _unknownRequiredStorage; - - bool _unknownSerialNumber; - bool _unknownVersion; + bool _addingCompany; + int? _addingCompanyId; + string _addingCompanyRoleId; + AuthenticationState _authState; + List _companies; + bool _creating; + CompanyBySoftwareVariantViewModel _currentCompanyBySoftwareVariant; + bool _deleteInProgress; + string _deleteText; + string _deleteTitle; + bool _deletingCompanyBySoftwareVariant; + bool _editing; + Modal _frmDelete; + bool _loaded; + SoftwareVariantViewModel _model; + List _roles; + bool _savingCompany; + List _softwareVariantCompanies; + List _softwareVariants; + List _softwareVersions; + bool _unknownCatalogueNumber; + bool _unknownIntroduced; + bool _unknownMinimumMemory; + bool _unknownName; + bool _unknownParent; + bool _unknownPartNumber; + bool _unknownProductCode; + bool _unknownRecommendedMemory; + bool _unknownRequiredStorage; + bool _unknownSerialNumber; + bool _unknownVersion; [Parameter] public ulong Id { get; set; } @@ -81,10 +94,13 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _softwareVariants = await Service.GetAsync(); - _softwareVersions = await SoftwareVersionsService.GetAsync(); - _model = _creating ? new SoftwareVariantViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _softwareVariants = await Service.GetAsync(); + _softwareVersions = await SoftwareVersionsService.GetAsync(); + _companies = await CompaniesService.GetAsync(); + _roles = await DocumentRolesService.GetEnabledAsync(); + _model = _creating ? new SoftwareVariantViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingCompanyRoleId = _roles.First().Id; _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/software_variants/edit/", @@ -209,5 +225,103 @@ namespace Marechai.Pages.Admin.Details void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateDate(e); void ValidateUnsignedLongBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateUnsignedLong(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 CompaniesBySoftwareVariantService.CreateAsync(_addingCompanyId.Value, Id, _addingCompanyRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _softwareVariantCompanies = await CompaniesBySoftwareVariantService.GetBySoftwareVariant(Id); + + _addingCompany = false; + _savingCompany = false; + _addingCompanyId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowCpuDeleteModal(ulong itemId) + { + _currentCompanyBySoftwareVariant = _softwareVariantCompanies.FirstOrDefault(n => n.Id == itemId); + _deletingCompanyBySoftwareVariant = true; + _deleteTitle = L["Delete company from this software variant"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the company {0} with role {1} from this software variant?"], + _currentCompanyBySoftwareVariant?.Company, _currentCompanyBySoftwareVariant?.Role); + + _frmDelete.Show(); + } + + void ModalClosing(ModalClosingEventArgs obj) + { + _deleteInProgress = false; + _deletingCompanyBySoftwareVariant = false; + _currentCompanyBySoftwareVariant = null; + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_deletingCompanyBySoftwareVariant) + await ConfirmDeleteCpuByMachine(); + } + + async Task ConfirmDeleteCpuByMachine() + { + if(_currentCompanyBySoftwareVariant is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesBySoftwareVariantService.DeleteAsync(_currentCompanyBySoftwareVariant.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _softwareVariantCompanies = await CompaniesBySoftwareVariantService.GetBySoftwareVariant(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/SoftwareVariantsService.en.resx b/Marechai/Resources/Services/SoftwareVariantsService.en.resx index 17d040cc..a03ce486 100644 --- a/Marechai/Resources/Services/SoftwareVariantsService.en.resx +++ b/Marechai/Resources/Services/SoftwareVariantsService.en.resx @@ -179,4 +179,25 @@ Software variants + + Companies involved in this software variant + + + Add new + + + Delete company from this software variant + + + Are you sure you want to delete the company {0} with role {1} from this software variant? + + + Add + + + Company + + + Role + \ No newline at end of file diff --git a/Marechai/Resources/Services/SoftwareVariantsService.es.resx b/Marechai/Resources/Services/SoftwareVariantsService.es.resx index ef7f6375..c567e83f 100644 --- a/Marechai/Resources/Services/SoftwareVariantsService.es.resx +++ b/Marechai/Resources/Services/SoftwareVariantsService.es.resx @@ -286,4 +286,25 @@ Variantes de software + + Compañías involucradas en esta variante de software + + + Añadir nueva + + + Compañía + + + Rol + + + Añadir + + + Eliminar compañía de esta variante de software + + + ¿Estás seguro de eliminar la compañía {0} con rol {1} de esta variante de software? + \ No newline at end of file diff --git a/Marechai/Services/CompaniesBySoftwareVariantService.cs b/Marechai/Services/CompaniesBySoftwareVariantService.cs new file mode 100644 index 00000000..90b69598 --- /dev/null +++ b/Marechai/Services/CompaniesBySoftwareVariantService.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 CompaniesBySoftwareVariantService + { + readonly MarechaiContext _context; + + public CompaniesBySoftwareVariantService(MarechaiContext context) => _context = context; + + public async Task> GetBySoftwareVariant(ulong softwareVariantId) => + await _context.CompaniesBySoftwareVariants.Where(p => p.SoftwareVariantId == softwareVariantId). + Select(p => new CompanyBySoftwareVariantViewModel + { + Id = p.Id, + Company = p.Company.Name, + CompanyId = p.CompanyId, + RoleId = p.RoleId, + Role = p.Role.Name, + SoftwareVariantId = p.SoftwareVariantId + }).OrderBy(p => p.Company).ThenBy(p => p.Role).ToListAsync(); + + public async Task DeleteAsync(ulong id, string userId) + { + CompaniesBySoftwareVariant item = await _context.CompaniesBySoftwareVariants.FindAsync(id); + + if(item is null) + return; + + _context.CompaniesBySoftwareVariants.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int companyId, ulong softwareVariantId, string roleId, string userId) + { + var item = new CompaniesBySoftwareVariant + { + CompanyId = companyId, + SoftwareVariantId = softwareVariantId, + RoleId = roleId + }; + + await _context.CompaniesBySoftwareVariants.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/CompanyBySoftwareVariantViewModel.cs b/Marechai/ViewModels/CompanyBySoftwareVariantViewModel.cs new file mode 100644 index 00000000..c669e17f --- /dev/null +++ b/Marechai/ViewModels/CompanyBySoftwareVariantViewModel.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 CompanyBySoftwareVariantViewModel : BaseViewModel + { + public int CompanyId { get; set; } + public ulong SoftwareVariantId { get; set; } + public string RoleId { get; set; } + public string Company { get; set; } + public string Role { get; set; } + } +} \ No newline at end of file