diff --git a/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs b/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs index 87226a0..19b4e04 100644 --- a/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs +++ b/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs @@ -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(); diff --git a/Src/VideoGameQuotes.Web/Models/QuoteModel.cs b/Src/VideoGameQuotes.Web/Models/QuoteModel.cs index c915e4e..d4b301f 100644 --- a/Src/VideoGameQuotes.Web/Models/QuoteModel.cs +++ b/Src/VideoGameQuotes.Web/Models/QuoteModel.cs @@ -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; } diff --git a/Src/VideoGameQuotes.Web/Services/QuoteService.cs b/Src/VideoGameQuotes.Web/Services/QuoteService.cs index ebb2ca9..5ecaa4c 100644 --- a/Src/VideoGameQuotes.Web/Services/QuoteService.cs +++ b/Src/VideoGameQuotes.Web/Services/QuoteService.cs @@ -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 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)]; + } } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj b/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj index bf07bf3..e29eb77 100644 --- a/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj +++ b/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj @@ -116,6 +116,7 @@ + diff --git a/Src/VideoGameQuotes.Web/Views/Quote/NoQuotes.aspx b/Src/VideoGameQuotes.Web/Views/Quote/NoQuotes.aspx new file mode 100644 index 0000000..e248eb0 --- /dev/null +++ b/Src/VideoGameQuotes.Web/Views/Quote/NoQuotes.aspx @@ -0,0 +1,8 @@ +<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" MasterPageFile="~/Views/Shared/Site.Master" %> +No Quotes + +

+ It appears you’re trying to look at some video game quotes. Unfortunately, we don’t have any. + You can fix that by <%= Html.ActionLink("adding some", "Submit", "Quote") %>. +

+
\ No newline at end of file