best quotes
This commit is contained in:
		
							parent
							
								
									babf23af24
								
							
						
					
					
						commit
						6dc4c4d945
					
				@ -28,6 +28,19 @@ namespace VideoGameQuotes.Web.Controllers {
 | 
			
		||||
			return View(new QuoteCollectionModel { Quotes = quotes, User = currentUserProvider.CurrentUser });
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public ActionResult Best(int start = 0, int end = 19) {
 | 
			
		||||
			if (start < 0 || end <= 0 || start > end) {
 | 
			
		||||
				return new StatusOverrideResult(View("BadPaging")) { StatusCode = HttpStatusCode.BadRequest };
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			return View(new PagedQuoteCollectionModel { 
 | 
			
		||||
				Quotes = quoteService.GetBestQuotes(start, end), 
 | 
			
		||||
				User = currentUserProvider.CurrentUser,
 | 
			
		||||
				Start = start,
 | 
			
		||||
				End = end
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public ActionResult Random() {
 | 
			
		||||
			var quote = quoteService.GetRandomQuote();
 | 
			
		||||
			if (quote == null) {
 | 
			
		||||
 | 
			
		||||
@ -38,6 +38,7 @@ namespace VideoGameQuotes.Web {
 | 
			
		||||
			routes.IgnoreRoute("media/{*anything}");
 | 
			
		||||
 | 
			
		||||
			routes.MapRoute("home", "{action}", new { controller = "Home", action = "Index" }, new { action = "about|contact" });
 | 
			
		||||
			routes.MapRoute("best", "best/{start}-{end}/", new { controller = "Quote", action = "Best" }, new { start = @"\d+", end = @"\d+" });
 | 
			
		||||
			routes.MapRoute("quote", "{action}", new { controller = "Quote" }, new { action = "submit|search|recent|random|best" });
 | 
			
		||||
			routes.MapRoute("individual-quote", "quote/{id}/{*text}", new { controller = "Quote", action = "Quote" }, new { id = @"\d+" });
 | 
			
		||||
			routes.MapRoute("create-category", "category/create", new { controller = "Quote", action = "CreateCategory" });
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using JetBrains.Annotations;
 | 
			
		||||
using VideoGameQuotes.Api;
 | 
			
		||||
 | 
			
		||||
@ -9,6 +10,17 @@ namespace VideoGameQuotes.Web.Models {
 | 
			
		||||
		public User User { get; set; }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public class PagedQuoteCollectionModel : QuoteCollectionModel {
 | 
			
		||||
		public int Start { get; set; }
 | 
			
		||||
		public int End { get; set; }
 | 
			
		||||
		public int PageSize { get { return End - Start + 1; } }
 | 
			
		||||
		public int NextStart { get { return End + 1; } }
 | 
			
		||||
		public int NextEnd { get { return End + PageSize; } }
 | 
			
		||||
		public int PreviousStart { get { return Math.Max(0, Start - PageSize); } }
 | 
			
		||||
		public int PreviousEnd { get { return Math.Max(PageSize - 1, End - PageSize); } }
 | 
			
		||||
		public bool HasPrevious { get { return Start > 0; } }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public class QuoteModel {
 | 
			
		||||
		[NotNull]
 | 
			
		||||
		public Quote Quote { get; set; }
 | 
			
		||||
 | 
			
		||||
@ -21,6 +21,7 @@ namespace VideoGameQuotes.Web.Services {
 | 
			
		||||
		IEnumerable<Quote> GetMostRecentQuotes(int limit);
 | 
			
		||||
		[CanBeNull]
 | 
			
		||||
		Quote GetRandomQuote();
 | 
			
		||||
		IEnumerable<Quote> GetBestQuotes(int start, int end);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public class QuoteService : IQuoteService {
 | 
			
		||||
@ -115,5 +116,15 @@ namespace VideoGameQuotes.Web.Services {
 | 
			
		||||
 | 
			
		||||
			return quotes[new Random().Next(quotes.Length)];
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		[UnitOfWork]
 | 
			
		||||
		public IEnumerable<Quote> GetBestQuotes(int start, int end) {
 | 
			
		||||
			var records = quoteRepository.Records.ToArray();
 | 
			
		||||
 | 
			
		||||
			return records
 | 
			
		||||
				.OrderByDescending(quote => quote.UpVotes)
 | 
			
		||||
				.Skip(start)
 | 
			
		||||
				.Take(end - start + 1);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -116,6 +116,7 @@
 | 
			
		||||
    <Content Include="Views\Home\About.aspx" />
 | 
			
		||||
    <Content Include="Views\Home\Contact.aspx" />
 | 
			
		||||
    <Content Include="Views\Home\ContactSuccess.aspx" />
 | 
			
		||||
    <Content Include="Views\Quote\Best.aspx" />
 | 
			
		||||
    <Content Include="Views\Quote\NoQuotes.aspx" />
 | 
			
		||||
    <Content Include="Views\Quote\Quote.aspx" />
 | 
			
		||||
    <Content Include="Views\Quote\QuoteNotFound.aspx" />
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								Src/VideoGameQuotes.Web/Views/Quote/Best.aspx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Src/VideoGameQuotes.Web/Views/Quote/Best.aspx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<VideoGameQuotes.Web.Models.PagedQuoteCollectionModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
 | 
			
		||||
<%@ Import Namespace="VideoGameQuotes.Web.Models" %>
 | 
			
		||||
<asp:Content runat="server" ID="Title" ContentPlaceHolderID="TitleContent">Best: <%= Model.Start %> – <%= Model.End %></asp:Content>
 | 
			
		||||
<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
 | 
			
		||||
 | 
			
		||||
	<p>
 | 
			
		||||
	<% if (Model.HasPrevious) { %>
 | 
			
		||||
		<%= Html.ActionLink("previous", "best", new { start = Model.PreviousStart, end = Model.PreviousEnd }) %>
 | 
			
		||||
	<% } %>
 | 
			
		||||
		<%= Html.ActionLink("next", "best", new { start = Model.NextStart, end = Model.NextEnd }) %>
 | 
			
		||||
	</p>
 | 
			
		||||
 | 
			
		||||
	<%
 | 
			
		||||
		foreach (var quote in Model.Quotes) {
 | 
			
		||||
			Html.RenderPartial("SingleQuote", new QuoteModel { Quote = quote, User = Model.User });
 | 
			
		||||
		}
 | 
			
		||||
	%>
 | 
			
		||||
</asp:Content>
 | 
			
		||||
<asp:Content runat="server" ID="DeferrableScripts" ContentPlaceHolderID="DeferrableScripts"></asp:Content>
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user