2011-02-27 08:47:16 +00:00
|
|
|
|
using System;
|
2011-02-22 09:39:59 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
2011-02-24 01:10:52 +00:00
|
|
|
|
using Portoa.Persistence;
|
2011-02-23 21:53:51 +00:00
|
|
|
|
using Portoa.Util;
|
2011-03-07 20:56:34 +00:00
|
|
|
|
using Portoa.Web.Util;
|
2011-02-22 09:39:59 +00:00
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
using VideoGameQuotes.Web.Models;
|
|
|
|
|
using VideoGameQuotes.Web.Security;
|
|
|
|
|
using VideoGameQuotes.Web.Services;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Controllers {
|
|
|
|
|
public class SystemController : Controller {
|
|
|
|
|
private readonly ISystemService systemService;
|
|
|
|
|
|
|
|
|
|
public SystemController(ISystemService systemService) {
|
|
|
|
|
this.systemService = systemService;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 23:41:42 +00:00
|
|
|
|
protected new ActionResult Json(object data) {
|
|
|
|
|
return this.SerializeToJson(data);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 01:10:52 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Delete(int id) {
|
2011-02-24 01:10:52 +00:00
|
|
|
|
if (id < 1) {
|
|
|
|
|
return Json(this.CreateJsonResponse("Invalid ID"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var system = systemService.FindById(id);
|
|
|
|
|
var games = systemService.GetGamesForSystem(system);
|
|
|
|
|
if (games.Any()) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse(
|
|
|
|
|
string.Format("The following games are still using this system: {0}", games.Implode(game => game.Name, ", "))
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
} catch (EntityNotFoundException) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("No system exists for ID " + id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
systemService.Delete(id);
|
|
|
|
|
return Json(this.CreateJsonResponse());
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 21:53:51 +00:00
|
|
|
|
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
|
2011-03-03 23:41:42 +00:00
|
|
|
|
public ActionResult Edit(EditSystemModel model) {
|
2011-02-23 21:53:51 +00:00
|
|
|
|
if (model.SystemId < 1) {
|
|
|
|
|
ModelState.AddModelError("SystemId", "Invalid system ID");
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 08:47:16 +00:00
|
|
|
|
byte[] icon = null;
|
|
|
|
|
if (model.SystemIcon != null) {
|
|
|
|
|
icon = Convert.FromBase64String(model.SystemIcon);
|
|
|
|
|
if (icon.Length > 1024) {
|
|
|
|
|
ModelState.AddModelError("SystemIcon", "Icon must be smaller than 1KB");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 21:53:51 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var system = systemService.FindById(model.SystemId);
|
|
|
|
|
system.Name = model.SystemName;
|
|
|
|
|
|
|
|
|
|
system.Abbreviation = model.SystemAbbreviation;
|
|
|
|
|
system.ReleaseDate = model.SystemReleaseDate;
|
2011-02-27 08:47:16 +00:00
|
|
|
|
system.Icon = icon;
|
2011-02-23 21:53:51 +00:00
|
|
|
|
|
|
|
|
|
system = systemService.Save(system);
|
|
|
|
|
return Json(this.CreateJsonResponse(data: system.ToDto()));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-22 09:39:59 +00:00
|
|
|
|
[HttpPost, VerifyUser]
|
2011-02-23 21:53:51 +00:00
|
|
|
|
public ActionResult Create(EditSystemModel model) {
|
2011-02-27 08:47:16 +00:00
|
|
|
|
byte[] icon = null;
|
|
|
|
|
if (model.SystemIcon != null) {
|
|
|
|
|
icon = Convert.FromBase64String(model.SystemIcon);
|
|
|
|
|
if (icon.Length > 1024) {
|
|
|
|
|
ModelState.AddModelError("SystemIcon", "Icon must be smaller than 1KB");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-22 09:39:59 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
2011-02-23 01:30:53 +00:00
|
|
|
|
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
|
2011-02-22 09:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 01:30:53 +00:00
|
|
|
|
if (systemService.FindByNameOrAbbreviation(model.SystemName, model.SystemAbbreviation).Any()) {
|
|
|
|
|
return Json(this.CreateJsonResponse("A system with that name or abbreviation already exists."));
|
2011-02-22 09:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 08:47:16 +00:00
|
|
|
|
var system = new GamingSystem {
|
|
|
|
|
Abbreviation = model.SystemAbbreviation,
|
|
|
|
|
Name = model.SystemName,
|
|
|
|
|
ReleaseDate = model.SystemReleaseDate,
|
|
|
|
|
Icon = icon
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
system = systemService.Save(system);
|
2011-02-22 09:39:59 +00:00
|
|
|
|
return Json(this.CreateJsonResponse(data: system.ToDto()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|