From 8b8d82f3d3a17719d0fc4e06d4f0a4cdcc24f8b7 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Mon, 3 Feb 2014 21:16:35 -0500 Subject: [PATCH] Add ability to fix errors during import --- Web/Attributes/MileageLogTypeValid.cs | 16 +++++++ Web/Controllers/ControllerBase.cs | 27 ++++++++++++ Web/Controllers/LogController.cs | 63 ++++++++++----------------- Web/DAL/LogImportTemplateWriter.cs | 3 +- Web/Scripts/Shared/ImportCreate.js | 2 +- Web/ViewModels/LogImportViewModel.cs | 15 ++++--- Web/Views/Log/LogImportPartial.cshtml | 9 ++++ Web/Web.csproj | 2 + 8 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 Web/Attributes/MileageLogTypeValid.cs create mode 100644 Web/Views/Log/LogImportPartial.cshtml diff --git a/Web/Attributes/MileageLogTypeValid.cs b/Web/Attributes/MileageLogTypeValid.cs new file mode 100644 index 0000000..1d79ead --- /dev/null +++ b/Web/Attributes/MileageLogTypeValid.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using MileageTraker.Web.Models; +using MileageTraker.Web.Utility; + +namespace MileageTraker.Web.Attributes +{ + public class MileageLogTypeValid : ValidationAttribute + { + public override bool IsValid(object value) + { + if (value == null) + return false; + return typeof (MileageLogType).IsValidValue(value.ToString()); + } + } +} \ No newline at end of file diff --git a/Web/Controllers/ControllerBase.cs b/Web/Controllers/ControllerBase.cs index 9f12841..0557333 100644 --- a/Web/Controllers/ControllerBase.cs +++ b/Web/Controllers/ControllerBase.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Web.Mvc; using MileageTraker.Web.Attributes; using MileageTraker.Web.DAL; @@ -33,6 +36,19 @@ namespace MileageTraker.Web.Controllers base.OnException(filterContext); } + public string RenderRazorViewToString(string viewName, object model) + { + ViewData.Model = model; + using (var sw = new StringWriter()) + { + var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); + var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); + viewResult.View.Render(viewContext, sw); + viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); + return sw.GetStringBuilder().ToString(); + } + } + protected string GetCookieValue(string key) { return @@ -81,5 +97,16 @@ namespace MileageTraker.Web.Controllers var selectList = new SelectList(DataService.GetPurposeTypes(), "PurposeTypeId", "Purpose"); return selectList; } + + protected static IEnumerable GetModelStateErrorList(IEnumerable> modelStateDictionary) + { + var errorList = + from kvp in modelStateDictionary + where kvp.Value.Errors.Any() + let errors = string.Join(", ", kvp.Value.Errors.Select(e => e.ErrorMessage)) + let msg = kvp.Key + ": " + errors + select msg; + return errorList; + } } } \ No newline at end of file diff --git a/Web/Controllers/LogController.cs b/Web/Controllers/LogController.cs index 5e5765f..6a5255f 100644 --- a/Web/Controllers/LogController.cs +++ b/Web/Controllers/LogController.cs @@ -7,10 +7,8 @@ using System.Web.Mvc; using System.Web.Routing; using MileageTraker.Web.Attributes; using MileageTraker.Web.DAL; -using MileageTraker.Web.Models; using MileageTraker.Web.Utility; using MileageTraker.Web.ViewModels; -using MileageTraker.Web.ViewModels.CreateLog; using MileageTraker.Web.ViewModels.Log; using MileageTraker.Web.ViewModels.Vehicle; @@ -174,13 +172,6 @@ namespace MileageTraker.Web.Controllers return View(viewModel); } - [HttpGet] - public ActionResult Create(LogImportViewModel viewModel) - { - // TODO: complete? - return View(); - } - public ActionResult Edit(int id) { var log = DataService.GetLog(id); @@ -250,7 +241,6 @@ namespace MileageTraker.Web.Controllers return PartialView(new LogPartialDetails(log)); } - // TODO: Fix? public ActionResult ImportUpload() { return View(); @@ -306,24 +296,8 @@ namespace MileageTraker.Web.Controllers { var buttonAttributes = new Dictionary { { "class", "btn btn-mini" }, { "target", "_blank" } }; - var correctItLink = HtmlHelper.GenerateLink( - ControllerContext.RequestContext, RouteTable.Routes, - "Correct", "Default", "Create", "Log", - null, - buttonAttributes); - try { - // verify log type - if (!typeof (MileageLogType).IsValidValue(viewModel.LogType)) - { - return Json(new - { - Status = ImportStatus.Failure.ToString(), - Message = "Invalid Log Type: \"" + viewModel.LogType + "\"", - //Action = correctItLink - }, JsonRequestBehavior.AllowGet); - } // verify purpose var purposeType = DataService.FindPurposeType(viewModel.Purpose); if (purposeType == null) @@ -332,7 +306,7 @@ namespace MileageTraker.Web.Controllers { Status = ImportStatus.Failure.ToString(), Message = "Invalid Purpose: " + viewModel.Purpose, - //Action = correctItLink + Action = GetFixLink(viewModel) }, JsonRequestBehavior.AllowGet); } @@ -346,7 +320,7 @@ namespace MileageTraker.Web.Controllers { Status = ImportStatus.Failure.ToString(), Message = string.Join(", ", errorList), - //Action = correctItLink + Action = GetFixLink(viewModel) }, JsonRequestBehavior.AllowGet); } @@ -380,7 +354,7 @@ namespace MileageTraker.Web.Controllers { Status = ImportStatus.Failure.ToString(), Message = "Vehile with supplied ID does not exist", - Action = correctItLink + Action = GetFixLink(viewModel) }, JsonRequestBehavior.AllowGet); } @@ -413,7 +387,7 @@ namespace MileageTraker.Web.Controllers { Status = ImportStatus.Failure.ToString(), Message = fullErrorMessage, - Action = correctItLink + Action = GetFixLink(viewModel) }, JsonRequestBehavior.AllowGet); } @@ -424,20 +398,31 @@ namespace MileageTraker.Web.Controllers { Status = ImportStatus.Failure.ToString(), Message = message, - Action = correctItLink + Action = GetFixLink(viewModel) }, JsonRequestBehavior.AllowGet); } } - private static IEnumerable GetModelStateErrorList(ModelStateDictionary modelStateDictionary) + private string GetFixLink(LogImportViewModel viewModel) { - var errorList = - from kvp in modelStateDictionary - where kvp.Value.Errors.Any() - let errors = string.Join(", ", kvp.Value.Errors.Select(e => e.ErrorMessage)) - let msg = kvp.Key + ": " + errors - select msg; - return errorList; + try + { + viewModel.GetLogViewModel(); + } + catch + { + return string.Empty; + } + + return RenderRazorViewToString("LogImportPartial", viewModel); + } + + [HttpPost] + public ActionResult ImportFix(LogImportViewModel viewModel) + { + var logViewModel = viewModel.GetLogViewModel(); + logViewModel.Purpose.Available = GetPurposeTypesSelectList(); + return View("Create", logViewModel); } public ActionResult ImportTemplate() diff --git a/Web/DAL/LogImportTemplateWriter.cs b/Web/DAL/LogImportTemplateWriter.cs index f8ae73c..d1ac06c 100644 --- a/Web/DAL/LogImportTemplateWriter.cs +++ b/Web/DAL/LogImportTemplateWriter.cs @@ -4,7 +4,6 @@ using ExcelLibrary.SpreadSheet; using MileageTraker.Web.Models; using MileageTraker.Web.Utility; using MileageTraker.Web.ViewModels; -using MileageTraker.Web.ViewModels.CreateLog; namespace MileageTraker.Web.DAL { @@ -16,7 +15,7 @@ namespace MileageTraker.Web.DAL new LogImportViewModel { CityName = "City Name", - Date = "10-20-2010", + Date = "10/20/2010", EndOdometer = "10010", GasPurchased = "3.141", LogType = "Commuting", diff --git a/Web/Scripts/Shared/ImportCreate.js b/Web/Scripts/Shared/ImportCreate.js index 1f5fef5..efb7502 100644 --- a/Web/Scripts/Shared/ImportCreate.js +++ b/Web/Scripts/Shared/ImportCreate.js @@ -26,7 +26,7 @@ else if (result.Status == "Failure") { $('.import-status', $row).html('Failure().ConvertUsing(Convert.ToDouble); Mapper.CreateMap().ConvertUsing(new DateTimeTypeConverter()); Mapper.CreateMap() + .ForMember(u => u.LogType, opt => opt.Ignore()) .ForMember(u => u.Purpose, opt => opt.Ignore()); Mapper.CreateMap() .ForMember(u => u.Purpose, opt => opt.Ignore()) @@ -73,13 +75,12 @@ namespace MileageTraker.Web.ViewModels { } - //public LogViewModel GetLogViewModel() - //{ - // var log = new LogViewModel(); - // Mapper.Map(this, log); - // return log; - //} - + public LogViewModel GetLogViewModel() + { + var log = new LogViewModel(); + Mapper.Map(this, log); + return log; + } public Models.Log GetLog() { diff --git a/Web/Views/Log/LogImportPartial.cshtml b/Web/Views/Log/LogImportPartial.cshtml new file mode 100644 index 0000000..0e37cee --- /dev/null +++ b/Web/Views/Log/LogImportPartial.cshtml @@ -0,0 +1,9 @@ +@model MileageTraker.Web.ViewModels.LogImportViewModel +@{ + Layout = null; +} +@using (Html.BeginForm("ImportFix", "Log", FormMethod.Post, new{target="_blank"})) +{ + @Html.EditorForModel() + +} \ No newline at end of file diff --git a/Web/Web.csproj b/Web/Web.csproj index 0741672..1604866 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -112,6 +112,7 @@ + @@ -231,6 +232,7 @@ +