deleting games works
This commit is contained in:
parent
52db4810ae
commit
f275b9192f
@ -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) {
|
||||
|
@ -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<int> systemIds);
|
||||
Game Save(Game game);
|
||||
Game FindById(int id);
|
||||
void Delete(int id);
|
||||
int GetQuotesForGame(Game game);
|
||||
}
|
||||
|
||||
public class GameService : IGameService {
|
||||
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.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();
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@
|
||||
<a href="#" id="create-game-link" class="create-new-link" title="create new game"></a>
|
||||
<% 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="delete-game-link" class="delete-link" title="delete currently selected game"></a>
|
||||
<% } %>
|
||||
</p>
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user