コード例 #1
0
ファイル: SearchController.cs プロジェクト: akassama/net-news
        public IActionResult Index([FromQuery(Name = "p")] string p = "1", [FromQuery(Name = "s")] string s = "10", [FromQuery(Name = "q")] string q = null)
        {
            if (string.IsNullOrEmpty(q) || q == "Index")
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (q.Length < 2)
            {
                TempData["ErrorMessage"] = "Query too short...";
                return(RedirectToAction("Index", "Home"));
            }

            try
            {
                ViewBag.SearchValue = q;

                //set default pagination values
                ViewBag.PageNo   = 1;
                ViewBag.PageSize = 10;
                if (!string.IsNullOrEmpty(p) && !string.IsNullOrEmpty(s))
                {
                    ViewBag.PageNo   = Int32.Parse(p);
                    ViewBag.PageSize = Int32.Parse(s);
                }

                ViewBag.PageSkip = (ViewBag.PageNo - 1) * ViewBag.PageSize;
                int PageSkip = ViewBag.PageSkip;
                int PageSize = ViewBag.PageSize;
                ViewBag.TotalRecords = _context.vwPostsApproved.Where(s => s.PostTitle.Contains(q) || s.PostExtract.Contains(q) || s.PostContent.Contains(q) || s.PostTags.Contains(q)).Count();

                var SearchData = _context.vwPostsApproved.Where(s => s.PostTitle.Contains(q) || s.PostExtract.Contains(q) || s.PostContent.Contains(q) || s.PostTags.Contains(q)).OrderByDescending(s => s.ApprovalsDateAdded).Skip(PageSkip).Take(PageSize).ToList();

                if (_systemConfiguration.logSearches)
                {
                    //log search
                    string StatType    = "Search";
                    string ActionValue = q;
                    string VisitorIP   = functions.FormatVisitorIP(_sessionManager.SessionIP, _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString());
                    string OtherInfo   = null; //add any other info here
                    functions.LogSiteStat(StatType, ActionValue, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), OtherInfo);
                }

                //Set Meta SEO
                ViewData["Title"] = "Search results for " + q;
                ViewData["ContentDescription"] = "Search results for " + q;
                ViewData["ContentKeywords"]    = q + "," + functions.GetSiteLookupData("MetaKeywords");

                return(View(SearchData));
            }
            catch (Exception ex)
            {
                //Log Error
                _logger.LogInformation("Get Search Details Error: " + ex.ToString());
                TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email.";
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #2
0
        // GET: Latest News Data
        public IActionResult Index([FromQuery(Name = "p")] string p = "1", [FromQuery(Name = "s")] string s = "10")
        {
            try
            {
                //set default pagination values
                ViewBag.PageNo   = 1;
                ViewBag.PageSize = 10;
                if (!string.IsNullOrEmpty(p) && !string.IsNullOrEmpty(s))
                {
                    ViewBag.PageNo   = Int32.Parse(p);
                    ViewBag.PageSize = Int32.Parse(s);
                }

                ViewBag.PageSkip = (ViewBag.PageNo - 1) * ViewBag.PageSize;
                int      PageSkip            = ViewBag.PageSkip;
                int      PageSize            = ViewBag.PageSize;
                DateTime LatestNewsDateRange = DateTime.Now.AddDays(-1);
                ViewBag.TotalRecords = _context.vwPostsApproved.Where(s => s.ApprovalsDateAdded > LatestNewsDateRange).Count();
                if (ViewBag.TotalRecords == 0)
                {
                    //go back further
                    LatestNewsDateRange  = DateTime.Now.AddDays(-5);
                    ViewBag.TotalRecords = _context.vwPostsApproved.Where(s => s.ApprovalsDateAdded > LatestNewsDateRange).Count();
                }

                var CategoryData = _context.vwPostsApproved.Where(s => s.ApprovalsDateAdded > LatestNewsDateRange).OrderByDescending(s => s.ApprovalsDateAdded).Skip(PageSkip).Take(PageSize).ToList();

                string VisitorIP = functions.FormatVisitorIP(_sessionManager.SessionIP, _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString());

                string OtherInfo = null; //add any other info here

                if (_systemConfiguration.logSearches)
                {
                    //log search
                    string StatType    = "LatestNews";
                    string ActionValue = "Latest News";
                    functions.LogSiteStat(StatType, ActionValue, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), OtherInfo);
                }

                //log visit
                string LogName = "LatestNews";
                functions.VisitLog(_systemConfiguration.visitLogTypes.Split(",")[2], LogName, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), null, OtherInfo);

                //Set Meta SEO
                ViewData["ContentDescription"] = functions.GetSiteLookupData("MetaDescription");
                ViewData["ContentKeywords"]    = functions.GetSiteLookupData("MetaKeywords");
                ViewBag.Title = "Latest News, " + functions.GetSiteLookupData("MetaTitle");

                return(View(CategoryData));
            }
            catch (Exception ex)
            {
                //Log Error
                _logger.LogInformation("Get Latest News Error: " + ex.ToString());
                TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email.";
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #3
0
        // GET: Category Data
        public async Task <IActionResult> Index(string id, [FromQuery(Name = "p")] string p = "1", [FromQuery(Name = "s")] string s = "10")
        {
            if (string.IsNullOrEmpty(id) || id == "Index")
            {
                return(RedirectToAction("Index", "Home"));
            }

            try
            {
                ViewBag.Category = id;
                string ShortCategoryName = functions.ConvertCase(id, "TitleTrim");
                var    DBQuery           = _context.Categories.Where(s => s.ShortCategoryName == ShortCategoryName);
                //if category not found
                if (!DBQuery.Any())
                {
                    TempData["ErrorMessage"] = "Category not found";
                    return(RedirectToAction("Index", "Home"));
                }
                string CategoryID = DBQuery.FirstOrDefault().CategoryID;
                ViewBag.CategoryID = CategoryID;

                //set default pagination values
                ViewBag.PageNo   = 1;
                ViewBag.PageSize = 10;
                if (!string.IsNullOrEmpty(p) && !string.IsNullOrEmpty(s))
                {
                    ViewBag.PageNo   = Int32.Parse(p);
                    ViewBag.PageSize = Int32.Parse(s);
                }

                ViewBag.PageSkip = (ViewBag.PageNo - 1) * ViewBag.PageSize;
                int PageSkip = ViewBag.PageSkip;
                int PageSize = ViewBag.PageSize;
                ViewBag.TotalRecords = _context.vwPostsApproved.Where(s => s.PostCategory == CategoryID).Count();

                var CategoryData = _context.vwPostsApproved.Where(s => s.PostCategory == CategoryID).OrderByDescending(s => s.ApprovalsDateAdded).Skip(PageSkip).Take(PageSize).ToListAsync();

                string VisitorIP = functions.FormatVisitorIP(_sessionManager.SessionIP, _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString());
                string OtherInfo = null; //add any other info here

                if (_systemConfiguration.logSearches)
                {
                    //log category
                    string StatType    = "Category";
                    string ActionValue = CategoryID;
                    functions.LogSiteStat(StatType, ActionValue, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), OtherInfo);
                }

                //log visit
                string CategoryName = _context.Categories.Where(s => s.CategoryID == CategoryID).FirstOrDefault().CategoryName;
                functions.VisitLog(_systemConfiguration.visitLogTypes.Split(",")[2], CategoryName, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), null, OtherInfo);

                ViewData["Title"] = functions.ConvertCase(id, "SplitUpper");
                ViewData["ContentDescription"] = functions.GetSiteLookupData("SiteName") + " " + id + " Category";
                ViewData["ContentKeywords"]    = id;

                return(View(await CategoryData));
            }
            catch (Exception ex)
            {
                //Log Error
                _logger.LogInformation("Get Category Error: " + ex.ToString());
                TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email.";
            }

            return(RedirectToAction("Index", "Home"));
        }