From f275b9192ffa51ad2594266ed291168f6353f721 Mon Sep 17 00:00:00 2001 From: tmont Date: Thu, 24 Feb 2011 01:23:53 +0000 Subject: [PATCH] deleting games works --- .../Controllers/GameController.cs | 23 +++++++++++++++ .../Services/GameService.cs | 22 +++++++++++++-- .../Views/Quote/EditQuoteForm.ascx | 1 + Src/VideoGameQuotes.Web/media/js/vgquotes.js | 28 +++++++++++++++++-- 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/Src/VideoGameQuotes.Web/Controllers/GameController.cs b/Src/VideoGameQuotes.Web/Controllers/GameController.cs index 2faffb6..106c625 100644 --- a/Src/VideoGameQuotes.Web/Controllers/GameController.cs +++ b/Src/VideoGameQuotes.Web/Controllers/GameController.cs @@ -1,5 +1,6 @@ using System.Linq; using System.Web.Mvc; +using Portoa.Persistence; using Portoa.Util; using Portoa.Web; using Portoa.Web.Controllers; @@ -18,6 +19,28 @@ namespace VideoGameQuotes.Web.Controllers { this.userProvider = userProvider; } + [HttpPost, VerifyUser(Group = UserGroup.Admin)] + public JsonResult Delete(int id) { + if (id < 1) { + return Json(this.CreateJsonResponse("Invalid ID")); + } + + try { + var game = gameService.FindById(id); + var numQuotes = gameService.GetQuotesForGame(game); + if (numQuotes > 0) { + return Json(this.CreateJsonErrorResponse( + string.Format("There are {0} quotes that are still using this game", numQuotes) + )); + } + } catch (EntityNotFoundException) { + return Json(this.CreateJsonErrorResponse("No game exists for ID " + id)); + } + + gameService.Delete(id); + return Json(this.CreateJsonResponse()); + } + [HttpPost, VerifyUser(Group = UserGroup.Admin)] public JsonResult Edit(EditGameModel model) { if (model.GameId < 1) { diff --git a/Src/VideoGameQuotes.Web/Services/GameService.cs b/Src/VideoGameQuotes.Web/Services/GameService.cs index 8f00faf..544a195 100644 --- a/Src/VideoGameQuotes.Web/Services/GameService.cs +++ b/Src/VideoGameQuotes.Web/Services/GameService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using Portoa.Persistence; @@ -10,13 +11,17 @@ namespace VideoGameQuotes.Web.Services { Game FindByNameAndSystems(string name, IEnumerable systemIds); Game Save(Game game); Game FindById(int id); + void Delete(int id); + int GetQuotesForGame(Game game); } public class GameService : IGameService { private readonly IRepository repository; + private readonly IRepository quoteRepository; - public GameService(IRepository repository) { + public GameService(IRepository repository, IRepository quoteRepository) { this.repository = repository; + this.quoteRepository = quoteRepository; } [UnitOfWork] @@ -35,5 +40,18 @@ namespace VideoGameQuotes.Web.Services { public Game FindById(int id) { return repository.FindById(id); } + + [UnitOfWork] + public void Delete(int id) { + repository.Delete(id); + } + + [UnitOfWork] + public int GetQuotesForGame(Game game) { + return quoteRepository + .Records + .Where(quote => quote.Game == game) + .Count(); + } } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Web/Views/Quote/EditQuoteForm.ascx b/Src/VideoGameQuotes.Web/Views/Quote/EditQuoteForm.ascx index c093168..0f9a1a8 100644 --- a/Src/VideoGameQuotes.Web/Views/Quote/EditQuoteForm.ascx +++ b/Src/VideoGameQuotes.Web/Views/Quote/EditQuoteForm.ascx @@ -14,6 +14,7 @@ <% if (Model.CurrentUser != null && Model.CurrentUser.Group >= UserGroup.Admin) { %> + <% } %>

diff --git a/Src/VideoGameQuotes.Web/media/js/vgquotes.js b/Src/VideoGameQuotes.Web/media/js/vgquotes.js index 34c9eec..b980186 100644 --- a/Src/VideoGameQuotes.Web/media/js/vgquotes.js +++ b/Src/VideoGameQuotes.Web/media/js/vgquotes.js @@ -572,6 +572,29 @@ return false; }); + $("#delete-game-link").click(function() { + var $link = $(this); + $link.toggleClass("delete-link loading-link"); + var gameId = $("#GameId").val(); + + $.ajax("/game/delete", { + type: "POST", + data: { id: gameId }, + success: function(data, status, $xhr) { + if (data.Error !== null) { + alert(data.Error); + return; + } + + //remove game from the dropdown + $("#GameId option[value='" + gameId + "']").remove(); + }, + complete: function() { $link.toggleClass("delete-link loading-link"); } + }); + + return false; + }); + $(".edit-system-link").click(function() { var $link = $(this); $link.toggleClass("edit-link loading-link"); @@ -608,8 +631,6 @@ type: "POST", data: { id: systemId }, success: function(data, status, $xhr) { - $link.toggleClass("delete-link loading-link"); - if (data.Error !== null) { alert(data.Error); return; @@ -617,7 +638,8 @@ //remove checkbox for that system $link.parent().empty(); - } + }, + complete: function() { $link.toggleClass("delete-link loading-link"); } }); return false;