From 11c3b2c56b7dd1d35aa5f7f776942f4ab3b0fd01 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Thu, 18 Jun 2015 11:42:22 -0400 Subject: [PATCH] Validate new logs are not being entered after vehicle is inactive. --- Web/Models/Vehicle.cs | 3 +- Web/Scripts/Shared/Site.js | 2 +- .../CreateLog/CreateLogViewModel.cs | 28 +++++++++++------ Web/ViewModels/Log/LogViewModel.cs | 31 +++++++++++-------- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/Web/Models/Vehicle.cs b/Web/Models/Vehicle.cs index ff04946..bd6a39e 100644 --- a/Web/Models/Vehicle.cs +++ b/Web/Models/Vehicle.cs @@ -59,7 +59,8 @@ namespace MileageTraker.Web.Models public string PurDate { get; set; } [DataType(DataType.DateTime)] - [DisplayFormat(NullDisplayText = "Currently Active", DataFormatString = @"{0:MM/dd/yyyy}")] + [DisplayFormat(NullDisplayText = "Currently Active", DataFormatString = @"{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] + [InputSize("small")] [FormatHint("mm/dd/yyyy")] public DateTime? InactiveDate { get; set; } diff --git a/Web/Scripts/Shared/Site.js b/Web/Scripts/Shared/Site.js index 0d8d1fd..eb3186f 100644 --- a/Web/Scripts/Shared/Site.js +++ b/Web/Scripts/Shared/Site.js @@ -1,7 +1,7 @@ $(function () { $('.enable-javascript').show(); - $("input#Date").datepicker({ maxDate: '+0d' }); + $("input#Date, input#InactiveDate").datepicker({ maxDate: '+0d' }); $("input#CityName").autocomplete({ source: "/City/Autocomplete", diff --git a/Web/ViewModels/CreateLog/CreateLogViewModel.cs b/Web/ViewModels/CreateLog/CreateLogViewModel.cs index a10b004..5259960 100644 --- a/Web/ViewModels/CreateLog/CreateLogViewModel.cs +++ b/Web/ViewModels/CreateLog/CreateLogViewModel.cs @@ -95,21 +95,29 @@ namespace MileageTraker.Web.ViewModels.CreateLog if (LogType.Value == 0) yield return new ValidationResult("Required", new[] { "LogType" }); - - ValidationResult result = null; - try + + using (var dataService = new DataService()) { - using (var dataService = new DataService()) + ValidationResult odometerResult = null; + try { dataService.ValidateOdometerChronology(VehicleId, int.Parse(EndOdometer), Date); } + catch (Exception ex) + { + odometerResult = new ValidationResult(ex.Message, new [] {"EndOdometer"}); + } + if (odometerResult != null) + yield return odometerResult; + + var inactiveDate = dataService.GetVehicle(VehicleId).InactiveDate; + if (inactiveDate.HasValue && Date.Date > inactiveDate) + { + yield return new ValidationResult( + string.Format("Vehicle was set inactive on {0:MM/dd/yyyy}", inactiveDate.Value), + new[] { "Date" }); + } } - catch (Exception ex) - { - result = new ValidationResult(ex.Message, new [] {"EndOdometer"}); - } - if (result != null) - yield return result; } public override string ToString() diff --git a/Web/ViewModels/Log/LogViewModel.cs b/Web/ViewModels/Log/LogViewModel.cs index 187d5b6..eefdf32 100644 --- a/Web/ViewModels/Log/LogViewModel.cs +++ b/Web/ViewModels/Log/LogViewModel.cs @@ -124,27 +124,32 @@ namespace MileageTraker.Web.ViewModels.Log if (LogType.Value == 0) yield return new ValidationResult("Required", new[] { "LogType" }); - ValidationResult chronologyResult = null; - try + using (var dataService = new DataService()) { - using (var dataService = new DataService()) + ValidationResult chronologyResult = null; + try { dataService.ValidateOdometerChronology(VehicleId, int.Parse(EndOdometer), Date); } - } - catch (Exception ex) - { - chronologyResult = new ValidationResult(ex.Message, new [] {"EndOdometer"}); - } - if (chronologyResult != null) - yield return chronologyResult; + catch (Exception ex) + { + chronologyResult = new ValidationResult(ex.Message, new[] {"EndOdometer"}); + } + if (chronologyResult != null) + yield return chronologyResult; - using (var dataService = new DataService()) - { var user = dataService.FindUserByFullName(UserFullName); if (user == null) { - yield return new ValidationResult("User with this name doesn't exist", new[] { "UserFullName" }); + yield return new ValidationResult("User with this name doesn't exist", new[] {"UserFullName"}); + } + + var inactiveDate = dataService.GetVehicle(VehicleId).InactiveDate; + if (inactiveDate.HasValue && Date.Date > inactiveDate) + { + yield return new ValidationResult( + string.Format("Vehicle was set inactive on {0:MM/dd/yyyy}", inactiveDate.Value), + new[] { "Date" }); } } }