editing systems works

This commit is contained in:
tmont 2011-02-23 21:53:51 +00:00
parent 6b41bd609e
commit d256e2c3b6
10 changed files with 125 additions and 38 deletions

View File

@ -1,13 +1,8 @@
using System.ComponentModel.DataAnnotations; using System.Linq;
using System.IO;
using System.Linq;
using System.Net;
using System.Web.Mvc; using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Util; using Portoa.Util;
using Portoa.Web; using Portoa.Web;
using Portoa.Web.Controllers; using Portoa.Web.Controllers;
using Portoa.Web.Results;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
using VideoGameQuotes.Web.Models; using VideoGameQuotes.Web.Models;
using VideoGameQuotes.Web.Security; using VideoGameQuotes.Web.Security;

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using Portoa.Util;
using Portoa.Web.Controllers; using Portoa.Web.Controllers;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
using VideoGameQuotes.Web.Models; using VideoGameQuotes.Web.Models;
@ -15,8 +16,28 @@ namespace VideoGameQuotes.Web.Controllers {
this.systemService = systemService; this.systemService = systemService;
} }
[HttpPost, VerifyUser(Group = UserGroup.Admin)]
public JsonResult Edit(EditSystemModel model) {
if (model.SystemId < 1) {
ModelState.AddModelError("SystemId", "Invalid system ID");
}
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 = systemService.Save(system);
return Json(this.CreateJsonResponse(data: system.ToDto()));
}
[HttpPost, VerifyUser] [HttpPost, VerifyUser]
public ActionResult Create(CreateSystemModel model) { public ActionResult Create(EditSystemModel model) {
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Some errors occurred")); return Json(this.CreateJsonErrorResponse("Some errors occurred"));
} }

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using JetBrains.Annotations;
using Portoa.Validation.DataAnnotations; using Portoa.Validation.DataAnnotations;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
@ -80,21 +81,21 @@ namespace VideoGameQuotes.Web.Models {
return table.ToString(TagRenderMode.Normal); return table.ToString(TagRenderMode.Normal);
} }
public string MakePublisherTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 8) { public string MakePublisherTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 6) {
return MakeTable( return MakeTable(
"publisher-checkbox-table", "publisher-checkbox-table",
cellsPerRow, cellsPerRow,
AllPublishers, AllPublishers,
publisher => CreateCheckbox("PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name) publisher => CreateCheckbox("PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name, "publisher")
); );
} }
public string MakeSystemTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 8) { public string MakeSystemTable(HtmlHelper<EditQuoteModel> html, int cellsPerRow = 6) {
return MakeTable( return MakeTable(
"system-checkbox-table", "system-checkbox-table",
cellsPerRow, cellsPerRow,
AllSystems, AllSystems,
system => CreateCheckbox("SystemIds", system.Id, "system_" + system.Id, system.Abbreviation) system => CreateCheckbox("SystemIds", system.Id, "system_" + system.Id, system.Abbreviation, "system")
); );
} }
@ -104,7 +105,7 @@ namespace VideoGameQuotes.Web.Models {
"category-checkbox-table", "category-checkbox-table",
cellsPerRow, cellsPerRow,
AllCategories, AllCategories,
category => CreateCheckbox("CategoryIds", category.Id, "category_" + category.Id, category.Name, categoryIds.Contains(category.Id)) category => CreateCheckbox("CategoryIds", category.Id, "category_" + category.Id, category.Name, "category", categoryIds.Contains(category.Id))
); );
} }
@ -117,15 +118,24 @@ namespace VideoGameQuotes.Web.Models {
); );
} }
private static string CreateCheckbox(string name, object value, string id, string labelText, bool isChecked = false) { private string CreateCheckbox(string name, object value, string id, string labelText, string editType = null, bool isChecked = false) {
return string.Format( return string.Format(
"<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" id=\"{2}\"{4}/><label for=\"{2}\">{3}</label>", "<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" id=\"{2}\"{4}/><label for=\"{2}\">{3}</label>{5}",
name, name,
value, value,
id, id,
labelText, labelText,
isChecked ? " checked=\"checked\"" : "" isChecked ? " checked=\"checked\"" : "",
GetEditAndDeleteLinks(editType)
); );
} }
private string GetEditAndDeleteLinks([CanBeNull]string editType) {
if (editType == null || CurrentUser.Group < UserGroup.Admin) {
return string.Empty;
}
return string.Format("<a href=\"#\" class=\"edit-link edit-{0}-link\" title=\"edit\"></a><a href=\"#\" class=\"delete-link delete-{0}-link\" title=\"delete\"></a>", editType);
}
} }
} }

