deleting systems works only if they are orphaned (no games attached)

This commit is contained in:
tmont 2011-02-24 01:10:52 +00:00
parent d256e2c3b6
commit fcaefea5f8
5 changed files with 71 additions and 7 deletions

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
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.Controllers; using Portoa.Web.Controllers;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
@ -16,6 +17,28 @@ namespace VideoGameQuotes.Web.Controllers {
this.systemService = systemService; this.systemService = systemService;
} }
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
public JsonResult Delete(int id) {
if (id < 1) {
return Json(this.CreateJsonResponse("Invalid ID"));
}
try {
var system = systemService.FindById(id);
var games = systemService.GetGamesForSystem(system);
if (games.Any()) {
return Json(this.CreateJsonErrorResponse(
string.Format("The following games are still using this system: {0}", games.Implode(game => game.Name, ", "))
));
}
} catch (EntityNotFoundException) {
return Json(this.CreateJsonErrorResponse("No system exists for ID " + id));
}
systemService.Delete(id);
return Json(this.CreateJsonResponse());
}
[HttpPost, VerifyUser(Group = UserGroup.Admin)] [HttpPost, VerifyUser(Group = UserGroup.Admin)]
public JsonResult Edit(EditSystemModel model) { public JsonResult Edit(EditSystemModel model) {
if (model.SystemId < 1) { if (model.SystemId < 1) {

View File

@ -71,7 +71,7 @@ namespace VideoGameQuotes.Web {
//bullshit route so that RenderAction works //bullshit route so that RenderAction works
routes.MapRoute("mainmenu", "home/mainmenu", new { controller = "Home", action = "MainMenu" }); routes.MapRoute("mainmenu", "home/mainmenu", new { controller = "Home", action = "MainMenu" });
routes.MapRoute("crud-default", "{controller}/{action}", null, new { controller = "system|publisher|game", action = "create|edit" }); routes.MapRoute("crud-default", "{controller}/{action}", null, new { controller = "system|publisher|game", action = "create|edit|delete" });
routes.MapRoute("users-paged", "admin/users/{start}-{end}", new { controller = "Admin", action = "Users" }, new { start = @"\d+", end = @"\d+" }); routes.MapRoute("users-paged", "admin/users/{start}-{end}", new { controller = "Admin", action = "Users" }, new { start = @"\d+", end = @"\d+" });
routes.MapRoute("admin", "admin/{action}", new { controller = "Admin", action = "Index" }, new { action = "users|create|flags|password" }); routes.MapRoute("admin", "admin/{action}", new { controller = "Admin", action = "Index" }, new { action = "users|create|flags|password" });

View File

@ -9,13 +9,17 @@ namespace VideoGameQuotes.Web.Services {
IEnumerable<GamingSystem> FindByNameOrAbbreviation(string name, string abbreviation); IEnumerable<GamingSystem> FindByNameOrAbbreviation(string name, string abbreviation);
GamingSystem Save(GamingSystem system); GamingSystem Save(GamingSystem system);
GamingSystem FindById(int id); GamingSystem FindById(int id);
void Delete(int id);
IEnumerable<Game> GetGamesForSystem(GamingSystem system);
} }
public class SystemService : ISystemService { public class SystemService : ISystemService {
private readonly IRepository<GamingSystem> repository; private readonly IRepository<GamingSystem> repository;
private readonly IRepository<Game> gameRepository;
public SystemService(IRepository<GamingSystem> repository) { public SystemService(IRepository<GamingSystem> repository, IRepository<Game> gameRepository) {
this.repository = repository; this.repository = repository;
this.gameRepository = gameRepository;
} }
[UnitOfWork] [UnitOfWork]
@ -34,5 +38,16 @@ namespace VideoGameQuotes.Web.Services {
public GamingSystem FindById(int id) { public GamingSystem FindById(int id) {
return repository.FindById(id); return repository.FindById(id);
} }
[UnitOfWork]
public void Delete(int id) {
repository.Delete(id);
}
public IEnumerable<Game> GetGamesForSystem(GamingSystem system) {
return gameRepository
.Records
.Where(game => game.Systems.Contains(system));
}
} }
} }

View File

@ -544,12 +544,13 @@
var gameId = $("#GameId").val(); var gameId = $("#GameId").val();
$.vgquotes.getResourceById("game", gameId, function(game) { $.vgquotes.getResourceById("game", gameId, function(game) {
$("#edit-game-link").toggleClass("edit-link loading-link");
if (game === null) { if (game === null) {
alert("Unable to fetch game " + gameId); alert("Unable to fetch game " + gameId);
return; return;
} }
$("#edit-game-link").toggleClass("edit-link loading-link");
toggleFormAndLink("game")(); toggleFormAndLink("game")();
$("#create-game-form > .edit-mode").val(1); $("#create-game-form > .edit-mode").val(1);
$("#create-game-form > fieldset > legend").text("Edit Game"); $("#create-game-form > fieldset > legend").text("Edit Game");
@ -578,12 +579,13 @@
var systemId = $link.siblings("input[name='SystemIds']").val(); var systemId = $link.siblings("input[name='SystemIds']").val();
$.vgquotes.getResourceById("system", systemId, function(system) { $.vgquotes.getResourceById("system", systemId, function(system) {
$link.toggleClass("edit-link loading-link");
if (system === null) { if (system === null) {
alert("Unable to fetch system " + systemId); alert("Unable to fetch system " + systemId);
return; return;
} }
$link.toggleClass("edit-link loading-link");
toggleFormAndLink("system")(); toggleFormAndLink("system")();
$("#create-system-form > .edit-mode").val(systemId); $("#create-system-form > .edit-mode").val(systemId);
$("#create-system-form > fieldset > legend").text("Edit System"); $("#create-system-form > fieldset > legend").text("Edit System");
@ -596,6 +598,30 @@
return false; return false;
}); });
$(".delete-system-link").click(function() {
var $link = $(this);
$link.toggleClass("delete-link loading-link");
var systemId = $link.siblings("input[name='SystemIds']").val();
$.ajax("/system/delete", {
type: "POST",
data: { id: systemId },
success: function(data, status, $xhr) {
$link.toggleClass("delete-link loading-link");
if (data.Error !== null) {
alert(data.Error);
return;
}
//remove checkbox for that system
$link.parent().empty();
}
});
return false;
});
}; };
(function(){ (function(){