Ejemplo n.º 1
0
        public void AddNewBlogPost(BlogPost newBlogPost)
        {
            newBlogPost.PostDate   = DateTime.Now;
            newBlogPost.PostStatus = 1;
            if (newBlogPost.PublishDate == null)
            {
                newBlogPost.PublishDate = DateTime.Now;
            }
            var postId  = _repo.AddNewBlogPost(newBlogPost);
            var tagList = _repo.GetAllTags();

            if (newBlogPost.tags != null)
            {
                foreach (var tag in newBlogPost.tags)
                {
                    var tagExists = false;
                    var tagId     = 0;

                    foreach (var tag2 in tagList)
                    {
                        if (tag2.TagName == tag)
                        {
                            // Tag already exists, bool set to true
                            tagExists = true;
                            tagId     = tag2.TagID;
                            break;
                        }
                    }
                    // Check bool to see if tag exists
                    if (tagExists)
                    {
                        // Tag exists on table, only need to add it to PostTags to tie it to a post
                        _repo.AddNewPostTag(tagId, postId);
                    }
                    else
                    {
                        // Tag doesn't exist on table, needs to be created, then tied to a post on PostTags table
                        tagId = _repo.AddNewTag(tag);
                        _repo.AddNewPostTag(tagId, postId);
                    }
                }
            }
        }