View File

@ -2,7 +2,8 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
namespace VideoGameQuotes.Web.Models { namespace VideoGameQuotes.Web.Models {
public class CreateSystemModel { public class EditSystemModel {
public int SystemId { get; set; }
[Required(ErrorMessage = "Systems must have a name, fatty")] [Required(ErrorMessage = "Systems must have a name, fatty")]
public string SystemName { get; set; } public string SystemName { get; set; }
[Required(ErrorMessage = "Abbreviation is required, ugly"), StringLength(12, ErrorMessage = "Abbreviations cannot be longer than 12 characters, flabcakes")] [Required(ErrorMessage = "Abbreviation is required, ugly"), StringLength(12, ErrorMessage = "Abbreviations cannot be longer than 12 characters, flabcakes")]

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Portoa.Persistence; using Portoa.Persistence;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
@ -7,6 +8,7 @@ namespace VideoGameQuotes.Web.Services {
public interface ISystemService { public interface ISystemService {
IEnumerable<GamingSystem> FindByNameOrAbbreviation(string name, string abbreviation); IEnumerable<GamingSystem> FindByNameOrAbbreviation(string name, string abbreviation);
GamingSystem Save(GamingSystem system); GamingSystem Save(GamingSystem system);
GamingSystem FindById(int id);
} }
public class SystemService : ISystemService { public class SystemService : ISystemService {
@ -27,5 +29,10 @@ namespace VideoGameQuotes.Web.Services {
public GamingSystem Save(GamingSystem system) { public GamingSystem Save(GamingSystem system) {
return repository.Save(system); return repository.Save(system);
} }
[UnitOfWork]
public GamingSystem FindById(int id) {
return repository.FindById(id);
}
} }
} }

View File

@ -101,7 +101,7 @@
<Compile Include="Models\BrowseModelBinder.cs" /> <Compile Include="Models\BrowseModelBinder.cs" />
<Compile Include="Models\ChangePasswordModel.cs" /> <Compile Include="Models\ChangePasswordModel.cs" />
<Compile Include="Models\CreateAdminModel.cs" /> <Compile Include="Models\CreateAdminModel.cs" />
<Compile Include="Models\CreateSystemModel.cs" /> <Compile Include="Models\EditSystemModel.cs" />
<Compile Include="Models\InvalidUsernameModel.cs" /> <Compile Include="Models\InvalidUsernameModel.cs" />
<Compile Include="Models\MainMenuModel.cs" /> <Compile Include="Models\MainMenuModel.cs" />
<Compile Include="Models\QualifiedBrowseModel.cs" /> <Compile Include="Models\QualifiedBrowseModel.cs" />
@ -141,6 +141,7 @@
<Content Include="media\css\quote.css" /> <Content Include="media\css\quote.css" />
<Content Include="media\css\reset.css" /> <Content Include="media\css\reset.css" />
<Content Include="media\images\add.png" /> <Content Include="media\images\add.png" />
<Content Include="media\images\delete.png" />
<Content Include="media\images\favicon.png" /> <Content Include="media\images\favicon.png" />
<Content Include="media\images\loading.gif" /> <Content Include="media\images\loading.gif" />
<Content Include="media\images\search.png" /> <Content Include="media\images\search.png" />

View File

@ -19,6 +19,7 @@
<div id="create-game-form" class="subform"> <div id="create-game-form" class="subform">
<input type="hidden" value="0" class="edit-mode" /> <input type="hidden" value="0" class="edit-mode" />
<input type="hidden" value="0" id="SystemId" />
<fieldset> <fieldset>
<legend>Create new game</legend> <legend>Create new game</legend>
@ -46,7 +47,7 @@
<span id="SystemIds" class="label">Systems</span> <span id="SystemIds" class="label">Systems</span>
<br /> <br />
<%= Model.MakeSystemTable(Html) %> <%= Model.MakeSystemTable(Html) %>
<a href="#" id="create-system-link" class="create-new-link">Create new system&hellip;</a> <a href="#" id="create-system-link" class="create-new-link"></a>
</div> </div>
<div id="create-system-form" class="subform"> <div id="create-system-form" class="subform">
@ -83,7 +84,7 @@
<span class="label">Publishers</span> <span class="label">Publishers</span>
<br /> <br />
<%= Model.MakePublisherTable(Html) %> <%= Model.MakePublisherTable(Html) %>
<a href="#" id="create-publisher-link" class="create-new-link">Create new publisher</a> <a href="#" id="create-publisher-link" class="create-new-link"></a>
</div> </div>
<div id="create-publisher-form" class="subform"> <div id="create-publisher-form" class="subform">
@ -127,7 +128,7 @@
</p> </p>
<%= Model.MakeCategoryTable(Html) %> <%= Model.MakeCategoryTable(Html) %>
<a href="#" id="create-category-link" class="create-new-link">Create new category</a> <a href="#" id="create-category-link" class="create-new-link"></a>
</div> </div>
<% if (Model.QuoteId > 0) { %> <% if (Model.QuoteId > 0) { %>

View File

@ -152,8 +152,9 @@ ul.menu {
width: 500px; width: 500px;
height: 120px; height: 120px;
} }
.create-new-link, .edit-link, .loading-link { .create-new-link, .edit-link, .loading-link, .delete-link {
padding-left: 16px; padding-left: 16px;
margin-left: 2px;
} }
.create-new-link { .create-new-link {
background: transparent url(/media/images/add.png) left center no-repeat; background: transparent url(/media/images/add.png) left center no-repeat;
@ -164,6 +165,9 @@ ul.menu {
.loading-link { .loading-link {
background: transparent url(/media/images/loading.gif) left center no-repeat; background: transparent url(/media/images/loading.gif) left center no-repeat;
} }
.delete-link {
background: transparent url(/media/images/delete.png) left center no-repeat;
}
#browse-default-menu li { #browse-default-menu li {
margin-bottom: 2px; margin-bottom: 2px;

Binary file not shown.

After

Width:  |  Height:  |  Size: 715 B

View File

@ -53,11 +53,17 @@
}; };
var cache = { var cache = {
games: { } game: { },
system: { },
category: { },
publisher: { }
}; };
return { return {
refresh: function() { }, refresh: function() { },
parseDate: function(jsonDate) { return new Date(parseInt(jsonDate.substr(6))); },
formatDate: function(date) { return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(); },
parseAndFormatDate: function(jsonDate) { return $.vgquotes.formatDate($.vgquotes.parseDate(jsonDate)); },
ajaxErrorHandler: function(xhr) { ajaxErrorHandler: function(xhr) {
console.dir(xhr); console.dir(xhr);
alert("An error occurred (" + xhr.statusCode + ")"); alert("An error occurred (" + xhr.statusCode + ")");
@ -70,21 +76,21 @@
}, },
loadingGif: "/media/images/loading.gif", loadingGif: "/media/images/loading.gif",
editIcon: "/media/images/pencil.png", editIcon: "/media/images/pencil.png",
getGame: function(id, callback) { getResourceById: function(type, id, callback) {
id = id.toString(); id = id.toString();
// if (typeof(cache.games[id]) !== "undefined") { // if (typeof(cache[type][id]) !== "undefined") {
// callback.call(null, cache.games[id]); // callback.call(null, cache[type][id]);
// return; // return;
// } // }
callApi("/api/game/" + id, "GET", null, function(data) { callApi("/api/" + type + "/" + id, "GET", null, function(data) {
if (typeof(data) === "undefined" || typeof(data.Error) === "undefined" || data.Error !== null) { if (typeof(data) === "undefined" || typeof(data.Error) === "undefined" || data.Error !== null) {
callback.call(null, null); callback.call(null, null);
return; return;
} }
cache.games[id] = data.Data.games[0]; cache[type][id] = data.Data[type + "s"][0];
callback.call(null, cache.games[id]); callback.call(null, cache[type][id]);
}); });
} }
}; };
@ -319,7 +325,7 @@
.find("input[type='text']").val("").end() .find("input[type='text']").val("").end()
.find("input[type='checkbox']").removeAttr("checked").end() .find("input[type='checkbox']").removeAttr("checked").end()
.find("> .edit-mode").val(0).end() .find("> .edit-mode").val(0).end()
.find("> legend").text("Create new " + type).end() .find("> fieldset > legend").text("Create new " + type).end()
.toggle(); .toggle();
$("#" + type + "-select").toggle(); $("#" + type + "-select").toggle();
@ -352,9 +358,19 @@
$("#create-system-submit").click(function() { $("#create-system-submit").click(function() {
var $container = $("#create-system-form"); var $container = $("#create-system-form");
$.ajax("/system/create", {
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;
}
$.ajax(url, {
type: "POST", type: "POST",
data: { SystemName: $("#SystemName").val(), SystemAbbreviation: $("#SystemAbbreviation").val(), SystemReleaseDate: $("#SystemReleaseDate").val() }, data: data,
beforeSend: function() { $container.clearModelErrors(); }, beforeSend: function() { $container.clearModelErrors(); },
success: function(data, statux, $xhr) { success: function(data, statux, $xhr) {
if (data.Error !== null) { if (data.Error !== null) {
@ -362,8 +378,13 @@
return; return;
} }
//add checkbox to table if (systemId <= 0) {
addNewCheckbox($("#system-checkbox-table"), "SystemIds", data.Data.Id, data.Data.Abbreviation, 8); //add checkbox to table
addNewCheckbox($("#system-checkbox-table"), "SystemIds", data.Data.Id, data.Data.Abbreviation, 6);
} else {
//update checkbox's label with new abbreviation
$("#system-checkbox-table").find("input[value='" + data.Data.Id +"'] + label").text(data.Data.Abbreviation);
}
toggleFormAndLink("system")(); toggleFormAndLink("system")();
} }
}); });
@ -410,7 +431,7 @@
data: data, data: data,
traditional: true, traditional: true,
beforeSend: function() { $container.clearModelErrors(); }, beforeSend: function() { $container.clearModelErrors(); },
success: function(data, statux, $xhr) { success: function(data, status, $xhr) {
if (data.Error !== null) { if (data.Error !== null) {
$container.applyModelErrors(data.Error, data.Data); $container.applyModelErrors(data.Error, data.Data);
return; return;
@ -522,16 +543,16 @@
$("#edit-game-link").toggleClass("edit-link loading-link"); $("#edit-game-link").toggleClass("edit-link loading-link");
var gameId = $("#GameId").val(); var gameId = $("#GameId").val();
$.vgquotes.getGame(gameId, function(game) { $.vgquotes.getResourceById("game", gameId, function(game) {
if (game === null) { if (game === null) {
alert("Unable to fetch game " + id); alert("Unable to fetch game " + gameId);
return; return;
} }
$("#edit-game-link").toggleClass("edit-link loading-link"); $("#edit-game-link").toggleClass("edit-link loading-link");
toggleFormAndLink("game")(); toggleFormAndLink("game")();
$("#create-game-form > .edit-mode").val(1); $("#create-game-form > .edit-mode").val(1);
$("#create-game-form > legend").text("Edit Game"); $("#create-game-form > fieldset > legend").text("Edit Game");
//populate game form with game data //populate game form with game data
$("#GameName").val(game.Name); $("#GameName").val(game.Name);
@ -549,6 +570,32 @@
return false; return false;
}); });
$(".edit-system-link").click(function() {
var $link = $(this);
$link.toggleClass("edit-link loading-link");
var systemId = $link.siblings("input[name='SystemIds']").val();
$.vgquotes.getResourceById("system", systemId, function(system) {
if (system === null) {
alert("Unable to fetch system " + systemId);
return;
}
$link.toggleClass("edit-link loading-link");
toggleFormAndLink("system")();
$("#create-system-form > .edit-mode").val(systemId);
$("#create-system-form > fieldset > legend").text("Edit System");
//populate system form with systemdata
$("#SystemName").val(system.Name);
$("#SystemAbbreviation").val(system.Abbreviation);
$("#SystemReleaseDate").val($.vgquotes.parseAndFormatDate(system.ReleaseDate));
});
return false;
});
}; };
(function(){ (function(){