<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Hermes.DataAccess;
using Microsoft.AspNet.Http;
using Microsoft.Data.Entity;

namespace Hermes.Content.Blogs
{
    public interface IBlogManager : IContentManager<Blog, Guid>
    {
        IQueryable<Blog> Blogs { get; }
        IQueryable<BlogPost> BlogPosts { get; }
    }

    public class BlogManager : ContentManager<Blog, Guid>, IBlogManager
    {
        #region Constructor

        private IBlogStore _blogStore;
        private IBlogPostStore _blogPostsStore;
        private ILogger _logger;

        public BlogManager(IBlogStore store, IHttpContextAccessor contextAccessor, ILoggerFactory loggerFactory, IBlogPostStore blogPostsStore)
            : base(store, contextAccessor)
        {
            _blogPostsStore = blogPostsStore;
            _logger = loggerFactory.CreateLogger<BlogManager>();
        }

        #endregion

        public IQueryable<Blog> Blogs { get { return _store.Items; } }
        public IQueryable<BlogPost> BlogPosts { get { return _blogPostsStore.Items; } }

        #region BlogPost CRUD Operations

        public virtual async Task<HermesResult> AddPostAsync(BlogPost blogPost)
        {
            var result = DoesTitleExist(blogPost.Title);

            if (result.Succeeded)
            {
                var blogResult = DoesBlogExist(blogPost);

                if (blogResult.Succeeded)
                {
                    if (blogPost.Blog != null && blogPost.BlogId == null)
                    {
                        blogPost.BlogId = blogPost.Blog.Id;
                    }
                    else if (blogPost.Blog == null && blogPost.BlogId != null)
                    {
                        var blog = await _store.FindByIdAsync(blogPost.Blog.Id, CancellationToken);
                        blogPost.Blog = blog;                        
                    }

                    return await _blogPostsStore.AddAsync(blogPost, CancellationToken);
                }

                return blogResult;
            }

            return result;
        }

        public async Task<BlogPost> FindPostByIdAsync(Guid id)
        {
            var blogPost = await _blogPostsStore.FindByIdAsync(id, CancellationToken);<commit_msg>updated the blog manager<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Hermes.DataAccess;
using Microsoft.AspNet.Http;
using Microsoft.Data.Entity;

namespace Hermes.Content.Blogs
{
    public interface IBlogManager : IContentManager<Blog, Guid>
    {
        IQueryable<Blog> Blogs { get; }
        IQueryable<BlogPost> BlogPosts { get; }
    }

    public class BlogManager : ContentManager<Blog, Guid>, IPersistentItemManager<Blog, Guid>, IBlogManager
    {
        #region Constructor

        private IBlogStore _blogStore;
        private IBlogPostStore _blogPostsStore;
        private ILogger _logger;

        public BlogManager(IBlogStore store, IHttpContextAccessor contextAccessor, ILoggerFactory loggerFactory, IBlogPostStore blogPostsStore)
            : base(store, contextAccessor)
        {
            _blogPostsStore = blogPostsStore;
            _logger = loggerFactory.CreateLogger<BlogManager>();
        }

        #endregion

        public IQueryable<Blog> Blogs { get { return _store.Items; } }
        public IQueryable<BlogPost> BlogPosts { get { return _blogPostsStore.Items; } }

        #region BlogPost CRUD Operations

        public virtual async Task<HermesResult> AddPostAsync(BlogPost blogPost)
        {
            var result = DoesTitleExist(blogPost.Title);

            if (result.Succeeded)
            {
                var blogResult = DoesBlogExist(blogPost);

                if (blogResult.Succeeded)
                {
                    if (blogPost.Blog != null && blogPost.BlogId == null)
                    {
                        blogPost.BlogId = blogPost.Blog.Id;
                    }
                    else if (blogPost.Blog == null && blogPost.BlogId != null)
                    {
                        var blog = await _store.FindByIdAsync(blogPost.Blog.Id, CancellationToken);
                        blogPost.Blog = blog;                        
                    }

                    blogPost.Slug = await GenerateNewSlugAsync(blogPost.Title);

                    return await _blogPostsStore.AddAsync(blogPost, CancellationToken);
                }

                return blogResult;
            }

            return result;
        }

        public async Task<BlogPost> FindPostByIdAsync(Guid id)
        {
            var blogPost = await _blogPostsStore.FindByIdAsync(id, CancellationToken);
{'fraction_non_alphanumeric': 0.07405875051717005, 'fraction_numerical': 0.0, 'mean_word_length': 1.9179239589619794, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4612554', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1211, 'n_words': 271}