private ICollection<Tag> GetTags(ArticleDataModel model)
        {
            var titletgas = model.Title.Split(' ');
            var allTags = new HashSet<string>(titletgas);

            foreach (var modelTag in model.Tags)
            {
                allTags.Add(modelTag.Name);
            }

            var articleTags = new HashSet<Tag>();
            foreach (var tagName in allTags)
            {
                var tag = this.data.Tags.All()
                .FirstOrDefault(t => t.Name == tagName);
                if (tag == null)
                {
                    tag = new Tag { Name = tagName };
                    this.data.Tags.Add(tag);
                }

                articleTags.Add(tag);
            }

            return articleTags;
        }
Exemplo n.º 2
0
        public Tag AddNewTag(string name)
        {
            var tagToAdd = new Tag
            {
                Name = name
            };

            this.tags.Add(tagToAdd);
            this.tags.SaveChanges();

            return this.tags.GetById(tagToAdd.Id);
        }
Exemplo n.º 3
0
        public ICollection<Tag> GetCollection(IEnumerable<string> tagNames)
        {
            var result = new HashSet<Tag>();

            foreach (var tag in tagNames)
            {
                var tagInDb = this.tags.All().FirstOrDefault(t => t.Name == tag);
                if (tagInDb == null)
                {
                    tagInDb = new Tag
                    {
                        Name = tag,
                    };

                    this.tags.Add(tagInDb);
                    this.tags.SaveChanges();
                }

                result.Add(tagInDb);
            }

            return result;
        }
        private Article[] GenerateValidTestArticles(int count)
        {
            Article[] articles = new Article[count];
            var category = new Category()
            {
                ID = 1,
                Name = "Test Category"
            };

            var tags = new Tag[]{
                new Tag(){
                     ID = 1,
                     Name="tag"
                }
            };

            for (int i = 0; i < count; i++)
            {
               var article = new Article
                {
                    ID = i,
                    Title = " Title #" + i,
                    Content = "The Content #" + i,
                    Category = category,
                    DateCreated = DateTime.Now,
                    Tags = tags,
                    Author = new ApplicationUser()
                };
               articles[i] = article;
            }

            return articles;
        }
        private ICollection<Tag> GetTags(ArticleViewModel article)
        {
            if (string.IsNullOrWhiteSpace(article.Title))
            {
                throw new ArgumentNullException("Article title cannot be null or empty!");
            }

            var titleTags = article.Title.Split(' ');
            var allTags = new HashSet<string>(titleTags);

            // Check if .Tags is not null
            foreach (var tag in article.Tags)
            {
                allTags.Add(tag);
            }

            var articleTags = new HashSet<Tag>();
            foreach (var tagName in allTags)
            {
                var tag = this.data.Tags.All().FirstOrDefault(t => t.Name == tagName);
                if (tag == null)
                {
                    tag = new Tag { Name = tagName };
                    this.data.Tags.Add(tag);
                }

                articleTags.Add(tag);
            }

            return articleTags;
        }
Exemplo n.º 6
0
        private HashSet<Tag> GetTags(ArticleDataModel model)
        {
            HashSet<Tag> tags = new HashSet<Tag>();
            var newTagNames = model.Tags.ToList();
            var tagsFromTitle = model.Title.Split(' ');
            newTagNames.AddRange(tagsFromTitle);

            foreach (var newTagName in newTagNames)
            {
                var tag = this.data.Tags.All()
                    .FirstOrDefault(t => t.Name == newTagName);
                if (tag == null)
                {
                    tag = new Tag { Name = newTagName };
                    this.data.Tags.Add(tag);
                }

                tags.Add(tag);
            }
            return tags;
        }