diff --git a/server/AyaNova/biz/TagUtil.cs b/server/AyaNova/biz/TagUtil.cs index 9521dab1..bc5b5614 100644 --- a/server/AyaNova/biz/TagUtil.cs +++ b/server/AyaNova/biz/TagUtil.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System; using AyaNova.Util; using System.Linq; +using Microsoft.EntityFrameworkCore; using AyaNova.Models; namespace AyaNova.Biz @@ -51,20 +52,64 @@ namespace AyaNova.Biz if (newTags.Count == 0 && (originalTags == null || originalTags.Count == 0)) return; -List deleteTags=new List(); -List addTags=new List(); - - if(originalTags!=null){ + List deleteTags = new List(); + List addTags = new List(); + + if (originalTags != null) + { //Update //This logic is supposed to only come up with CHANGES, if the item is in both lists then it should disappear and not need to be dealt with //testing will validate it - deleteTags=originalTags.Except(newTags).ToList(); - addTags=newTags.Except(originalTags).ToList(); - HERE + deleteTags = originalTags.Except(newTags).ToList(); + addTags = newTags.Except(originalTags).ToList(); + - }else{ - //Add } + else + { + //Add + addTags = newTags; + } + + + //ADD / INCREMENT TAGS + + foreach (string s in addTags) + { + bool bDone = false; + //Keep on trying until there is success + //this allows for concurrency issues + do + { + //START: Get tag word and concurrency token and count + var ExistingTag = ct.Tag.FirstOrDefault(x => x.Name == s); + //if not present, then add it with a count of 0 + if (ExistingTag == null) + { + ct.Tag.Add(new Tag() { Name = s, RefCount = 1 }); + } + else + { + //Update the refcount + ExistingTag.RefCount += 1; + } + try + { + ct.SaveChanges(); + bDone = true; + } + catch (Exception ex) when(ex is DbUpdateConcurrencyException ) + { + //allow others to flow past + string sss=ex.ToString(); + } + + //If that fails due to others added it or works, either way go back to START: + //UPDATE: INCREMENT the refcount and update the record + //If that fails due to a concurrency exception go to START: + } while (bDone == false); + } + //Add / increase reference count for added tags //remove / decrease reference count for removed tags @@ -98,13 +143,7 @@ WHERE name = 'bill' //Create table tags, word varchar 255, refcount longint, concurrency token //so do this: - //ADD / INCREMENT TAGS - //START: Get tag word and concurrency token and count - //if not present, then add it with a count of 0 - //If that fails due to others added it or works, either way go back to START: - //UPDATE: INCREMENT the refcount and update the record - //If that fails due to a concurrency exception go to START: //REMOVE / DECREMENT TAGS