From 644dbdb1dc2b03a7d1ad32900f5c3dc149fafcd6 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 9 Aug 2020 17:05:22 +0100 Subject: [PATCH] Add companies to software family admin page. --- .../Models/CompaniesBySoftwareFamily.cs | 4 + .../Pages/Admin/Details/SoftwareFamily.razor | 85 +++++++++++ .../Admin/Details/SoftwareFamily.razor.cs | 137 ++++++++++++++++-- .../Services/SoftwareFamiliesService.en.resx | 21 +++ .../Services/SoftwareFamiliesService.es.resx | 21 +++ .../CompaniesBySoftwareFamilyService.cs | 80 ++++++++++ .../CompanyBySoftwareFamilyViewModel.cs | 36 +++++ 7 files changed, 373 insertions(+), 11 deletions(-) create mode 100644 Marechai/Services/CompaniesBySoftwareFamilyService.cs create mode 100644 Marechai/ViewModels/CompanyBySoftwareFamilyViewModel.cs diff --git a/Marechai.Database/Models/CompaniesBySoftwareFamily.cs b/Marechai.Database/Models/CompaniesBySoftwareFamily.cs index a7c11edb..19bab5e5 100644 --- a/Marechai.Database/Models/CompaniesBySoftwareFamily.cs +++ b/Marechai.Database/Models/CompaniesBySoftwareFamily.cs @@ -35,5 +35,9 @@ namespace Marechai.Database.Models public virtual Company Company { get; set; } [Required] public virtual SoftwareFamily SoftwareFamily { get; set; } + + public string RoleId { get; set; } + public int CompanyId { get; set; } + public ulong SoftwareFamilyId { get; set; } } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/SoftwareFamily.razor b/Marechai/Pages/Admin/Details/SoftwareFamily.razor index 1175a9be..9f3523b0 100644 --- a/Marechai/Pages/Admin/Details/SoftwareFamily.razor +++ b/Marechai/Pages/Admin/Details/SoftwareFamily.razor @@ -32,6 +32,9 @@ @using Marechai.Database.Models @inherits OwningComponentBase @inject IStringLocalizer L +@inject CompaniesService CompaniesService +@inject DocumentRolesService DocumentRolesService +@inject CompaniesBySoftwareFamilyService CompaniesBySoftwareFamilyService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -114,3 +117,85 @@ } @L["Back to list"] +@if (!_editing) +{ +
+

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

+ + @if (_addingCompany) + { +
+ + @L["Company"] + + + + @L["Role"] + + + + +
+ } + @if (_softwareFamilyCompanies?.Count > 0) + { +
+ + + + + + + + + + @foreach (var item in _softwareFamilyCompanies) + { + + + + + + } + +
+ @L["Company"] + + @L["Role"] +
+ @item.Company + + @item.Role + + +
+
+ } + + + + + + @_deleteTitle + + + + @_deleteText + + + + + + + +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/SoftwareFamily.razor.cs b/Marechai/Pages/Admin/Details/SoftwareFamily.razor.cs index 98c36a36..f1f5d867 100644 --- a/Marechai/Pages/Admin/Details/SoftwareFamily.razor.cs +++ b/Marechai/Pages/Admin/Details/SoftwareFamily.razor.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Blazorise; using Marechai.Shared; @@ -36,14 +37,27 @@ namespace Marechai.Pages.Admin.Details { public partial class SoftwareFamily { - AuthenticationState _authState; - bool _creating; - bool _editing; - bool _loaded; - SoftwareFamilyViewModel _model; - List _softwareFamilies; - bool _unknownIntroduced; - bool _unknownParent; + bool _addingCompany; + int? _addingCompanyId; + string _addingCompanyRoleId; + AuthenticationState _authState; + List _companies; + bool _creating; + CompanyBySoftwareFamilyViewModel _currentCompanyBySoftwareFamily; + bool _deleteInProgress; + string _deleteText; + string _deleteTitle; + bool _deletingCompanyBySoftwareFamily; + bool _editing; + Modal _frmDelete; + bool _loaded; + SoftwareFamilyViewModel _model; + List _roles; + bool _savingCompany; + List _softwareFamilies; + List _softwareFamilyCompanies; + bool _unknownIntroduced; + bool _unknownParent; [Parameter] public ulong Id { get; set; } @@ -63,9 +77,12 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _softwareFamilies = await Service.GetAsync(); - _model = _creating ? new SoftwareFamilyViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _softwareFamilies = await Service.GetAsync(); + _companies = await CompaniesService.GetAsync(); + _roles = await DocumentRolesService.GetEnabledAsync(); + _model = _creating ? new SoftwareFamilyViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingCompanyRoleId = _roles.First().Id; _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/software_families/edit/", @@ -137,5 +154,103 @@ namespace Marechai.Pages.Admin.Details Validators.ValidateString(e, L["Name must be smaller than 256 characters."], 256); void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateDate(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 CompaniesBySoftwareFamilyService.CreateAsync(_addingCompanyId.Value, Id, _addingCompanyRoleId, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _softwareFamilyCompanies = await CompaniesBySoftwareFamilyService.GetBySoftwareFamily(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) + { + _currentCompanyBySoftwareFamily = _softwareFamilyCompanies.FirstOrDefault(n => n.Id == itemId); + _deletingCompanyBySoftwareFamily = true; + _deleteTitle = L["Delete company from this software family"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the company {0} with role {1} from this software family?"], + _currentCompanyBySoftwareFamily?.Company, _currentCompanyBySoftwareFamily?.Role); + + _frmDelete.Show(); + } + + void ModalClosing(ModalClosingEventArgs obj) + { + _deleteInProgress = false; + _deletingCompanyBySoftwareFamily = false; + _currentCompanyBySoftwareFamily = null; + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_deletingCompanyBySoftwareFamily) + await ConfirmDeleteCpuByMachine(); + } + + async Task ConfirmDeleteCpuByMachine() + { + if(_currentCompanyBySoftwareFamily is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompaniesBySoftwareFamilyService.DeleteAsync(_currentCompanyBySoftwareFamily.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _softwareFamilyCompanies = await CompaniesBySoftwareFamilyService.GetBySoftwareFamily(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/SoftwareFamiliesService.en.resx b/Marechai/Resources/Services/SoftwareFamiliesService.en.resx index 1a38a338..04ecf1eb 100644 --- a/Marechai/Resources/Services/SoftwareFamiliesService.en.resx +++ b/Marechai/Resources/Services/SoftwareFamiliesService.en.resx @@ -74,4 +74,25 @@ Name must be smaller than 256 characters. + + Companies involved in this software family + + + Add new + + + Company + + + Role + + + Delete company from this software family + + + Are you sure you want to delete the company {0} with role {1} from this software family? + + + Add + \ No newline at end of file diff --git a/Marechai/Resources/Services/SoftwareFamiliesService.es.resx b/Marechai/Resources/Services/SoftwareFamiliesService.es.resx index 907b470a..9dbc2f98 100644 --- a/Marechai/Resources/Services/SoftwareFamiliesService.es.resx +++ b/Marechai/Resources/Services/SoftwareFamiliesService.es.resx @@ -181,4 +181,25 @@ El nombre debe contener menos de 256 caracteres. + + Compañías involucradas en esta familia de software + + + Añadir nueva + + + Compañía + + + Rol + + + Añadir + + + Eliminar compañía de esta familia de software + + + ¿Estás seguro de eliminar la compañía {0} con rol {1} de esta familia de software? + \ No newline at end of file diff --git a/Marechai/Services/CompaniesBySoftwareFamilyService.cs b/Marechai/Services/CompaniesBySoftwareFamilyService.cs new file mode 100644 index 00000000..50042b18 --- /dev/null +++ b/Marechai/Services/CompaniesBySoftwareFamilyService.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 CompaniesBySoftwareFamilyService + { + readonly MarechaiContext _context; + + public CompaniesBySoftwareFamilyService(MarechaiContext context) => _context = context; + + public async Task> GetBySoftwareFamily(ulong softwareFamilyId) => + await _context.CompaniesBySoftwareFamilies.Where(p => p.SoftwareFamilyId == softwareFamilyId). + Select(p => new CompanyBySoftwareFamilyViewModel + { + Id = p.Id, + Company = p.Company.Name, + CompanyId = p.CompanyId, + RoleId = p.RoleId, + Role = p.Role.Name, + SoftwareFamilyId = p.SoftwareFamilyId + }).OrderBy(p => p.Company).ThenBy(p => p.Role).ToListAsync(); + + public async Task DeleteAsync(ulong id, string userId) + { + CompaniesBySoftwareFamily item = await _context.CompaniesBySoftwareFamilies.FindAsync(id); + + if(item is null) + return; + + _context.CompaniesBySoftwareFamilies.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int companyId, ulong softwareFamilyId, string roleId, string userId) + { + var item = new CompaniesBySoftwareFamily + { + CompanyId = companyId, + SoftwareFamilyId = softwareFamilyId, + RoleId = roleId + }; + + await _context.CompaniesBySoftwareFamilies.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/CompanyBySoftwareFamilyViewModel.cs b/Marechai/ViewModels/CompanyBySoftwareFamilyViewModel.cs new file mode 100644 index 00000000..1b20b1b6 --- /dev/null +++ b/Marechai/ViewModels/CompanyBySoftwareFamilyViewModel.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 CompanyBySoftwareFamilyViewModel : BaseViewModel + { + public int CompanyId { get; set; } + public ulong SoftwareFamilyId { get; set; } + public string RoleId { get; set; } + public string Company { get; set; } + public string Role { get; set; } + } +} \ No newline at end of file