<commit_before>using System;
using System.Linq;
using Microsoft.Framework.Logging;
using Microsoft.AspNet.Mvc;

namespace HawkProto2
{
    public class HomeController : Controller
    {
        const int PAGE_SIZE = 5;

        private readonly IPostRepository _repo;
        
        public HomeController(IPostRepository repo)
        {
            if (repo == null)
            {
                throw new ArgumentNullException(nameof(repo));
            }
            
            this._repo = repo;
        }

        [Route("")]
        public IActionResult Index()
        {
            return IndexPage(1);
        }
                
        [Route("/page/{pageNum}")]
        public IActionResult IndexPage(int pageNum)
        {
            int skip = 0;
            if (pageNum > 0)
            {
                skip = (pageNum - 1) * PAGE_SIZE;
            }
            else
            {
                return RedirectToAction("Index");
            }
            
            var posts = _repo.AllPosts().Skip(skip).Take(PAGE_SIZE).ToArray();
            var totalPostCount = _repo.AllPosts().Count();
            var lastPage = totalPostCount / PAGE_SIZE + (totalPostCount % PAGE_SIZE == 0 ? 0 : 1);
            
            if (totalPostCount == 0)
                return new HttpNotFoundResult();
            
            if (posts.Length == 0)
            {
                return RedirectToAction("IndexPage", new { pageNum = lastPage });
            }    
            return View("Index", posts);
        }
        
        [Route("archive")]
        public IActionResult Archive()
        {
            var posts = _repo.AllPosts().ToArray();
            return View(posts);
        }
        
        [Route("about")]
        public IActionResult About()
        {
            return Content("HomeController.About");
        }
        
        [Route("{year:int}")]
        public IActionResult PostsByYear(int year)
        {
            return PostsByYearPage(year, 1);
        }
        
        [Route("{year:int}/page/{pageNum:int}")]
        public IActionResult PostsByYearPage(int year, int pageNum)
        {
            //TODO add date constraints
            return Content($"HomeController.PostsByYear {year} page #{pageNum}");
        }

        [Route("{year:int}/{month:int}")]
        public IActionResult PostsByMonth(int year, int month)
        {
            return PostsByMonthPage(year, month, 1);
        }

        [Route("{year:int}/{month:int}/page/{pageNum}")]
        public IActionResult PostsByMonthPage(int year, int month, int pageNum)
        {
            //TODO add date constraints
            return Content($"HomeController.PostsByMonth {year}-{month} page #{pageNum}");
        }

        [Route("{year:int}/{month:int}/{day:int}")]
        public IActionResult PostsByDay(int year, int month, int day)
        {
            return PostsByDayPage(year, month, day, 1);
        }

        [Route("{year:int}/{month:int}/{day:int}/page/{pageNum}")]
        public IActionResult PostsByDayPage(int year, int month, int day, int pageNum)
        {
            //TODO add date constraints
            return Content($"HomeController.PostsByDay {year}-{month}-{day} page #{pageNum}");
        }

        [Route("{year:int}/{month:int}/{day:int}/{slug}")]
        public IActionResult Post(int year, int month, int day, string slug)
        {
            //TODO add date constraints
            return Content($"HomeController.Post {year}-{month}-{day} {slug}");
        }

        [Route("{slug}")]
        public IActionResult Post(string slug)
        {
            //TODO add date constraints
            return Content($"HomeController.Post {slug}");
        }
        
        [Route("category/{name}")]
        public IActionResult Category(string name)
        {
            return CategoryPage(name, 1);
        }

        [Route("category/{name}/page/{pageNum}")]
        public IActionResult CategoryPage(string name, int pageNum)
        {
            return Content($"HomeController.Category {name} page #{pageNum}");
        }
        
        [Route("tag/{name}")]
        public IActionResult Tag(string name)
        {
            return TagPage(name, 1);
        }

        [Route("tag/{name}/page/{pageNum}")]
        public IActionResult TagPage(string name, int pageNum)
        {
            return Content($"HomeController.Tag {name} page #{pageNum}");
        }
        
        [Route("author/{name}")]
        public IActionResult Author(string name)
        {
            return AuthorPage(name, 1);    
        }
        
        [Route("author/{name}/page/{pageNum}")]
        public IActionResult AuthorPage(string name, int pageNum)
        {
            return Content($"HomeController.Author {name} page #{pageNum}");
        }
        
    }
}<commit_msg>added post collection pages support<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Framework.Logging;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;

namespace HawkProto2
{
    public class HomeController : Controller
    {
        const int PAGE_SIZE = 5;

        private readonly IPostRepository _repo;
        private readonly ILogger _logger;
        
        public HomeController(IPostRepository repo, ILoggerFactory loggerFactory)
        {
            if (repo == null)
            {
                throw new ArgumentNullException(nameof(repo));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }
            
            this._repo = repo;
            this._logger = loggerFactory.CreateLogger(nameof(HomeController));
        }

        [Route("")]
        public IActionResult Index()
        {
            return IndexPage(1);
        }
        
