edit and delete publishers
This commit is contained in:
parent
f275b9192f
commit
7c2fe503de
@ -1,4 +1,7 @@
|
||||
using System.Web.Mvc;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Portoa.Persistence;
|
||||
using Portoa.Util;
|
||||
using Portoa.Web.Controllers;
|
||||
using VideoGameQuotes.Api;
|
||||
using VideoGameQuotes.Web.Models;
|
||||
@ -13,8 +16,48 @@ namespace VideoGameQuotes.Web.Controllers {
|
||||
this.publisherService = publisherService;
|
||||
}
|
||||
|
||||
[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()));
|
||||
}
|
||||
|
||||
[HttpPost, VerifyUser]
|
||||
public ActionResult Create(CreatePublisherModel model) {
|
||||
public ActionResult Create(EditPublisherModel model) {
|
||||
if (!ModelState.IsValid) {
|
||||
return Json(this.CreateJsonErrorResponse("Some errors occurred."));
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace VideoGameQuotes.Web.Models {
|
||||
public class CreatePublisherModel {
|
||||
public class EditPublisherModel {
|
||||
[Required(ErrorMessage = "Publishers must have a name, douchebag")]
|
||||
public string PublisherName { get; set; }
|
||||
public string PublisherWebsite { get; set; }
|
||||
public int PublisherId { get; set; }
|
||||
}
|
||||
}
|
@ -81,7 +81,7 @@ namespace VideoGameQuotes.Web.Models {
|
||||
return table.ToString(TagRenderMode.Normal);
|
||||
}
|
||||
|
||||
public string MakePublisherTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 6) {
|
||||
public string MakePublisherTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 4) {
|
||||
return MakeTable(
|
||||
"publisher-checkbox-table",
|
||||
cellsPerRow,
|
||||
|
@ -1,4 +1,6 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Portoa.Persistence;
|
||||
using VideoGameQuotes.Api;
|
||||
@ -9,13 +11,18 @@ namespace VideoGameQuotes.Web.Services {
|
||||
[CanBeNull]
|
||||
Publisher FindByName(string name);
|
||||
Publisher Save(Publisher publisher);
|
||||
IEnumerable<Game> GetGamesForPublisher(Publisher publisher);
|
||||
Publisher FindById(int id);
|
||||
void Delete(int id);
|
||||
}
|
||||
|
||||
public class PublisherService : IPublisherService {
|
||||
private readonly IRepository<Publisher> repository;
|
||||
private readonly IRepository<Game> gameRepository;
|
||||
|
||||
public PublisherService(IRepository<Publisher> repository) {
|
||||
public PublisherService(IRepository<Publisher> repository, IRepository<Game> gameRepository) {
|
||||
this.repository = repository;
|
||||
this.gameRepository = gameRepository;
|
||||
}
|
||||
|
||||
[UnitOfWork]
|
||||
@ -27,5 +34,22 @@ namespace VideoGameQuotes.Web.Services {
|
||||
public Publisher Save(Publisher publisher) {
|
||||
return repository.Save(publisher);
|
||||
}
|
||||
|
||||
[UnitOfWork]
|
||||
public IEnumerable<Game> GetGamesForPublisher(Publisher publisher) {
|
||||
return gameRepository
|
||||
.Records
|
||||
.Where(game => game.Publishers.Contains(publisher));
|
||||
}
|
||||
|
||||
[UnitOfWork]
|
||||
public Publisher FindById(int id) {
|
||||
return repository.FindById(id);
|
||||
}
|
||||
|
||||
[UnitOfWork]
|
||||
public void Delete(int id) {
|
||||
repository.Delete(id);
|
||||
}
|
||||
}
|
||||
}
|
@ -91,7 +91,7 @@
|
||||
<Compile Include="Controllers\PublisherController.cs" />
|
||||
<Compile Include="Controllers\SystemController.cs" />
|
||||
<Compile Include="Models\EditGameModel.cs" />
|
||||
<Compile Include="Models\CreatePublisherModel.cs" />
|
||||
<Compile Include="Models\EditPublisherModel.cs" />
|
||||
<Compile Include="Services\GameService.cs" />
|
||||
<Compile Include="Services\PublisherService.cs" />
|
||||
<Compile Include="Services\SystemService.cs" />
|
||||
|
@ -358,11 +358,10 @@
|
||||
|
||||
$("#create-system-submit").click(function() {
|
||||
var $container = $("#create-system-form");
|
||||
|
||||
var data = { SystemName: $("#SystemName").val(), SystemAbbreviation: $("#SystemAbbreviation").val(), SystemReleaseDate: $("#SystemReleaseDate").val() };
|
||||
|
||||
var url = "/system/create";
|
||||
var systemId = $container.find("> .edit-mode").val();
|
||||
|
||||
if (systemId > 0) {
|
||||
url = "/system/edit";
|
||||
data.SystemId = systemId;
|
||||
@ -394,9 +393,18 @@
|
||||
|
||||
$("#create-publisher-submit").click(function() {
|
||||
var $container = $("#create-publisher-form");
|
||||
$.ajax("/publisher/create", {
|
||||
var data = { PublisherName: $("#PublisherName").val(), PublisherWebsite: $("#PublisherWebsite").val() };
|
||||
var url = "/publisher/create";
|
||||
var publisherId = $container.find("> .edit-mode").val();
|
||||
|
||||
if (publisherId > 0) {
|
||||
url = "/publisher/edit";
|
||||
data.PublisherId = publisherId;
|
||||
}
|
||||
|
||||
$.ajax(url, {
|
||||
type: "POST",
|
||||
data: { PublisherName: $("#PublisherName").val(), PublisherWebsite: $("#PublisherWebsite").val() },
|
||||
data: data,
|
||||
beforeSend: function() { $container.clearModelErrors(); },
|
||||
success: function(data, statux, $xhr) {
|
||||
if (data.Error !== null) {
|
||||
@ -404,7 +412,13 @@
|
||||
return;
|
||||
}
|
||||
|
||||
addNewCheckbox($("#publisher-checkbox-table"), "PublisherIds", data.Data.Id, data.Data.Name, 8);
|
||||
if (publisherId <= 0) {
|
||||
addNewCheckbox($("#publisher-checkbox-table"), "PublisherIds", data.Data.Id, data.Data.Name, 4);
|
||||
} else {
|
||||
//update publisher name
|
||||
$("#publisher-checkbox-table").find("input[value='" + data.Data.Id +"'] + label").text(data.Data.Name);
|
||||
}
|
||||
|
||||
toggleFormAndLink("publisher")();
|
||||
}
|
||||
});
|
||||
@ -613,7 +627,7 @@
|
||||
$("#create-system-form > .edit-mode").val(systemId);
|
||||
$("#create-system-form > fieldset > legend").text("Edit System");
|
||||
|
||||
//populate system form with systemdata
|
||||
//populate system form with system data
|
||||
$("#SystemName").val(system.Name);
|
||||
$("#SystemAbbreviation").val(system.Abbreviation);
|
||||
$("#SystemReleaseDate").val($.vgquotes.parseAndFormatDate(system.ReleaseDate));
|
||||
@ -644,6 +658,54 @@
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
$(".edit-publisher-link").click(function() {
|
||||
var $link = $(this);
|
||||
$link.toggleClass("edit-link loading-link");
|
||||
|
||||
var publisherId = $link.siblings("input[name='PublisherIds']").val();
|
||||
|
||||
$.vgquotes.getResourceById("publisher", publisherId, function(publisher) {
|
||||
$link.toggleClass("edit-link loading-link");
|
||||
|
||||
if (publisher === null) {
|
||||
alert("Unable to fetch publisher " + publisherId);
|
||||
return;
|
||||
}
|
||||
|
||||
toggleFormAndLink("publisher")();
|
||||
$("#create-publisher-form > .edit-mode").val(publisherId);
|
||||
$("#create-publisher-form > fieldset > legend").text("Edit Publisher");
|
||||
|
||||
//populate publisher form with publisher data
|
||||
$("#PublisherName").val(publisher.Name);
|
||||
$("#PublisherWebsite").val(publisher.Website);
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
$(".delete-publisher-link").click(function() {
|
||||
var $link = $(this);
|
||||
$link.toggleClass("delete-link loading-link");
|
||||
|
||||
$.ajax("/publisher/delete", {
|
||||
type: "POST",
|
||||
data: { id: $link.siblings("input[name='PublisherIds']").val() },
|
||||
success: function(data, status, $xhr) {
|
||||
if (data.Error !== null) {
|
||||
alert(data.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
//remove checkbox for that publisher
|
||||
$link.parent().empty();
|
||||
},
|
||||
complete: function() { $link.toggleClass("delete-link loading-link"); }
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
(function(){
|
||||
|
Loading…
Reference in New Issue
Block a user