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.IO;
using System.Linq;
using System.Net;
using System.Linq;
using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Util;
using Portoa.Web;
using Portoa.Web.Controllers;
using Portoa.Web.Results;
using VideoGameQuotes.Api;
using VideoGameQuotes.Web.Models;
using VideoGameQuotes.Web.Security;

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Portoa.Util;
using Portoa.Web.Controllers;
using VideoGameQuotes.Api;
using VideoGameQuotes.Web.Models;
@ -15,8 +16,28 @@ namespace VideoGameQuotes.Web.Controllers {
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]
public ActionResult Create(CreateSystemModel model) {
public ActionResult Create(EditSystemModel model) {
if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
}

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using JetBrains.Annotations;
using Portoa.Validation.DataAnnotations;
using VideoGameQuotes.Api;
@ -80,21 +81,21 @@ namespace VideoGameQuotes.Web.Models {
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(
"publisher-checkbox-table",
cellsPerRow,
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(
"system-checkbox-table",
cellsPerRow,
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",
cellsPerRow,
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(
"<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,
value,
id,
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;
namespace VideoGameQuotes.Web.Models {
public class CreateSystemModel {
public class EditSystemModel {
public int SystemId { get; set; }
[Required(ErrorMessage = "Systems must have a name, fatty")]
public string SystemName { get; set; }
[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 Portoa.Persistence;
using VideoGameQuotes.Api;
@ -7,6 +8,7 @@ namespace VideoGameQuotes.Web.Services {
public interface ISystemService {
IEnumerable<GamingSystem> FindByNameOrAbbreviation(string name, string abbreviation);
GamingSystem Save(GamingSystem system);
GamingSystem FindById(int id);
}
public class SystemService : ISystemService {
@ -27,5 +29,10 @@ namespace VideoGameQuotes.Web.Services {
public GamingSystem Save(GamingSystem 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\ChangePasswordModel.cs" />
<Compile Include="Models\CreateAdminModel.cs" />
<Compile Include="Models\CreateSystemModel.cs" />
<Compile Include="Models\EditSystemModel.cs" />
<Compile Include="Models\InvalidUsernameModel.cs" />
<Compile Include="Models\MainMenuModel.cs" />
<Compile Include="Models\QualifiedBrowseModel.cs" />
@ -141,6 +141,7 @@
<Content Include="media\css\quote.css" />
<Content Include="media\css\reset.css" />
<Content Include="media\images\add.png" />
<Content Include="media\images\delete.png" />
<Content Include="media\images\favicon.png" />
<Content Include="media\images\loading.gif" />
<Content Include="media\images\search.png" />

View File

@ -19,6 +19,7 @@
<div id="create-game-form" class="subform">
<input type="hidden" value="0" class="edit-mode" />
<input type="hidden" value="0" id="SystemId" />
<fieldset>
<legend>Create new game</legend>
@ -46,7 +47,7 @@
<span id="SystemIds" class="label">Systems</span>
<br />
<%= 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 id="create-system-form" class="subform">
@ -83,7 +84,7 @@
<span class="label">Publishers</span>
<br />
<%= 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 id="create-publisher-form" class="subform">
@ -127,7 +128,7 @@
</p>
<%= 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>
<% if (Model.QuoteId > 0) { %>

View File

@ -152,8 +152,9 @@ ul.menu {
width: 500px;
height: 120px;
}
.create-new-link, .edit-link, .loading-link {
.create-new-link, .edit-link, .loading-link, .delete-link {
padding-left: 16px;
margin-left: 2px;
}
.create-new-link {
background: transparent url(/media/images/add.png) left center no-repeat;
@ -164,6 +165,9 @@ ul.menu {
.loading-link {
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 {
margin-bottom: 2px;

Binary file not shown.

After

Width:  |  Height:  |  Size: 715 B

View File

@ -53,11 +53,17 @@
};
var cache = {
games: { }
game: { },
system: { },
category: { },
publisher: { }
};
return {
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) {
console.dir(xhr);
alert("An error occurred (" + xhr.statusCode + ")");
@ -70,21 +76,21 @@
},
loadingGif: "/media/images/loading.gif",
editIcon: "/media/images/pencil.png",
getGame: function(id, callback) {
getResourceById: function(type, id, callback) {
id = id.toString();
// if (typeof(cache.games[id]) !== "undefined") {
// callback.call(null, cache.games[id]);
// if (typeof(cache[type][id]) !== "undefined") {
// callback.call(null, cache[type][id]);
// 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) {
callback.call(null, null);
return;
}
cache.games[id] = data.Data.games[0];
callback.call(null, cache.games[id]);
cache[type][id] = data.Data[type + "s"][0];
callback.call(null, cache[type][id]);
});
}
};
@ -319,7 +325,7 @@
.find("input[type='text']").val("").end()
.find("input[type='checkbox']").removeAttr("checked").end()
.find("> .edit-mode").val(0).end()
.find("> legend").text("Create new " + type).end()
.find("> fieldset > legend").text("Create new " + type).end()
.toggle();
$("#" + type + "-select").toggle();
@ -352,9 +358,19 @@
$("#create-system-submit").click(function() {
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",
data: { SystemName: $("#SystemName").val(), SystemAbbreviation: $("#SystemAbbreviation").val(), SystemReleaseDate: $("#SystemReleaseDate").val() },
data: data,
beforeSend: function() { $container.clearModelErrors(); },
success: function(data, statux, $xhr) {
if (data.Error !== null) {
@ -362,8 +378,13 @@
return;
}
//add checkbox to table
addNewCheckbox($("#system-checkbox-table"), "SystemIds", data.Data.Id, data.Data.Abbreviation, 8);
if (systemId <= 0) {
//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")();
}
});
@ -410,7 +431,7 @@
data: data,
traditional: true,
beforeSend: function() { $container.clearModelErrors(); },
success: function(data, statux, $xhr) {
success: function(data, status, $xhr) {
if (data.Error !== null) {
$container.applyModelErrors(data.Error, data.Data);
return;
@ -522,16 +543,16 @@
$("#edit-game-link").toggleClass("edit-link loading-link");
var gameId = $("#GameId").val();
$.vgquotes.getGame(gameId, function(game) {
$.vgquotes.getResourceById("game", gameId, function(game) {
if (game === null) {
alert("Unable to fetch game " + id);
alert("Unable to fetch game " + gameId);
return;
}
$("#edit-game-link").toggleClass("edit-link loading-link");
toggleFormAndLink("game")();
$("#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
$("#GameName").val(game.Name);
@ -549,6 +570,32 @@
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(){