random page

This commit is contained in:
tmont 2011-02-14 19:14:13 +00:00
parent a5f96076dc
commit 0b8af8b8a1
5 changed files with 33 additions and 0 deletions

View File

@ -28,6 +28,15 @@ namespace VideoGameQuotes.Web.Controllers {
return View(new QuoteCollectionModel { Quotes = quotes, User = currentUserProvider.CurrentUser });
}
public ActionResult Random() {
var quote = quoteService.GetRandomQuote();
if (quote == null) {
return View("NoQuotes");
}
return RedirectToAction("Quote", new { id = quote.Id, text = quote.GetUrlFriendlyText() });
}
[IsValidUser]
public ActionResult Submit() {
var model = new QuoteSubmitModel();

View File

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

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Portoa.Persistence;
using VideoGameQuotes.Api;
@ -18,6 +19,8 @@ namespace VideoGameQuotes.Web.Services {
Category GetCategory(int categoryId);
Category SaveCategory(Category category);
IEnumerable<Quote> GetMostRecentQuotes(int limit);
[CanBeNull]
Quote GetRandomQuote();
}
public class QuoteService : IQuoteService {
@ -102,5 +105,15 @@ namespace VideoGameQuotes.Web.Services {
.OrderByDescending(quote => quote.Created)
.Take(limit);
}
[UnitOfWork]
public Quote GetRandomQuote() {
var quotes = quoteRepository.Records.ToArray();
if (quotes.Length == 0) {
return null;
}
return quotes[new Random().Next(quotes.Length)];
}
}
}

View File

@ -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\NoQuotes.aspx" />
<Content Include="Views\Quote\Quote.aspx" />
<Content Include="Views\Quote\QuoteNotFound.aspx" />
<Content Include="Views\Quote\Recent.aspx" />

View File

@ -0,0 +1,8 @@
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" MasterPageFile="~/Views/Shared/Site.Master" %>
<asp:Content runat="server" ID="Title" ContentPlaceHolderID="TitleContent">No Quotes</asp:Content>
<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
<p>
It appears you&rsquo;re trying to look at some video game quotes. Unfortunately, we don&rsquo;t have any.
You can fix that by <%= Html.ActionLink("adding some", "Submit", "Quote") %>.
</p>
</asp:Content>