Add companies to software variant admin page.

This commit is contained in:
2020-08-09 17:05:44 +01:00
parent 644dbdb1dc
commit 5d8e345447
7 changed files with 384 additions and 23 deletions

View File

@@ -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; }
}
}

View File

@@ -34,6 +34,9 @@
@inject IStringLocalizer<SoftwareVariantsService> 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 @@
}
<a href="/admin/software_variants" class="btn btn-secondary">@L["Back to list"]</a>
</div>
@if (!_editing)
{
<hr />
<h3>@L["Companies involved in this software variant"]</h3>
<Button Color="Color.Success" Clicked="OnAddCompanyClick" Disabled="_addingCompany">@L["Add new (company)"]</Button>
@if (_addingCompany)
{
<div>
<Field>
<FieldLabel>@L["Company"]</FieldLabel>
<Select Disabled="_savingCompany" TValue="int?" @bind-SelectedValue="@_addingCompanyId">
@foreach (var company in _companies)
{
<SelectItem TValue="int?" Value="@company.Id">@company.Name</SelectItem>
}
</Select>
</Field>
<Field>
<FieldLabel>@L["Role"]</FieldLabel>
<Select Disabled="!_editing" TValue="string" @bind-SelectedValue="@_addingCompanyRoleId">
@foreach (var role in _roles)
{
<SelectItem TValue="string" Value="@role.Id">@role.Name</SelectItem>
}
</Select>
</Field>
<Button Color="Color.Primary" Clicked="@CancelAddCpu" Disabled="@_savingCompany">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddCpu" Disabled="@_savingCompany">@L["Add"]</Button>
</div>
}
@if (_softwareVariantCompanies?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Company"]
</th>
<th>
@L["Role"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _softwareVariantCompanies)
{
<tr>
<td>
@item.Company
</td>
<td>
@item.Role
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowCpuDeleteModal(item.Id);}" Disabled="@_addingCompany">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop />
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@_deleteTitle</ModalTitle>
<CloseButton Clicked="@HideModal" />
</ModalHeader>
<ModalBody>
<Text>@_deleteText</Text>
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
</ModalFooter>
</ModalContent>
</Modal>
}

View File

@@ -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<SoftwareVariantViewModel> _softwareVariants;
List<SoftwareVersionViewModel> _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<CompanyViewModel> _companies;
bool _creating;
CompanyBySoftwareVariantViewModel _currentCompanyBySoftwareVariant;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingCompanyBySoftwareVariant;
bool _editing;
Modal _frmDelete;
bool _loaded;
SoftwareVariantViewModel _model;
List<DocumentRoleViewModel> _roles;
bool _savingCompany;
List<CompanyBySoftwareVariantViewModel> _softwareVariantCompanies;
List<SoftwareVariantViewModel> _softwareVariants;
List<SoftwareVersionViewModel> _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();
}
}
}

View File

@@ -179,4 +179,25 @@
<data name="Software variants" xml:space="preserve">
<value>Software variants</value>
</data>
<data name="Companies involved in this software variant" xml:space="preserve">
<value>Companies involved in this software variant</value>
</data>
<data name="Add new (company)" xml:space="preserve">
<value>Add new</value>
</data>
<data name="Delete company from this software variant" xml:space="preserve">
<value>Delete company from this software variant</value>
</data>
<data name="Are you sure you want to delete the company {0} with role {1} from this software variant?" xml:space="preserve">
<value>Are you sure you want to delete the company {0} with role {1} from this software variant?</value>
</data>
<data name="Add" xml:space="preserve">
<value>Add</value>
</data>
<data name="Company" xml:space="preserve">
<value>Company</value>
</data>
<data name="Role" xml:space="preserve">
<value>Role</value>
</data>
</root>

View File

@@ -286,4 +286,25 @@
<data name="Software variants" xml:space="preserve">
<value>Variantes de software</value>
</data>
<data name="Companies involved in this software variant" xml:space="preserve">
<value>Compañías involucradas en esta variante de software</value>
</data>
<data name="Add new (company)" xml:space="preserve">
<value>Añadir nueva</value>
</data>
<data name="Company" xml:space="preserve">
<value>Compañía</value>
</data>
<data name="Role" xml:space="preserve">
<value>Rol</value>
</data>
<data name="Add" xml:space="preserve">
<value>Añadir</value>
</data>
<data name="Delete company from this software variant" xml:space="preserve">
<value>Eliminar compañía de esta variante de software</value>
</data>
<data name="Are you sure you want to delete the company {0} with role {1} from this software variant?" xml:space="preserve">
<value>¿Estás seguro de eliminar la compañía {0} con rol {1} de esta variante de software?</value>
</data>
</root>

View File

@@ -0,0 +1,80 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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<List<CompanyBySoftwareVariantViewModel>> 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<ulong> 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;
}
}
}

View File

@@ -0,0 +1,36 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
namespace Marechai.ViewModels
{
public class CompanyBySoftwareVariantViewModel : BaseViewModel<ulong>
{
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; }
}
}