From 8b3fa3652416db445e1f3028f2d6d62d2bff1e27 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 8 Sep 2021 11:00:34 +0200 Subject: [PATCH] Ancora update in import Tags --- MP.FileData/Controllers/FileController.cs | 87 +++++++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/MP.FileData/Controllers/FileController.cs b/MP.FileData/Controllers/FileController.cs index 97a3930e..f008f7ee 100644 --- a/MP.FileData/Controllers/FileController.cs +++ b/MP.FileData/Controllers/FileController.cs @@ -244,7 +244,9 @@ namespace MP.FileData.Controllers FileContent = File.ReadAllBytes(o.FullName) }).ToList(); + // gestione Tags (da migliorare...) List excludeTags = new List() { "M4", "M5", "M4+A", "M4+B", "M5+A", "M5+B" }; + var currTags = TagGetAll(); // calcolo MD5 e tags foreach (var item in newRec) @@ -253,11 +255,13 @@ namespace MP.FileData.Controllers item.MD5 = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); List Tags = new List(); - // cerco codice tag come nome file(TAG) nei primi 500 char + List Tag2ins = new List(); + // cerco codice tag come nome file(TAG) nei primi 100 char + int maxChar = 100; string code2search = ""; - if (item.FileStringContent.Length > 200) + if (item.FileStringContent.Length > maxChar) { - code2search = item.FileStringContent.Substring(0, 200); + code2search = item.FileStringContent.Substring(0, maxChar); } else { @@ -265,8 +269,21 @@ namespace MP.FileData.Controllers } Tags = TagsUtils.searchProgComment(item.Name, code2search, excludeTags); - //// aggiungo i tags SE non ci fossero - //item.Tags = Tags; + foreach (var tag in Tags) + { + var foundTag = currTags.Where(x => x.TagId == tag).ToList(); + // aggiungo i tags SE non ci fossero + if (foundTag.Count == 0) + { + Tag2ins.Add(new TagModel() { TagId = tag }); + } + } + + // salvo i tags + TagInsert(Tag2ins); + + // salvo i tags relativi ai files + item.Tags = Tag2ins; } // aggiungo in blocco @@ -468,6 +485,25 @@ namespace MP.FileData.Controllers return answ; } + /// + /// Elenco TAGS (tutti) + /// + /// + /// + /// + public List TagGetAll() + { + List dbResult = new List(); + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + dbResult = localDbCtx + .DbSetTags + .OrderBy(x => x.TagId) + .ToList(); + } + return dbResult; + } + /// /// Elenco TAGS (FILTRATO!!!) /// @@ -506,6 +542,47 @@ namespace MP.FileData.Controllers return dbResult; } + /// + /// Ricerca singolo record + /// + /// + /// + public TagModel TagGetSingle(string TagId) + { + TagModel dbResult = new TagModel(); + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + dbResult = localDbCtx + .DbSetTags + .Where(x => x.TagId == TagId) + .FirstOrDefault(); + } + return dbResult; + } + + /// + /// inserisce i tag + /// + /// + /// + public bool TagInsert(List newTags) + { + bool done = false; + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + try + { + localDbCtx.DbSetTags.AddRange(newTags); + localDbCtx.SaveChanges(); + } + catch (Exception exc) + { + Log.Error($"Eccezione in TagInsert{Environment.NewLine}{exc}"); + } + } + return done; + } + #endregion Public Methods #if false