Ejemplo n.º 1
0
        public virtual Tag[] SyncVotes(User user, Tag[] tags, ITagUsageFactory <T> tagUsageFactory,
                                       bool onlyAdd = false)
        {
            var actualTags    = tags.Distinct().ToArray();
            var tagUsagesDiff = CollectionHelper.Diff(Usages, actualTags, (t1, t2) => t1.Tag.Equals(t2));
            var modifiedTags  = new List <Tag>(tagUsagesDiff.Added.Length + tagUsagesDiff.Removed.Length + tagUsagesDiff.Unchanged.Length);

            foreach (var newUsageTag in tagUsagesDiff.Added)
            {
                var newUsage = tagUsageFactory.CreateTagUsage(newUsageTag);
                Usages.Add(newUsage);
                newUsage.CreateVote(user);
                newUsageTag.UsageCount++;
                modifiedTags.Add(newUsageTag);
            }

            if (!onlyAdd)
            {
                foreach (var removedTag in tagUsagesDiff.Removed)
                {
                    removedTag.RemoveVote(user);

                    if (!removedTag.HasVotes)
                    {
                        removedTag.Tag.UsageCount--;
                        Usages.Remove(removedTag);
                    }

                    modifiedTags.Add(removedTag.Tag);
                }
            }

            foreach (var updated in tagUsagesDiff.Unchanged)
            {
                updated.CreateVote(user);
                modifiedTags.Add(updated.Tag);
            }

            return(modifiedTags.ToArray());
        }
Ejemplo n.º 2
0
        public virtual void SyncVotes(User user, string[] tagNames, Dictionary <string, Tag> allTags, ITagFactory tagFactory, ITagUsageFactory <T> tagUsageFactory,
                                      bool onlyAdd = false)
        {
            var newTags = tagNames.Where(t => !allTags.ContainsKey(t));

            foreach (var tag in newTags)
            {
                var newTag = tagFactory.CreateTag(tag);
                allTags.Add(newTag.Name, newTag);
            }

            tagNames = tagNames.Select(t => allTags[t].ActualTag.Name).Distinct().ToArray();
            var tagUsagesDiff = CollectionHelper.Diff(Usages, tagNames, (t1, t2) => t1.Tag.Name.Equals(t2, StringComparison.InvariantCultureIgnoreCase));

            foreach (var newUsageName in tagUsagesDiff.Added)
            {
                var tag      = allTags[newUsageName];
                var newUsage = tagUsageFactory.CreateTagUsage(tag);
                Usages.Add(newUsage);
                newUsage.CreateVote(user);
            }

            if (!onlyAdd)
            {
                foreach (var removedTag in tagUsagesDiff.Removed)
                {
                    removedTag.RemoveVote(user);

                    if (removedTag.Count <= 0)
                    {
                        Usages.Remove(removedTag);
                    }
                }
            }

            foreach (var updated in tagUsagesDiff.Unchanged)
            {
                updated.CreateVote(user);
            }
        }