From dfa5277e8c7fddfa2389acf5dbf5ebee8680e49a Mon Sep 17 00:00:00 2001 From: tmont Date: Mon, 14 Feb 2011 02:50:33 +0000 Subject: [PATCH] added javascript madness to add new category --- .../Controllers/QuoteController.cs | 36 +++++++++- Src/VideoGameQuotes.Web/Global.asax.cs | 1 + .../Models/QuoteSubmitModel.cs | 7 +- .../Services/QuoteService.cs | 6 ++ .../Views/Quote/Submit.aspx | 68 ++++++++++++++++++- Src/VideoGameQuotes.Web/media/css/global.css | 5 +- 6 files changed, 117 insertions(+), 6 deletions(-) diff --git a/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs b/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs index 5ca4a22..eb239e1 100644 --- a/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs +++ b/Src/VideoGameQuotes.Web/Controllers/QuoteController.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using JetBrains.Annotations; +using Portoa.Web.Controllers; using VideoGameQuotes.Api; using VideoGameQuotes.Web.Models; using VideoGameQuotes.Web.Security; @@ -21,7 +23,6 @@ namespace VideoGameQuotes.Web.Controllers { public ActionResult Submit() { var model = new QuoteSubmitModel(); ResetModel(model); - return View(model); } @@ -29,6 +30,7 @@ namespace VideoGameQuotes.Web.Controllers { public ActionResult Submit(QuoteSubmitModel model) { if (!ModelState.IsValid) { ResetModel(model); + return View(model); } @@ -39,6 +41,26 @@ namespace VideoGameQuotes.Web.Controllers { Text = model.QuoteText }; + //categories + if ((model.CategoryIds != null && model.CategoryIds.Count > 0)) { + //modifying systems, so remove errthing + quote.ClearCategories(); + + if (model.CategoryIds != null && model.CategoryIds.Count > 0) { + foreach (var categoryId in model.CategoryIds) { + quote.AddCategory(quoteService.GetCategory(categoryId)); + } + } + + //if (!string.IsNullOrEmpty(model.SystemName)) { + // game.AddSystem(new GamingSystem { + // Name = model.SystemName, + // Abbreviation = model.SystemAbbreviation, + // ReleaseDate = model.SystemReleaseDate + // }); + //} + } + if (quote.Game == null) { ResetModel(model); return View(model); @@ -129,5 +151,17 @@ namespace VideoGameQuotes.Web.Controllers { public ActionResult Quote(int id) { return View(quoteService.GetQuote(id)); } + + [HttpPost] + public JsonResult CreateCategory(Category category) { + try { + category = quoteService.SaveCategory(category); + var data = new Dictionary { { "categoryId", category.Id.ToString() }, { "categoryName", category.Name } }; + return Json(this.CreateJsonResponse(null, data)); + } catch (Exception e) { + return Json(this.CreateJsonErrorResponse(e)); + } + } + } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Web/Global.asax.cs b/Src/VideoGameQuotes.Web/Global.asax.cs index 290d963..c9c261a 100644 --- a/Src/VideoGameQuotes.Web/Global.asax.cs +++ b/Src/VideoGameQuotes.Web/Global.asax.cs @@ -40,6 +40,7 @@ namespace VideoGameQuotes.Web { routes.MapRoute("about", "about", new { controller = "Home", action = "About" }); routes.MapRoute("contact", "contact", new { controller = "Home", action = "Contact" }); routes.MapRoute("submit", "submit", new { controller = "Quote", action = "Submit" }); + routes.MapRoute("create-category", "category/create", new { controller = "Quote", action = "CreateCategory" }); routes.MapRoute("individual-quote", "q/{id}/{*text}", new { controller = "Quote", action = "Quote" }, new { id = @"\d+" }); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } diff --git a/Src/VideoGameQuotes.Web/Models/QuoteSubmitModel.cs b/Src/VideoGameQuotes.Web/Models/QuoteSubmitModel.cs index 8cd673c..8259f1b 100644 --- a/Src/VideoGameQuotes.Web/Models/QuoteSubmitModel.cs +++ b/Src/VideoGameQuotes.Web/Models/QuoteSubmitModel.cs @@ -54,8 +54,9 @@ namespace VideoGameQuotes.Web.Models { .Concat(AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name })); } - private static string MakeTable(int cellsPerRow, IEnumerable items, Func cellDataCallback) { + private static string MakeTable(string tableId, int cellsPerRow, IEnumerable items, Func cellDataCallback) { var table = new TagBuilder("table"); + table.MergeAttribute("id", tableId); var count = 0; var row = new TagBuilder("tr"); var cell = new TagBuilder("td"); @@ -84,6 +85,7 @@ namespace VideoGameQuotes.Web.Models { public string MakePublisherTable(HtmlHelper html, int cellsPerRow = 8) { return MakeTable( + "publisher-checkbox-table", cellsPerRow, AllPublishers, publisher => CreateCheckbox(html, "PublisherIds", publisher.Id, "publisher_" + publisher.Id, publisher.Name) @@ -92,6 +94,7 @@ namespace VideoGameQuotes.Web.Models { public string MakeSystemTable(HtmlHelper html, int cellsPerRow = 8) { return MakeTable( + "system-checkbox-table", cellsPerRow, AllSystems, system => CreateCheckbox(html, "SystemIds", system.Id, "system_" + system.Id, system.Abbreviation) @@ -100,6 +103,7 @@ namespace VideoGameQuotes.Web.Models { public string MakeCategoryTable(HtmlHelper html, int cellsPerRow = 8) { return MakeTable( + "category-checkbox-table", cellsPerRow, AllCategories, category => CreateCheckbox(html, "CategoryIds", category.Id, "category_" + category.Id, category.Name) @@ -108,6 +112,7 @@ namespace VideoGameQuotes.Web.Models { public string MakeRegionTable(HtmlHelper html, int cellsPerRow = 8) { return MakeTable( + "region-checkbox-table", cellsPerRow, (IEnumerable)Enum.GetValues(typeof(Region)), region => CreateCheckbox(html, "GameRegions", region, "region_" + (int)region, region.ToString()) diff --git a/Src/VideoGameQuotes.Web/Services/QuoteService.cs b/Src/VideoGameQuotes.Web/Services/QuoteService.cs index d681699..5c120c2 100644 --- a/Src/VideoGameQuotes.Web/Services/QuoteService.cs +++ b/Src/VideoGameQuotes.Web/Services/QuoteService.cs @@ -15,6 +15,7 @@ namespace VideoGameQuotes.Web.Services { Publisher GetPublisher(int id); GamingSystem GetSystem(int systemId); Category GetCategory(int categoryId); + Category SaveCategory(Category category); } public class QuoteService : IQuoteService { @@ -86,5 +87,10 @@ namespace VideoGameQuotes.Web.Services { public Category GetCategory(int categoryId) { return categoryRepository.FindById(categoryId); } + + [UnitOfWork] + public Category SaveCategory(Category category) { + return categoryRepository.Save(category); + } } } \ No newline at end of file diff --git a/Src/VideoGameQuotes.Web/Views/Quote/Submit.aspx b/Src/VideoGameQuotes.Web/Views/Quote/Submit.aspx index f12d5a3..17b50d1 100644 --- a/Src/VideoGameQuotes.Web/Views/Quote/Submit.aspx +++ b/Src/VideoGameQuotes.Web/Views/Quote/Submit.aspx @@ -124,7 +124,6 @@ diff --git a/Src/VideoGameQuotes.Web/media/css/global.css b/Src/VideoGameQuotes.Web/media/css/global.css index dcaa726..b671dca 100644 --- a/Src/VideoGameQuotes.Web/media/css/global.css +++ b/Src/VideoGameQuotes.Web/media/css/global.css @@ -132,7 +132,7 @@ legend { border-bottom: 2px solid #000000; } -#create-game-form, #create-system-form, #create-publisher-form { +#create-game-form, #create-system-form, #create-publisher-form, #create-category-form { display: none; } #create-quote-form input[type="text"] { @@ -145,6 +145,9 @@ legend { #create-quote-form td { padding: 5px; } +#new-category-name { + width: 100px !important; +} #footer { text-align: center;