diff --git a/Core/UserLicenseRequest.cs b/Core/UserLicenseRequest.cs new file mode 100644 index 0000000..068d3e4 --- /dev/null +++ b/Core/UserLicenseRequest.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Core +{ + public class UserLicenseRequest + { + public string MasterKey { get; set; } = ""; + public Dictionary ParamDict { get; set; } = new Dictionary(); + } +} diff --git a/LiMan.Api/Controllers/AttivazioniController.cs b/LiMan.Api/Controllers/AttivazioniController.cs index 0da4d6d..4f1ed08 100644 --- a/LiMan.Api/Controllers/AttivazioniController.cs +++ b/LiMan.Api/Controllers/AttivazioniController.cs @@ -1,4 +1,5 @@ -using LiMan.APi.Data; +using Core; +using LiMan.APi.Data; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using NLog; @@ -39,8 +40,7 @@ namespace LiMan.APi.Controllers /// /// Recupero record di sub licenza (con chiavi anonimizzate) /// - /// Idx licenza MASTER - /// Codice univoco impiego licenza + /// ID Licenza MASTER /// // GET api/attivazioni/5 [HttpGet("{id}")] @@ -53,12 +53,12 @@ namespace LiMan.APi.Controllers /// /// Verifica recupero un record di sub licenza /// - /// Idx licenza MASTER + /// ID Licenza MASTER /// Codice univoco impiego licenza /// - // GET api/attivazioni/5 - [HttpGet("verifica/{id}")] - public async Task VerificaImpiego(int id, string CodImpiego) + // POST api/attivazioni/verifica/5 + [HttpPost("verifica/{id}")] + public async Task VerificaImpiego(int id, [FromBody] string CodImpiego) { var result = await _DataService.AttivazioneSearch(id, CodImpiego, false); return result; @@ -67,17 +67,16 @@ namespace LiMan.APi.Controllers /// /// Richiesta attivazione di una lista di chiavi di licenza sub /// - /// Idx licenza MASTER - /// Elenco codici impiego (key) + valori in formato dizionari + /// Obj Richiesta con licenza MASTER + Elenco codici impiego (key) + valori in formato dizionari // POST api/attivazioni [HttpPost] - public async Task> Post(int id, [FromBody] Dictionary ParamDict) + public async Task> Post([FromBody] UserLicenseRequest CurrRequest) { List currData = new List(); // eseguo tentativo generazione attivazioni da licenza... - bool done = await _DataService.AttivazioniTryAdd(id, ParamDict, 60); - currData = await _DataService.AttivazioniByLic(id, false); + bool done = await _DataService.AttivazioniTryAdd(CurrRequest.MasterKey, CurrRequest.ParamDict, 60); + currData = await _DataService.AttivazioniByMasterKey(CurrRequest.MasterKey, false); return currData; } diff --git a/LiMan.Api/Data/ApiDataService.cs b/LiMan.Api/Data/ApiDataService.cs index 88149ac..f7114bf 100644 --- a/LiMan.Api/Data/ApiDataService.cs +++ b/LiMan.Api/Data/ApiDataService.cs @@ -80,19 +80,19 @@ namespace LiMan.APi.Data /// /// Effettua registrazione (se possibile) delle licenze indicate dall'elenco codici di impiego indicati /// - /// + /// Codice Licenza Master /// Elenco codici impiego (key) + valori in formato dizionari /// Numero giorni x scadenza veto modifica /// /// - public async Task AttivazioniTryAdd(int IdxLic, Dictionary ParamDict, int DayVeto) + public async Task AttivazioniTryAdd(string MasterKey, Dictionary ParamDict, int DayVeto) { bool taskDone = false; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); - taskDone = dbController.AttivazioniTryAdd(IdxLic, ParamDict, DayVeto); + taskDone = dbController.AttivazioniTryAdd(MasterKey, ParamDict, DayVeto); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata scrittura + rilettura da DB per AttivazioniTryAdd: {ts.TotalMilliseconds} ms"); @@ -101,9 +101,9 @@ namespace LiMan.APi.Data } /// - /// Elenco Attivaizoni da licenza master + /// Elenco Attivaizoni da ID Licenza master /// - /// Codice Licenza Master + /// Idx Licenza Master /// Indica se nascondere i dati sensibili /// public async Task> AttivazioniByLic(int IdxLic, bool HideData) @@ -121,10 +121,35 @@ namespace LiMan.APi.Data return await Task.FromResult(dbResult); } + /// + /// Elenco Attivaizoni da valore Licenza master + /// + /// Licenza Master + /// Indica se nascondere i dati sensibili + /// + public async Task> AttivazioniByMasterKey(string MasterKey, bool HideData) + { + List dbResult = new List(); + + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + + var licenza = dbController.GetLicenza(MasterKey); + if (licenza != null) + { + dbResult = dbController.GetAttivazioniByLic(licenza.IdxLic, HideData); + } + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Trace($"Effettuata lettura da DB per AttivazioniByLic: {ts.TotalMilliseconds} ms"); + + return await Task.FromResult(dbResult); + } + /// /// Elenco licenze dato cliente /// - /// Codice Licenza Master + /// ID Licenza Master /// Codice Impiego licenza /// Indica se nascondere i dati sensibili /// @@ -135,7 +160,7 @@ namespace LiMan.APi.Data Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); - dbResult = dbController.GetAttivazione( IdxLic, CodImpiego, HideData); + dbResult = dbController.GetAttivazione(IdxLic, CodImpiego, HideData); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB per AttivazioniSearch: {ts.TotalMilliseconds} ms"); diff --git a/LiMan.Api/LiMan.APi.xml b/LiMan.Api/LiMan.APi.xml index 3d53112..da55d52 100644 --- a/LiMan.Api/LiMan.APi.xml +++ b/LiMan.Api/LiMan.APi.xml @@ -59,24 +59,22 @@ Recupero record di sub licenza (con chiavi anonimizzate) - Idx licenza MASTER - Codice univoco impiego licenza + ID Licenza MASTER Verifica recupero un record di sub licenza - Idx licenza MASTER + ID Licenza MASTER Codice univoco impiego licenza - + Richiesta attivazione di una lista di chiavi di licenza sub - Idx licenza MASTER - Elenco codici impiego (key) + valori in formato dizionari + Obj Richiesta con licenza MASTER + Elenco codici impiego (key) + valori in formato dizionari @@ -159,11 +157,11 @@ Indica se nascondere i dati sensibili - + Effettua registrazione (se possibile) delle licenze indicate dall'elenco codici di impiego indicati - + Codice Licenza Master Elenco codici impiego (key) + valori in formato dizionari Numero giorni x scadenza veto modifica @@ -171,9 +169,17 @@ - Elenco Attivaizoni da licenza master + Elenco Attivaizoni da ID Licenza master - Codice Licenza Master + Idx Licenza Master + Indica se nascondere i dati sensibili + + + + + Elenco Attivaizoni da valore Licenza master + + Licenza Master Indica se nascondere i dati sensibili @@ -181,7 +187,7 @@ Elenco licenze dato cliente - Codice Licenza Master + ID Licenza Master Codice Impiego licenza Indica se nascondere i dati sensibili diff --git a/LiMan.DB/Controllers/DbController.cs b/LiMan.DB/Controllers/DbController.cs index 3ed69ca..60dbed8 100644 --- a/LiMan.DB/Controllers/DbController.cs +++ b/LiMan.DB/Controllers/DbController.cs @@ -55,6 +55,11 @@ namespace LiMan.DB.Controllers //Log.Info("Dispose di GWMSController"); } + /// + /// Recupero da DB le attivazioni richieste + /// + /// Idx Licenza + /// Indica se generare rand parametri sensibili public List GetAttivazioniByLic(int IdxLic, bool hideData) { List dbResult = new List(); @@ -120,49 +125,51 @@ namespace LiMan.DB.Controllers /// /// Cerca di aggiungere sul DB le attivazioni richieste /// - /// Idx Master Lic + /// Master Lic /// Elenco codici impiego (key) + valori in formato dizionari /// num gg di veto da impostare /// - public bool AttivazioniTryAdd(int idxLic, Dictionary ParamDict, int numDayVeto) + public bool AttivazioniTryAdd(string MasterKey, Dictionary ParamDict, int numDayVeto) { bool answ = false; // verifica se si può procedere... - var licenza = GetLicenza(idxLic); - var attivList = GetAttivazioniByLic(idxLic, false); - if (licenza.IsValid && (licenza.NumLicenze >= (attivList.Count + ParamDict.Count))) + var licenza = GetLicenza(MasterKey); + if (licenza != null) { - DateTime vetoDate = DateTime.Today.AddDays(numDayVeto); - using (LMDbContext localDbCtx = new LMDbContext(_configuration)) + var attivList = GetAttivazioniByLic(licenza.IdxLic, false); + if (licenza.IsValid && (licenza.NumLicenze >= (attivList.Count + ParamDict.Count))) { - try + DateTime vetoDate = DateTime.Today.AddDays(numDayVeto); + using (LMDbContext localDbCtx = new LMDbContext(_configuration)) { - // genero subLicenze... - var newRec = ParamDict - .Select(x => new SubLicenzaModel() - { - IdxLic = idxLic, - CodImpiego = x.Key, - VetoUnlock = vetoDate, - Tipo = Enum.TipoLicenza.UserKey, - Chiave = x.Value - }) - .ToList(); - localDbCtx - .DbSetSubLicenze - .AddRange(newRec); + try + { + // genero subLicenze... + var newRec = ParamDict + .Select(x => new SubLicenzaModel() + { + IdxLic = licenza.IdxLic, + CodImpiego = x.Key, + VetoUnlock = vetoDate, + Tipo = Enum.TipoLicenza.UserKey, + Chiave = x.Value + }) + .ToList(); + localDbCtx + .DbSetSubLicenze + .AddRange(newRec); - localDbCtx.SaveChanges(); - } - catch(Exception exc) - { - Log.Error($"Errore in AttivazioniTryAdd per IdxLic {idxLic} | #rec: {ParamDict.Count}{Environment.NewLine}{exc}"); + localDbCtx.SaveChanges(); + } + catch (Exception exc) + { + Log.Error($"Errore in AttivazioniTryAdd per MasterKey {MasterKey} | #rec: {ParamDict.Count}{Environment.NewLine}{exc}"); + } } } } - return answ; } @@ -363,6 +370,18 @@ namespace LiMan.DB.Controllers } return dbResult; } + public LicenzaModel GetLicenza(string MasterKey) + { + LicenzaModel dbResult = new LicenzaModel(); + using (LMDbContext localDbCtx = new LMDbContext(_configuration)) + { + dbResult = localDbCtx + .DbSetLicenze + .Where(x => x.Chiave == MasterKey) + .FirstOrDefault(); + } + return dbResult; + } public List GetLicenze() {