        private IActionResult PostsHelper(IEnumerable<Post> posts, int pageNum, string action, object routeValues = null)
        {
            // if the user asks for a page less than or equal to zero, redirect to the first page
            if (pageNum <= 0)
            {
                return RedirectToAction(action);
            }
             
            // if there are no posts, return 404
            var postCount = posts.Count();
            if (postCount == 0)
            {
                return new HttpNotFoundResult();
            }
            
            // if the user asks for a page beyond the last page, redirect to the last page
            var pageCount = postCount / PAGE_SIZE + (postCount % PAGE_SIZE == 0 ? 0 : 1);
            if (pageNum > pageCount)
            {
                var routeValueDict = new RouteValueDictionary(routeValues);
                routeValueDict.Add("pageNum", pageCount);

                return RedirectToAction(action + "Page", routeValueDict);
            }
            
            var skip = (pageNum - 1) * PAGE_SIZE;
            var pagePosts = posts.Skip(skip).Take(PAGE_SIZE).ToArray();
            
            return View("Index", pagePosts);
        }   
                      
        [Route("/page/{pageNum}")]
        public IActionResult IndexPage(int pageNum)
        {
            return PostsHelper(_repo.AllPosts(), pageNum, "Index");
        }
        
        [Route("archive")]
        public IActionResult Archive()
        {
            var posts = _repo.AllPosts().ToArray();
            return View(posts);
        }
        
        [Route("about")]
        public IActionResult About()
        {
            return Content("HomeController.About");
        }
        
        [Route("{year:int}")]
        public IActionResult PostsByYear(int year)
        {
            return PostsByYearPage(year, 1);
        }
        
        [Route("{year:int}/page/{pageNum:int}")]
        public IActionResult PostsByYearPage(int year, int pageNum)
        {
            return PostsHelper(_repo.AllPosts().Where(p => p.Date.Year == year), pageNum, "PostsByYear", new { year = year });
        }

        [Route("{year:int}/{month:range(1,12)}")]
        public IActionResult PostsByMonth(int year, int month)
        {
            return PostsByMonthPage(year, month, 1);
        }

        [Route("{year:int}/{month:range(1,12)}/page/{pageNum}")]
        public IActionResult PostsByMonthPage(int year, int month, int pageNum)
        {
            return PostsHelper(
                _repo.AllPosts().Where(p => p.Date.Year == year && p.Date.Month == month), 
                pageNum, "PostsByMonth", new { year = year, month = month });
        }

        [Route("{year:int}/{month:range(1,12)}/{day:range(1,31)}")]
        public IActionResult PostsByDay(int year, int month, int day)
        {
            return PostsByDayPage(year, month, day, 1);
        }

        [Route("{year:int}/{month:range(1,12)}/{day:range(1,31)}/page/{pageNum}")]
        public IActionResult PostsByDayPage(int year, int month, int day, int pageNum)
        {
            return PostsHelper(
                _repo.AllPosts().Where(p => p.Date.Year == year && p.Date.Month == month && p.Date.Day == day), 
                pageNum, "PostsByMonth", new { year = year, month = month, day = day });
        }

        [Route("{year:int}/{month:range(1,12)}/{day:range(1,31)}/{slug}")]
        public IActionResult Post(int year, int month, int day, string slug)
        {
            //TODO add date constraints
            return Content($"HomeController.Post {year}-{month}-{day} {slug}");
        }

        [Route("{slug}")]
        public IActionResult Post(string slug)
        {
            //TODO add date constraints
            return Content($"HomeController.Post {slug}");
        }
        
        [Route("category/{name}")]
        public IActionResult Category(string name)
        {
            return CategoryPage(name, 1);
        }

        [Route("category/{name}/page/{pageNum}")]
        public IActionResult CategoryPage(string name, int pageNum)
        {
            return PostsHelper(
                _repo.AllPosts().Where(p => p.Categories.Any(c => c.Slug == name)), 
                pageNum, "Category", new { name = name });
        }
        
        [Route("tag/{name}")]
        public IActionResult Tag(string name)
        {
            return TagPage(name, 1);
        }

        [Route("tag/{name}/page/{pageNum}")]
        public IActionResult TagPage(string name, int pageNum)
        {
            return PostsHelper(
                _repo.AllPosts().Where(p => p.Tags.Any(c => c.Slug == name)), 
                pageNum, "Tag", new { name = name });
        }
        
        [Route("author/{name}")]
        public IActionResult Author(string name)
        {
            return AuthorPage(name, 1);    
        }
        
        [Route("author/{name}/page/{pageNum}")]
        public IActionResult AuthorPage(string name, int pageNum)
        {
            return PostsHelper(
                _repo.AllPosts().Where(p => p.Author.Slug == name), 
                pageNum, "Author", new { name = name });
        }
        
    }
}
{'fraction_non_alphanumeric': 0.11986671469740634, 'fraction_numerical': 0.0053134005763688765, 'mean_word_length': 1.691468734852157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, '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': '6860777', 'n_tokens_mistral': 3086, 'n_tokens_neox': 2959, 'n_words': 675}