87 lines
3.0 KiB
C#
87 lines
3.0 KiB
C#
using System.Collections.Generic;
|
|
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]
|
|
public ActionResult Create(CreateGameModel model) {
|
|
if (!ModelState.IsValid) {
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
}
|
|
|
|
if (gameService.FindByNameAndSystems(model.Name, 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.Name, Website = model.Website, Region = model.Region, 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()));
|
|
}
|
|
}
|
|
|
|
public class PublisherController : Controller {
|
|
private readonly IPublisherService publisherService;
|
|
|
|
public PublisherController(IPublisherService publisherService) {
|
|
this.publisherService = publisherService;
|
|
}
|
|
|
|
[HttpPost, VerifyUser]
|
|
public ActionResult Create(CreatePublisherModel model) {
|
|
if (!ModelState.IsValid) {
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
}
|
|
|
|
if (publisherService.FindByName(model.Name) != null) {
|
|
return Json(this.CreateJsonResponse("A publisher with that name already exists"));
|
|
}
|
|
|
|
var publisher = publisherService.Save(new Publisher { Name = model.Name, Website = model.Website });
|
|
return Json(this.CreateJsonResponse(data: publisher.ToDto()));
|
|
}
|
|
}
|
|
|
|
public class SystemController : Controller {
|
|
private readonly ISystemService systemService;
|
|
|
|
public SystemController(ISystemService systemService) {
|
|
this.systemService = systemService;
|
|
}
|
|
|
|
[HttpPost, VerifyUser]
|
|
public ActionResult Create(CreateSystemModel model) {
|
|
if (!ModelState.IsValid) {
|
|
return Json(this.CreateJsonErrorResponse("Invalid request"));
|
|
}
|
|
|
|
if (systemService.FindByNameOrAbbreviation(model.Name, model.Abbreviation).Any()) {
|
|
return Json(this.CreateJsonResponse("A system with that name or abbreviation already exists"));
|
|
}
|
|
|
|
var system = systemService.Save(new GamingSystem { Abbreviation = model.Abbreviation, Name = model.Name, ReleaseDate = model.ReleaseDate });
|
|
return Json(this.CreateJsonResponse(data: system.ToDto()));
|
|
}
|
|
}
|
|
} |