deleting games works

This commit is contained in:
tmont 2011-02-24 01:23:53 +00:00
parent 52db4810ae
commit f275b9192f
4 changed files with 69 additions and 5 deletions

View File

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Util; using Portoa.Util;
using Portoa.Web; using Portoa.Web;
using Portoa.Web.Controllers; using Portoa.Web.Controllers;
@ -18,6 +19,28 @@ namespace VideoGameQuotes.Web.Controllers {
this.userProvider = userProvider; 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)] [HttpPost, VerifyUser(Group = UserGroup.Admin)]
public JsonResult Edit(EditGameModel model) { public JsonResult Edit(EditGameModel model) {
if (model.GameId < 1) { if (model.GameId < 1) {

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using Portoa.Persistence; using Portoa.Persistence;
@ -10,13 +11,17 @@ namespace VideoGameQuotes.Web.Services {
Game FindByNameAndSystems(string name, IEnumerable<int> systemIds); Game FindByNameAndSystems(string name, IEnumerable<int> systemIds);
Game Save(Game game); Game Save(Game game);
Game FindById(int id); Game FindById(int id);
void Delete(int id);
int GetQuotesForGame(Game game);
} }
public class GameService : IGameService { public class GameService : IGameService {
private readonly IRepository<Game> repository; private readonly IRepository<Game> repository;
private readonly IRepository<Quote> quoteRepository;
public GameService(IRepository<Game> repository) { public GameService(IRepository<Game> repository, IRepository<Quote> quoteRepository) {
this.repository = repository; this.repository = repository;
this.quoteRepository = quoteRepository;
} }
[UnitOfWork] [UnitOfWork]
@ -35,5 +40,18 @@ namespace VideoGameQuotes.Web.Services {
public Game FindById(int id) { public Game FindById(int id) {
return repository.FindById(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();
}
} }
} }

View File

@ -14,6 +14,7 @@
<a href="#" id="create-game-link" class="create-new-link" title="create new game"></a> <a href="#" id="create-game-link" class="create-new-link" title="create new game"></a>
<% if (Model.CurrentUser != null && Model.CurrentUser.Group >= UserGroup.Admin) { %> <% if (Model.CurrentUser != null && Model.CurrentUser.Group >= UserGroup.Admin) { %>
<a href="#" id="edit-game-link" class="edit-link" title="edit currently selected game"></a> <a href="#" id="edit-game-link" class="edit-link" title="edit currently selected game"></a>
<a href="#" id="delete-game-link" class="delete-link" title="delete currently selected game"></a>
<% } %> <% } %>
</p> </p>

View File

@ -572,6 +572,29 @@
return false; 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() { $(".edit-system-link").click(function() {
var $link = $(this); var $link = $(this);
$link.toggleClass("edit-link loading-link"); $link.toggleClass("edit-link loading-link");
@ -608,8 +631,6 @@
type: "POST", type: "POST",
data: { id: systemId }, data: { id: systemId },
success: function(data, status, $xhr) { success: function(data, status, $xhr) {
$link.toggleClass("delete-link loading-link");
if (data.Error !== null) { if (data.Error !== null) {
alert(data.Error); alert(data.Error);
return; return;
@ -617,7 +638,8 @@
//remove checkbox for that system //remove checkbox for that system
$link.parent().empty(); $link.parent().empty();
} },
complete: function() { $link.toggleClass("delete-link loading-link"); }
}); });
return false; return false;