best quotes

This commit is contained in:
tmont 2011-02-14 19:48:36 +00:00
parent babf23af24
commit 6dc4c4d945
6 changed files with 58 additions and 1 deletions

View File

@ -28,6 +28,19 @@ namespace VideoGameQuotes.Web.Controllers {
return View(new QuoteCollectionModel { Quotes = quotes, User = currentUserProvider.CurrentUser }); 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() { public ActionResult Random() {
var quote = quoteService.GetRandomQuote(); var quote = quoteService.GetRandomQuote();
if (quote == null) { if (quote == null) {

View File

@ -38,6 +38,7 @@ namespace VideoGameQuotes.Web {
routes.IgnoreRoute("media/{*anything}"); routes.IgnoreRoute("media/{*anything}");
routes.MapRoute("home", "{action}", new { controller = "Home", action = "Index" }, new { action = "about|contact" }); 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("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("individual-quote", "quote/{id}/{*text}", new { controller = "Quote", action = "Quote" }, new { id = @"\d+" });
routes.MapRoute("create-category", "category/create", new { controller = "Quote", action = "CreateCategory" }); routes.MapRoute("create-category", "category/create", new { controller = "Quote", action = "CreateCategory" });

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using JetBrains.Annotations; using JetBrains.Annotations;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
@ -9,6 +10,17 @@ namespace VideoGameQuotes.Web.Models {
public User User { get; set; } 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 { public class QuoteModel {
[NotNull] [NotNull]
public Quote Quote { get; set; } public Quote Quote { get; set; }

View File

@ -21,6 +21,7 @@ namespace VideoGameQuotes.Web.Services {
IEnumerable<Quote> GetMostRecentQuotes(int limit); IEnumerable<Quote> GetMostRecentQuotes(int limit);
[CanBeNull] [CanBeNull]
Quote GetRandomQuote(); Quote GetRandomQuote();
IEnumerable<Quote> GetBestQuotes(int start, int end);
} }
public class QuoteService : IQuoteService { public class QuoteService : IQuoteService {
@ -115,5 +116,15 @@ namespace VideoGameQuotes.Web.Services {
return quotes[new Random().Next(quotes.Length)]; 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);
}
} }
} }

View File

@ -116,6 +116,7 @@
<Content Include="Views\Home\About.aspx" /> <Content Include="Views\Home\About.aspx" />
<Content Include="Views\Home\Contact.aspx" /> <Content Include="Views\Home\Contact.aspx" />
<Content Include="Views\Home\ContactSuccess.aspx" /> <Content Include="Views\Home\ContactSuccess.aspx" />
<Content Include="Views\Quote\Best.aspx" />
<Content Include="Views\Quote\NoQuotes.aspx" /> <Content Include="Views\Quote\NoQuotes.aspx" />
<Content Include="Views\Quote\Quote.aspx" /> <Content Include="Views\Quote\Quote.aspx" />
<Content Include="Views\Quote\QuoteNotFound.aspx" /> <Content Include="Views\Quote\QuoteNotFound.aspx" />

View 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 %> &ndash; <%= 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>