diff --git a/Src/VideoGameQuotes.Web/Controllers/ApiController.cs b/Src/VideoGameQuotes.Web/Controllers/ApiController.cs index cc5a1ac..48b9843 100644 --- a/Src/VideoGameQuotes.Web/Controllers/ApiController.cs +++ b/Src/VideoGameQuotes.Web/Controllers/ApiController.cs @@ -135,13 +135,13 @@ namespace VideoGameQuotes.Web.Controllers { return new StatusOverrideResult(Json(this.CreateJsonErrorResponse(message ?? "Invalid request"))) { StatusCode = statusCode }; } - private ActionResult GetRecords(Func> recordGetter, string key) { + private ActionResult GetRecords(Func> recordGetter) { if (!ModelState.IsValid) { return Error(HttpStatusCode.BadRequest); } try { - return Json(this.CreateJsonResponse(data: new Dictionary { { key, recordGetter(apiService) } })); + return Json(this.CreateJsonResponse(data: new { records = recordGetter(apiService) })); } catch (ApiException e) { return Error(HttpStatusCode.BadRequest, e.Message); } catch { @@ -150,23 +150,23 @@ namespace VideoGameQuotes.Web.Controllers { } public ActionResult Game(ApiModel model) { - return GetRecords(service => service.GetGames(model), "games"); + return GetRecords(service => service.GetGames(model)); } public ActionResult System(ApiModel model) { - return GetRecords(service => service.GetSystems(model), "systems"); + return GetRecords(service => service.GetSystems(model)); } public ActionResult Category(ApiModel model) { - return GetRecords(service => service.GetCategories(model), "categories"); + return GetRecords(service => service.GetCategories(model)); } public ActionResult Publisher(ApiModel model) { - return GetRecords(service => service.GetPublishers(model), "publishers"); + return GetRecords(service => service.GetPublishers(model)); } public ActionResult Quote(ApiModel model) { - return GetRecords(service => service.GetQuotes(model), "quotes"); + return GetRecords(service => service.GetQuotes(model)); } } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Web/Services/QuoteService.cs b/Src/VideoGameQuotes.Web/Services/QuoteService.cs index 414b228..fb86305 100644 --- a/Src/VideoGameQuotes.Web/Services/QuoteService.cs +++ b/Src/VideoGameQuotes.Web/Services/QuoteService.cs @@ -172,7 +172,7 @@ namespace VideoGameQuotes.Web.Services { } totalCount = quotes.Count(); - return quotes.Skip(start - 1).Take(end - start + 1); + return quotes.OrderByDescending(quote => quote.Score).ThenBy(quote => quote.UpVotes).Skip(start - 1).Take(end - start + 1); } diff --git a/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj b/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj index eb30196..61a0816 100644 --- a/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj +++ b/Src/VideoGameQuotes.Web/VideoGameQuotes.Web.csproj @@ -160,6 +160,7 @@ + diff --git a/Src/VideoGameQuotes.Web/Views/Quote/DefaultBrowse.aspx b/Src/VideoGameQuotes.Web/Views/Quote/DefaultBrowse.aspx index a4cad2d..3870d5d 100644 --- a/Src/VideoGameQuotes.Web/Views/Quote/DefaultBrowse.aspx +++ b/Src/VideoGameQuotes.Web/Views/Quote/DefaultBrowse.aspx @@ -1,16 +1,14 @@ <%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" MasterPageFile="~/Views/Shared/Site.Master" %> Browse -

- Browse by: -

+

Browse

@@ -24,112 +22,5 @@
- + diff --git a/Src/VideoGameQuotes.Web/media/js/browse.js b/Src/VideoGameQuotes.Web/media/js/browse.js new file mode 100644 index 0000000..85f5945 --- /dev/null +++ b/Src/VideoGameQuotes.Web/media/js/browse.js @@ -0,0 +1,87 @@ +(function($, window, undefined){ + $(document).ready(function() { + var $browseMenu = $("#browse-default-menu"); + var $container = $("#browse-default-container"); + var $content = $("#browse-default-content"); + + var data = { + game: [], + system: [], + publisher: [], + category: [] + }; + + var renderData = function(data, itemRenderer) { + var $list = $("
    "), $item = $("
  1. ");; + $.each(data, function() { + $list.append($item.clone().append(itemRenderer(this))); + }); + + $content.append($list); + }; + + var getDataAndRender = function(type, itemRenderer) { + return function() { + var render = function() { + $browseMenu.hide(); + $container.show(); + renderData(data[type], itemRenderer); + }; + + if (data[type].length === 0) { + var $link = $(this); + $.ajax("/api/" + type + "/all", { + data: { sort: "alphabetical" }, + success: function(response, status, $xhr) { + if (response.Error !== null) { + alert(response.Error); + return; + } + + data[type] = response.Data.records; + render(); + }, + beforeSend: function() { $link.toggleClass("loading-link"); }, + complete: function() { $link.toggleClass("loading-link"); } + }); + } else { + render(); + } + + return false; + }; + }; + + var itemRendererByName = function(type) { + var $template = $(""); + return function(dto, count) { + return $template + .clone() + .attr("href", "/browse/" + type + "/" + dto.Id) + .text(dto.Name); + }; + }; + + var systemItemRenderer = function() { + var $template = $(""); + return function(dto, count) { + return $template + .clone() + .attr({ href: "/browse/system/" + dto.Id, title: dto.Name }) + .text(dto.Abbreviation); + }; + }(); + + $("#show-default-menu").click(function() { + $content.empty(); + $container.hide(); + $browseMenu.show(); + return false; + }); + + $("#browse-game").click(getDataAndRender("game", itemRendererByName("game"))); + $("#browse-system").click(getDataAndRender("system", systemItemRenderer)); + $("#browse-category").click(getDataAndRender("category", itemRendererByName("category"))); + $("#browse-publisher").click(getDataAndRender("publisher", itemRendererByName("publisher"))); + }); +}(jQuery, window)); \ No newline at end of file diff --git a/Src/VideoGameQuotes.Web/media/js/vgquotes.js b/Src/VideoGameQuotes.Web/media/js/vgquotes.js index 95a9178..a81e835 100644 --- a/Src/VideoGameQuotes.Web/media/js/vgquotes.js +++ b/Src/VideoGameQuotes.Web/media/js/vgquotes.js @@ -37,15 +37,9 @@ $.fn.clearModelErrors = function() { this - .find(".field-validation-error") - .remove() - .end() - .find(".input-validation-error") - .removeClass("input-validation-error") - .end() - .find(".error-summary") - .empty() - .hide(); + .find(".field-validation-error").remove().end() + .find(".input-validation-error").removeClass("input-validation-error").end() + .find(".error-summary").empty().hide(); return this; }; @@ -58,7 +52,7 @@ return; } - callback.call(null, data.Data[type + "s"][0]); + callback.call(null, data.Data.records[0]); }; };