2011-02-24 01:46:54 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Portoa.Persistence;
|
|
|
|
|
using Portoa.Util;
|
2011-02-23 01:30:53 +00:00
|
|
|
|
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 PublisherController : Controller {
|
|
|
|
|
private readonly IPublisherService publisherService;
|
|
|
|
|
|
|
|
|
|
public PublisherController(IPublisherService publisherService) {
|
|
|
|
|
this.publisherService = publisherService;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 01:46:54 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
|
|
|
|
public JsonResult Delete(int id) {
|
|
|
|
|
if (id < 1) {
|
|
|
|
|
return Json(this.CreateJsonResponse("Invalid ID"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var publisher = publisherService.FindById(id);
|
|
|
|
|
var games = publisherService.GetGamesForPublisher(publisher);
|
|
|
|
|
if (games.Any()) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse(
|
|
|
|
|
string.Format("The following games are still using this publisher: {0}", games.Implode(game => game.Name, ", "))
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
} catch (EntityNotFoundException) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("No publisher exists for ID " + id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
publisherService.Delete(id);
|
|
|
|
|
return Json(this.CreateJsonResponse());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
|
|
|
|
public JsonResult Edit(EditPublisherModel model) {
|
|
|
|
|
if (model.PublisherId < 1) {
|
|
|
|
|
ModelState.AddModelError("PublisherId", "Invalid publisher ID");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var publisher = publisherService.FindById(model.PublisherId);
|
|
|
|
|
publisher.Name = model.PublisherName;
|
|
|
|
|
publisher.Website = model.PublisherWebsite;
|
|
|
|
|
|
|
|
|
|
publisher = publisherService.Save(publisher);
|
|
|
|
|
return Json(this.CreateJsonResponse(data: publisher.ToDto()));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 01:30:53 +00:00
|
|
|
|
[HttpPost, VerifyUser]
|
2011-02-24 01:46:54 +00:00
|
|
|
|
public ActionResult Create(EditPublisherModel model) {
|
2011-02-23 01:30:53 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Some errors occurred."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (publisherService.FindByName(model.PublisherName) != null) {
|
|
|
|
|
return Json(this.CreateJsonResponse("A publisher with that name already exists."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var publisher = publisherService.Save(new Publisher { Name = model.PublisherName, Website = model.PublisherWebsite });
|
|
|
|
|
return Json(this.CreateJsonResponse(data: publisher.ToDto()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|