vgquotes/Src/VideoGameQuotes.Web/Controllers/SystemController.cs
tmont 6d17cfa573 * fixed some bugs that occurred when there are no quotes in the database
* fixed some bullshit route stuff that mono failed to implement correctly
* don't use JsonResult because it's broken on Mono
2011-03-03 23:41:42 +00:00

104 lines
3.1 KiB
C#

using System;
using System.Linq;
using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Util;
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 SystemController : Controller {
private readonly ISystemService systemService;
public SystemController(ISystemService systemService) {
this.systemService = systemService;
}
protected new ActionResult Json(object data) {
return this.SerializeToJson(data);
}
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
public ActionResult Delete(int id) {
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());
}
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
public ActionResult Edit(EditSystemModel model) {
if (model.SystemId < 1) {
ModelState.AddModelError("SystemId", "Invalid system ID");
}
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");
}
}
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;
system.Icon = icon;
system = systemService.Save(system);
return Json(this.CreateJsonResponse(data: system.ToDto()));
}
[HttpPost, VerifyUser]
public ActionResult Create(EditSystemModel model) {
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");
}
}
if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
}
if (systemService.FindByNameOrAbbreviation(model.SystemName, model.SystemAbbreviation).Any()) {
return Json(this.CreateJsonResponse("A system with that name or abbreviation already exists."));
}
var system = new GamingSystem {
Abbreviation = model.SystemAbbreviation,
Name = model.SystemName,
ReleaseDate = model.SystemReleaseDate,
Icon = icon
};
system = systemService.Save(system);
return Json(this.CreateJsonResponse(data: system.ToDto()));
}
}
}