Files
gwms/GWMS.UI/Pages/PlantSetup.razor.cs
T
2021-09-29 16:42:51 +02:00

170 lines
4.5 KiB
C#

using GWMS.Data.DTO;
using GWMS.UI.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GWMS.UI.Pages
{
[Authorize(Roles = "SuperAdmin, Admin")]
public partial class PlantSetup : ComponentBase, IDisposable
{
#region Private Fields
private PlantDTO currRecord = null;
private List<PlantDTO> ListRecords;
#endregion Private Fields
#region Protected Fields
/// <summary>
/// Valore PlantId filtrato da claim
/// </summary>
protected int ClaimPlantId = -1;
#endregion Protected Fields
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; }
[Inject]
protected AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject]
protected GWMSDataService DataService { get; set; }
[Inject]
protected IJSRuntime JSRuntime { get; set; }
protected int maxHourRate { get; set; } = 800;
protected int numDays { get; set; } = 7;
protected int stepMin { get; set; } = 30;
protected int totalCount
{
get
{
int answ = 0;
if (ListRecords != null)
{
answ = ListRecords.Count;
}
return answ;
}
}
#endregion Protected Properties
#region Private Methods
/// <summary>
/// Recupero Claims dell'utente...
///
/// https://docs.microsoft.com/it-it/aspnet/core/blazor/security/?view=aspnetcore-5.0
/// </summary>
/// <returns></returns>
private async Task GetClaimsData()
{
// recupero auth
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
// se autenticato --> controllo i claims
if (user.Identity.IsAuthenticated)
{
// cerco il claim PlantId...
var plantClaim = user.FindFirst(c => c.Type == "PlantId")?.Value;
int.TryParse(plantClaim, out ClaimPlantId);
}
else
{
ClaimPlantId = -1;
}
}
private async Task ReloadAllData()
{
ListRecords = null;
await GetClaimsData();
// se ho un plantId valido --> altrimenti non abilitato
if (ClaimPlantId == 0)
{
ListRecords = await DataService.PlantsGetAll();
}
else if (ClaimPlantId > 0)
{
var rawData = await DataService.PlantsGetAll();
ListRecords = rawData.Where(x => x.PlantId == ClaimPlantId).ToList();
}
else
{
ListRecords = new List<PlantDTO>();
}
}
#endregion Private Methods
#region Protected Methods
protected void Edit(PlantDTO selRecord)
{
// rileggo dal DB il record corrente...
var pUpd = Task.Run(async () => currRecord = await DataService.PlantsGetByCode(selRecord.PlantCode));
pUpd.Wait();
}
protected override async Task OnInitializedAsync()
{
AppMService.ShowSearch = false;
AppMService.PageName = "Setup Impianti";
AppMService.PageIcon = "fas fa-wrench pr-2";
await ReloadAllData();
}
protected void ResetData()
{
DataService.rollBackEdit(currRecord);
currRecord = null;
}
protected async Task UpdateData()
{
currRecord = null;
await DataService.InvalidateAllCache();
await ReloadAllData();
}
#endregion Protected Methods
#region Public Methods
public string checkSelect(int PlantId)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.PlantId == PlantId) ? "table-info" : "";
}
catch
{ }
}
return answ;
}
public void Dispose()
{
}
#endregion Public Methods
}
}