From 9c2149d52cdb58c305ff1a24df6164d995f57249 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Tue, 21 Jan 2014 11:30:17 -0500 Subject: [PATCH] Add cancel button for edit log. Disable edit button for past month Format gas correctly --- .../DenyPreviousMonthDateAttribute.cs | 6 +++-- Web/Controllers/CreateLogController.cs | 11 +++++++- Web/Models/DomainRules.cs | 27 +++++++++++++++++++ Web/ViewModels/CreateLog/EditPastViewModel.cs | 9 ++++++- Web/Views/CreateLog/EditPast.cshtml | 3 ++- Web/Views/CreateLog/RecentLogs.cshtml | 16 ++++++++--- Web/Web.csproj | 1 + 7 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 Web/Models/DomainRules.cs diff --git a/Web/Attributes/DenyPreviousMonthDateAttribute.cs b/Web/Attributes/DenyPreviousMonthDateAttribute.cs index ada829e..56b4d1e 100644 --- a/Web/Attributes/DenyPreviousMonthDateAttribute.cs +++ b/Web/Attributes/DenyPreviousMonthDateAttribute.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using MileageTraker.Web.Models; namespace MileageTraker.Web.Attributes { @@ -13,8 +14,9 @@ namespace MileageTraker.Web.Attributes private static DateTime GetCutoff() { - var today = DateTime.Today; - return today.AddMonths(-1).AddDays(-today.Day + 1); // last two months + return DomainRules.GetLogEntryCutoff(); + //var today = DateTime.Today; + //return today.AddMonths(-1).AddDays(-today.Day + 1); // last two months //if (today.Day > 10) // return today.AddDays(-today.Day); // beginning of this month //return today.AddMonths(-1).AddDays(-today.Day); // beginning of previous month diff --git a/Web/Controllers/CreateLogController.cs b/Web/Controllers/CreateLogController.cs index da27b8d..01191a5 100644 --- a/Web/Controllers/CreateLogController.cs +++ b/Web/Controllers/CreateLogController.cs @@ -62,7 +62,16 @@ namespace MileageTraker.Web.Controllers DataService.UpdateLog(log); - TempData["StatusMessage"] = "Log updated"; + TempData["StatusMessage-Type"] = "alert-success"; + TempData["StatusMessage"] = + @"You've successfully updated an entry + traveling to " + viewModel.CityName + @" + for " + log.Purpose.Purpose + @" + on " + viewModel.Date.ToShortDateString() + @" + in Vehicle Id " + viewModel.VehicleId + @" + ending in " + viewModel.EndOdometer + @" + miles on the odometer."; + return RedirectToAction("Index"); } diff --git a/Web/Models/DomainRules.cs b/Web/Models/DomainRules.cs new file mode 100644 index 0000000..64207b1 --- /dev/null +++ b/Web/Models/DomainRules.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace MileageTraker.Web.Models +{ + public static class DomainRules + { + /// + /// Returns the most recent date for which a log can be submitted from today + /// + public static DateTime GetLogEntryCutoff() + { + return GetLogEntryCutoff(DateTime.Today); + } + + /// + /// Returns the most recent date for which a log can be submitted given the + /// reference date + /// + public static DateTime GetLogEntryCutoff(DateTime referenceDate) + { + return referenceDate.AddMonths(-1).AddDays(-referenceDate.Day + 1); // last two months + } + } +} \ No newline at end of file diff --git a/Web/ViewModels/CreateLog/EditPastViewModel.cs b/Web/ViewModels/CreateLog/EditPastViewModel.cs index 8c93248..ef3702f 100644 --- a/Web/ViewModels/CreateLog/EditPastViewModel.cs +++ b/Web/ViewModels/CreateLog/EditPastViewModel.cs @@ -68,6 +68,12 @@ namespace MileageTraker.Web.ViewModels.CreateLog static EditPastViewModel() { + Func gasFormatter = l => + { + if (Math.Abs(l.GasPurchased - 0.0) < double.Epsilon) + return string.Empty; + return l.GasPurchased; + }; Mapper.CreateMap().ConvertUsing(Convert.ToInt32); Mapper.CreateMap().ConvertUsing(Convert.ToDouble); Mapper.CreateMap().ConvertUsing(new DateTimeTypeConverter()); @@ -75,7 +81,8 @@ namespace MileageTraker.Web.ViewModels.CreateLog .ForMember(u => u.Purpose, opt => opt.Ignore()); Mapper.CreateMap() .ForMember(vm => vm.Date, opt => opt.MapFrom(m => m.Date.ToString("d"))) - .ForMember(u => u.Purpose, opt => opt.Ignore()); + .ForMember(u => u.Purpose, opt => opt.Ignore()) + .ForMember(vm => vm.GasPurchased, opt => opt.ResolveUsing(gasFormatter)); } public EditPastViewModel() diff --git a/Web/Views/CreateLog/EditPast.cshtml b/Web/Views/CreateLog/EditPast.cshtml index 4fc48ed..fc09bbc 100644 --- a/Web/Views/CreateLog/EditPast.cshtml +++ b/Web/Views/CreateLog/EditPast.cshtml @@ -15,7 +15,8 @@ @Html.Partial("_ValidationSummary") @Html.EditorForModel()
- + + @Html.ActionLink("Cancel", "Index", new {}, new { @class = "btn" })
} diff --git a/Web/Views/CreateLog/RecentLogs.cshtml b/Web/Views/CreateLog/RecentLogs.cshtml index b64ab7d..cf98c2e 100644 --- a/Web/Views/CreateLog/RecentLogs.cshtml +++ b/Web/Views/CreateLog/RecentLogs.cshtml @@ -36,10 +36,17 @@ @Html.Encode(log.EndOdometer) - @Html.Encode(log.LogType.Enum.GetDisplayName()) + @Html.Encode(log.LogType.Enum.GetDisplayShortName()) - @Html.ActionLink("Edit", "EditPast", new { id = log.LogId }, new { @class = "btn btn-mini" }) + @if (log.Date >= DomainRules.GetLogEntryCutoff()) + { + @Html.ActionLink("Edit", "EditPast", new {id = log.LogId}, new {@class = "btn btn-mini"}) + } + else + { + @Html.ActionLink("Edit", "EditPast", new {id = log.LogId}, new {@class = "btn btn-mini disabled", title="Previous Month" }) + } } @@ -48,4 +55,7 @@ else {

Mileage history not found for @ViewData["name"]

-} \ No newline at end of file +} + \ No newline at end of file diff --git a/Web/Web.csproj b/Web/Web.csproj index 1c4fc02..fee8cae 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -169,6 +169,7 @@ 201303071926246_UpdatePurposeRequired.cs +