vgquotes/Src/VideoGameQuotes.Web/Controllers/GameController.cs
2011-02-23 21:53:51 +00:00

65 lines
2.3 KiB
C#

using System.Linq;
using System.Web.Mvc;
using Portoa.Util;
using Portoa.Web;
using Portoa.Web.Controllers;
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;
}
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
public JsonResult Edit(EditGameModel model) {
if (model.GameId < 1) {
ModelState.AddModelError("GameId", "Invalid game ID");
}
if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
}
var game = gameService.FindById(model.GameId);
game.Name = model.GameName;
game.Region = model.GameRegions;
game.Website = model.GameWebsite;
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()));
}
[HttpPost, VerifyUser]
public JsonResult Create(EditGameModel model) {
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."));
}
var game = new Game { Name = model.GameName, Website = model.GameWebsite, Region = model.GameRegions, Creator = userProvider.CurrentUser };
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()));
}
}
}