public static Logic.Objects.Tag MapTag(Data.Entities.Tag contextTag) { Logic.Objects.Tag logicTag = new Logic.Objects.Tag() { tagID = contextTag.TagId, tagName = contextTag.TagName, deletable = contextTag.ClientTagXref.Count > 0 ? false : true, tagImmutable = contextTag.TagName == Constants.MASS_MAILER_TAG || contextTag.TagName == Constants.MASS_MAIL_RECIPIENT_TAG || contextTag.TagName == Constants.GRINCH_TAG ? true : false }; return(logicTag); }
private void SaveTags(int?postID, IFormCollection form) { // replace " " with "-", so "#star wars" becomes "#star-wars" // all tags begin with #, but strip # out when saving to DB var currentTags = _tagService.GetTags(postID ?? 0);// Tags currently assigned to the post if (form["txtTags"] != "") { string[] newTags = form["txtTags"].ToString().Split('#'); foreach (var newTag in newTags) { if (newTag.Length == 0) { continue; } string formattedTag = newTag.Trim().ToLower().Replace(" ", "-"); // check if we already have this association var currentTag = currentTags.Where(p => p.Name == formattedTag).FirstOrDefault(); if (currentTag != null) { currentTags.Remove(currentTag); continue; } // check if the tag already exists var tag = _tagService.Get(p => p.Name == formattedTag); if (tag == null) { // add a new tag tag = new Data.Entities.Tag { Name = formattedTag, }; _context.Set <Data.Entities.Tag>().Add(tag); _context.SaveChanges(); } // save the association var relationship = new SlickCMS.Data.Entities.Relationship { PostId = postID ?? 0, TagId = tag.TagId, }; _context.Set <Data.Entities.Relationship>().Add(relationship); } // remove old associations foreach (var currentTag in currentTags) { var currentRelationship = _context.Set <Data.Entities.Relationship>().Where(r => r.CategoryId == currentTag.TagId && r.PostId == postID); _context.Set <Data.Entities.Relationship>().RemoveRange(currentRelationship); } } else { foreach (var currentTag in currentTags) { var currentRelationship = _context.Set <Data.Entities.Relationship>().Where(r => r.TagId == currentTag.TagId && r.PostId == postID); _context.Set <Data.Entities.Relationship>().RemoveRange(currentRelationship); } } _context.SaveChanges(); }