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

@ -11,17 +11,17 @@
<property name="Modified" column="modified" not-null="false" type="DateTime" />
<many-to-one name="Creator" column="creator" not-null="true" foreign-key="fk_quote_user"/>
<many-to-one name="Game" column="game_id" not-null="true" foreign-key="fk_quote_game" fetch="join" />
<!-- denormalization for performance purposes -->
<property name="Score" column="score" not-null="false" index="idx_score_upvotes" />
<property name="UpVotes" column="upvotes" not-null="false" index="idx_score_upvotes" />
<property name="DownVotes" column="downvotes" not-null="false" />
<set access="nosetter.camelcase" name="Categories" table="quote_category_map">
<key column="quote_id" />
<many-to-many class="VideoGameQuotes.Api.Category" column="category_id" />
</set>
<set access="nosetter.camelcase" name="Votes" table="vote" inverse="true" cascade="all-delete-orphan">
<key column="quote_id" not-null="true" />
<one-to-many class="VideoGameQuotes.Api.Vote" />

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Util;
using Portoa.Web.Controllers;
using VideoGameQuotes.Api;
@ -16,6 +17,28 @@ namespace VideoGameQuotes.Web.Controllers {
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)]
public JsonResult Edit(EditSystemModel model) {
if (model.SystemId < 1) {

View File

@ -71,7 +71,7 @@ namespace VideoGameQuotes.Web {
//bullshit route so that RenderAction works
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("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);
GamingSystem Save(GamingSystem system);
GamingSystem FindById(int id);
void Delete(int id);
IEnumerable<Game> GetGamesForSystem(GamingSystem system);
}
public class SystemService : ISystemService {
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.gameRepository = gameRepository;
}
[UnitOfWork]
@ -34,5 +38,16 @@ namespace VideoGameQuotes.Web.Services {
public GamingSystem FindById(int 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();
$.vgquotes.getResourceById("game", gameId, function(game) {
$("#edit-game-link").toggleClass("edit-link loading-link");
if (game === null) {
alert("Unable to fetch game " + gameId);
return;
}
$("#edit-game-link").toggleClass("edit-link loading-link");
toggleFormAndLink("game")();
$("#create-game-form > .edit-mode").val(1);
$("#create-game-form > fieldset > legend").text("Edit Game");
@ -578,12 +579,13 @@
var systemId = $link.siblings("input[name='SystemIds']").val();
$.vgquotes.getResourceById("system", systemId, function(system) {
$link.toggleClass("edit-link loading-link");
if (system === null) {
alert("Unable to fetch system " + systemId);
return;
}
$link.toggleClass("edit-link loading-link");
toggleFormAndLink("system")();
$("#create-system-form > .edit-mode").val(systemId);
$("#create-system-form > fieldset > legend").text("Edit System");
@ -596,6 +598,30 @@
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(){