<commit_before>﻿using Portal.CMS.Services.Analytics;
using Portal.CMS.Services.Authentication;
using Portal.CMS.Services.Posts;
using Portal.CMS.Services.Themes;
using Portal.CMS.Web.Areas.Admin.Helpers;
using Portal.CMS.Web.ViewModels.Blog;
using System.Linq;
using System.Web.Mvc;

namespace Portal.CMS.Web.Controllers
{
    public class BlogController : Controller
    {
        #region Dependencies

        private readonly IPostService _postService;
        private readonly IPostCommentService _postCommentService;
        private readonly IAnalyticsService _analyticsService;
        private readonly IUserService _userService;
        private readonly IThemeService _themeService;

        public BlogController(IPostService postService, IPostCommentService postCommentService, IAnalyticsService analyticsService, IUserService userService, IThemeService themeService)
        {
            _postService = postService;
            _postCommentService = postCommentService;
            _analyticsService = analyticsService;
            _userService = userService;
            _themeService = themeService;
        }

        #endregion Dependencies

        [HttpGet]
        public ActionResult Index(int? id)
        {
            var model = new BlogViewModel
            {
                RecentPosts = _postService.Read(UserHelper.UserId, string.Empty).ToList()
            };

            if (!model.RecentPosts.Any())
                return RedirectToAction("Index", "Home");

            if (id.HasValue)
                model.CurrentPost = _postService.Read(UserHelper.UserId, id.Value);
            else
                model.CurrentPost = model.RecentPosts.First();

            if (model.CurrentPost == null)
                return RedirectToAction("Index", "Home");

            model.Author = _userService.GetUser(model.CurrentPost.PostAuthorUserId);

            model.SimiliarPosts = _postService.Read(UserHelper.UserId, model.CurrentPost.PostCategory.PostCategoryName).ToList();

            model.RecentPosts.Remove(model.CurrentPost);
            model.SimiliarPosts.Remove(model.CurrentPost);

            return View(model);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Comment(int postId, string commentBody)
        {
            _postCommentService.Add(UserHelper.UserId.Value, postId, commentBody);

            return RedirectToAction(nameof(Index), "Blog", new { postId = postId });
        }

        [HttpGet]
        public ActionResult Analytic(int postId, string referrer)
        {
            if (UserHelper.IsLoggedIn)
                _analyticsService.LogPostView(postId, referrer, Request.UserHostAddress, Request.Browser.Browser, UserHelper.UserId);
            else
                _analyticsService.LogPostView(postId, referrer, Request.UserHostAddress, Request.Browser.Browser, null);

            return Json(new { State = true });
        }
    }
}<commit_msg>Fix Post Analytic Reporting Issue<commit_after>﻿using Portal.CMS.Services.Analytics;
using Portal.CMS.Services.Authentication;
using Portal.CMS.Services.Posts;
using Portal.CMS.Services.Themes;
using Portal.CMS.Web.Areas.Admin.Helpers;
using Portal.CMS.Web.ViewModels.Blog;
using System.Linq;
using System.Web.Mvc;

namespace Portal.CMS.Web.Controllers
{
    public class BlogController : Controller
    {
        #region Dependencies

        private readonly IPostService _postService;
        private readonly IPostCommentService _postCommentService;
        private readonly IAnalyticsService _analyticsService;
        private readonly IUserService _userService;
        private readonly IThemeService _themeService;

        public BlogController(IPostService postService, IPostCommentService postCommentService, IAnalyticsService analyticsService, IUserService userService, IThemeService themeService)
        {
            _postService = postService;
            _postCommentService = postCommentService;
            _analyticsService = analyticsService;
            _userService = userService;
            _themeService = themeService;
        }

        #endregion Dependencies

        [HttpGet]
        public ActionResult Index(int? id)
        {
            var model = new BlogViewModel
            {
                RecentPosts = _postService.Read(UserHelper.UserId, string.Empty).ToList()
            };

            if (!model.RecentPosts.Any())
                return RedirectToAction("Index", "Home");

            if (id.HasValue)
                model.CurrentPost = _postService.Read(UserHelper.UserId, id.Value);
            else
                model.CurrentPost = model.RecentPosts.First();

            if (model.CurrentPost == null)
                return RedirectToAction("Index", "Home");

            model.Author = _userService.GetUser(model.CurrentPost.PostAuthorUserId);

            model.SimiliarPosts = _postService.Read(UserHelper.UserId, model.CurrentPost.PostCategory.PostCategoryName).ToList();

            model.RecentPosts.Remove(model.CurrentPost);
            model.SimiliarPosts.Remove(model.CurrentPost);

            return View(model);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Comment(int postId, string commentBody)
        {
            _postCommentService.Add(UserHelper.UserId.Value, postId, commentBody);

            return RedirectToAction(nameof(Index), "Blog", new { postId = postId });
        }

        public ActionResult Analytic(int postId, string referrer)
        {
            if (UserHelper.IsLoggedIn)
                _analyticsService.LogPostView(postId, referrer, Request.UserHostAddress, Request.Browser.Browser, UserHelper.UserId);
            else
                _analyticsService.LogPostView(postId, referrer, Request.UserHostAddress, Request.Browser.Browser, null);

            return Json(new { State = true });
        }
    }
}
{'fraction_non_alphanumeric': 0.08135593220338982, 'fraction_numerical': 0.0, 'mean_word_length': 2.5548192771084337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1877017', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1484, 'n_words': 299}