2011-02-24 10:55:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2011-02-23 09:19:32 +00:00
|
|
|
|
using System.Web.Mvc;
|
2011-02-24 01:23:53 +00:00
|
|
|
|
using Portoa.Persistence;
|
2011-02-23 01:30:53 +00:00
|
|
|
|
using Portoa.Util;
|
|
|
|
|
using Portoa.Web.Controllers;
|
2011-02-26 04:29:21 +00:00
|
|
|
|
using Portoa.Web.Security;
|
2011-03-07 20:56:34 +00:00
|
|
|
|
using Portoa.Web.Util;
|
2011-02-23 01:30:53 +00:00
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
using VideoGameQuotes.Web.Models;
|
|
|
|
|
using VideoGameQuotes.Web.Security;
|
|
|
|
|
using VideoGameQuotes.Web.Services;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Controllers {
|
|
|
|
|
public class GameController : Controller {
|
|
|
|
|
private readonly IGameService gameService;
|
|
|
|
|
private readonly ICurrentUserProvider<User> userProvider;
|
|
|
|
|
|
|
|
|
|
public GameController(IGameService gameService, ICurrentUserProvider<User> userProvider) {
|
|
|
|
|
this.gameService = gameService;
|
|
|
|
|
this.userProvider = userProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 23:41:42 +00:00
|
|
|
|
protected new ActionResult Json(object data) {
|
|
|
|
|
return this.SerializeToJson(data);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 01:23:53 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Delete(int id) {
|
2011-02-24 01:23:53 +00:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 09:19:32 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Edit(EditGameModel model) {
|
2011-02-23 09:19:32 +00:00
|
|
|
|
if (model.GameId < 1) {
|
|
|
|
|
ModelState.AddModelError("GameId", "Invalid game ID");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 08:47:16 +00:00
|
|
|
|
byte[] icon = null;
|
2011-02-24 10:55:08 +00:00
|
|
|
|
if (model.GameIcon != null) {
|
|
|
|
|
icon = Convert.FromBase64String(model.GameIcon);
|
|
|
|
|
if (icon.Length > 1024) {
|
|
|
|
|
ModelState.AddModelError("GameIcon", "Icon must be smaller than 1KB");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 09:19:32 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var game = gameService.FindById(model.GameId);
|
|
|
|
|
game.Name = model.GameName;
|
2011-02-24 10:55:08 +00:00
|
|
|
|
game.Icon = icon;
|
2011-02-23 09:19:32 +00:00
|
|
|
|
game.Region = model.GameRegions;
|
|
|
|
|
game.Website = model.GameWebsite;
|
2011-02-24 10:55:08 +00:00
|
|
|
|
|
2011-02-23 09:19:32 +00:00
|
|
|
|
game.ClearPublishers();
|
|
|
|
|
game.ClearSystems();
|
|
|
|
|
(model.PublisherIds ?? Enumerable.Empty<int>()).Walk(id => game.AddPublisher(new Publisher { Id = id }));
|
|
|
|
|
(model.SystemIds ?? Enumerable.Empty<int>()).Walk(id => game.AddSystem(new GamingSystem { Id = id }));
|
|
|
|
|
|
|
|
|
|
game = gameService.Save(game);
|
|
|
|
|
return Json(this.CreateJsonResponse(data: game.ToDto()));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 01:30:53 +00:00
|
|
|
|
[HttpPost, VerifyUser]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Create(EditGameModel model) {
|
2011-02-27 08:47:16 +00:00
|
|
|
|
byte[] icon = null;
|
|
|
|
|
if (model.GameIcon != null) {
|
|
|
|
|
icon = Convert.FromBase64String(model.GameIcon);
|
|
|
|
|
if (icon.Length > 1024) {
|
|
|
|
|
ModelState.AddModelError("GameIcon", "Icon must be smaller than 1KB");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 01:30:53 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Some errors occurred."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gameService.FindByNameAndSystems(model.GameName, model.SystemIds) != null) {
|
|
|
|
|
return Json(this.CreateJsonResponse("A game with that name for one of those systems already exists."));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 08:47:16 +00:00
|
|
|
|
var game = new Game { Name = model.GameName, Website = model.GameWebsite, Region = model.GameRegions, Creator = userProvider.CurrentUser, Icon = icon };
|
2011-02-23 01:30:53 +00:00
|
|
|
|
model.SystemIds.Walk(id => game.AddSystem(new GamingSystem { Id = id }));
|
|
|
|
|
if (model.PublisherIds != null) {
|
|
|
|
|
model.PublisherIds.Walk(id => game.AddPublisher(new Publisher {Id = id}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
game = gameService.Save(game);
|
|
|
|
|
return Json(this.CreateJsonResponse(data: game.ToDto()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|