Basic implementation - largely untested
This commit is contained in:
@@ -5,6 +5,7 @@ using MileageTraker.Web.Attributes;
|
||||
using MileageTraker.Web.Models;
|
||||
using MileageTraker.Web.ViewModels;
|
||||
using MileageTraker.Web.ViewModels.CreateLog;
|
||||
using MileageTraker.Web.ViewModels.Log;
|
||||
|
||||
namespace MileageTraker.Web.Controllers
|
||||
{
|
||||
@@ -35,6 +36,40 @@ namespace MileageTraker.Web.Controllers
|
||||
return View(model);
|
||||
}
|
||||
|
||||
public ActionResult EditPast(int id)
|
||||
{
|
||||
var log = DataService.GetLog(id);
|
||||
var viewModel = new EditPastViewModel(log)
|
||||
{
|
||||
Purpose = { Available = GetPurposeTypesSelectList() }
|
||||
};
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ActionLog]
|
||||
public ActionResult EditPast(EditPastViewModel viewModel)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var log = DataService.GetLog(viewModel.LogId);
|
||||
viewModel.SetProperties(log);
|
||||
|
||||
if (viewModel.Purpose != null)
|
||||
log.Purpose =
|
||||
DataService.GetPurposeTypes()
|
||||
.First(pt => pt.PurposeTypeId == viewModel.Purpose.Selected);
|
||||
|
||||
DataService.UpdateLog(log);
|
||||
|
||||
TempData["StatusMessage"] = "Log updated";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
viewModel.Purpose.Available = GetPurposeTypesSelectList();
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpParamAction]
|
||||
[HttpPost]
|
||||
public ViewResult Edit(CreateLogViewModel model)
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Web.Mvc;
|
||||
using AutoMapper;
|
||||
using MileageTraker.Web.Attributes;
|
||||
using MileageTraker.Web.DAL;
|
||||
using MileageTraker.Web.Models;
|
||||
using MileageTraker.Web.Utility;
|
||||
|
||||
namespace MileageTraker.Web.ViewModels.CreateLog
|
||||
{
|
||||
public class EditPastViewModel : IValidatableObject
|
||||
{
|
||||
[HiddenInput(DisplayValue = false)]
|
||||
public int LogId { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Required")]
|
||||
[Remote("Exists", "Vehicle", ErrorMessage = "ID not found")]
|
||||
[StringLength(6, MinimumLength = 4, ErrorMessage = "Must be a 4 digit number")]
|
||||
[Display(Name = "Vehicle ID")]
|
||||
[InputSize("mini")]
|
||||
[RegularExpression(@"\d+", ErrorMessage = "Must be all numbers")]
|
||||
public string VehicleId { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Required")]
|
||||
[Range(1, 500000, ErrorMessage = "Between 1 and 500k")]
|
||||
[Display(Name = "End Odometer")]
|
||||
[Units("Miles")]
|
||||
[InputSize("small")]
|
||||
[RegularExpression(@"\d+", ErrorMessage = "Must be all numbers")]
|
||||
public string EndOdometer { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Required")]
|
||||
[Display(Name = "Type")]
|
||||
[NoEditLabel]
|
||||
public MileageLogTypeWrapper LogType { get; set; }
|
||||
[Required(ErrorMessage = "Required")]
|
||||
[Display(Name = "Destination City")]
|
||||
[InputSize("medium")]
|
||||
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
|
||||
public string CityName { get; set; }
|
||||
|
||||
[Display(Name = "Purpose")]
|
||||
[Required]
|
||||
public SelectListViewModel Purpose { get; set; }
|
||||
|
||||
[InputSize("large")]
|
||||
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
|
||||
public string Notes { get; set; }
|
||||
|
||||
[RegularExpression(@"\d+\.\d{3}", ErrorMessage = "Enter all 3 decimal places")]
|
||||
[Display(Name = "Gas Purchased")]
|
||||
[DisplayFormat(DataFormatString = "{0:0.000}", ApplyFormatInEditMode = true)]
|
||||
[Units("Gallons")]
|
||||
[FormatHint("n.nnn")]
|
||||
[InputSize("mini")]
|
||||
public string GasPurchased { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Required")]
|
||||
[DataType(DataType.Date)]
|
||||
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
|
||||
[DenyFutureDate(ErrorMessage = "Future date")]
|
||||
[DenyPreviousMonthDate(ErrorMessage = "Previous Month")]
|
||||
[FormatHint("mm/dd/yyyy")]
|
||||
[InputSize("small")]
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
static EditPastViewModel()
|
||||
{
|
||||
Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
|
||||
Mapper.CreateMap<string, double>().ConvertUsing(Convert.ToDouble);
|
||||
Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
|
||||
Mapper.CreateMap<EditPastViewModel, Models.Log>()
|
||||
.ForMember(u => u.Purpose, opt => opt.Ignore());
|
||||
Mapper.CreateMap<Models.Log, EditPastViewModel>()
|
||||
.ForMember(vm => vm.Date, opt => opt.MapFrom(m => m.Date.ToString("d")))
|
||||
.ForMember(u => u.Purpose, opt => opt.Ignore());
|
||||
}
|
||||
|
||||
public EditPastViewModel()
|
||||
{
|
||||
// view will crash if this isn't instantiated
|
||||
LogType = new MileageLogTypeWrapper();
|
||||
Purpose = new SelectListViewModel();
|
||||
}
|
||||
|
||||
public EditPastViewModel(Models.Log log) : this()
|
||||
{
|
||||
Mapper.Map(log, this);
|
||||
if (log.Purpose != null)
|
||||
Purpose.Selected = log.Purpose.PurposeTypeId;
|
||||
}
|
||||
|
||||
public Models.Log GetLog()
|
||||
{
|
||||
var log = new Models.Log();
|
||||
Mapper.Map(this, log);
|
||||
return log;
|
||||
}
|
||||
|
||||
public void SetProperties(Models.Log log)
|
||||
{
|
||||
Mapper.DynamicMap(this, log);
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if (LogType == MileageLogType.GasPurchase
|
||||
&& (string.IsNullOrEmpty(GasPurchased) || Math.Abs(double.Parse(GasPurchased) - 0) < double.Epsilon))
|
||||
yield return new ValidationResult("Enter amount of gas purchased", new [] {"GasPurchased"});
|
||||
|
||||
if (LogType.Value == 0)
|
||||
yield return new ValidationResult("Required", new[] { "LogType" });
|
||||
|
||||
ValidationResult result = null;
|
||||
try
|
||||
{
|
||||
using (var dataService = new DataService())
|
||||
{
|
||||
dataService.ValidateOdometerChronology(VehicleId, int.Parse(EndOdometer), Date);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = new ValidationResult(ex.Message, new [] {"EndOdometer"});
|
||||
}
|
||||
if (result != null)
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
@model MileageTraker.Web.ViewModels.CreateLog.EditPastViewModel
|
||||
@{
|
||||
|
||||
ViewBag.Title = "Edit Mileage Log";
|
||||
}
|
||||
|
||||
@Html.Partial("_StatusMessage")
|
||||
|
||||
<h2 class="center-content">@ViewBag.Title</h2>
|
||||
|
||||
@using (Html.BeginForm("EditPast", "CreateLog", FormMethod.Post, new { @class = "form-horizontal well center-content" }))
|
||||
{
|
||||
<fieldset>
|
||||
<legend></legend>
|
||||
@Html.Partial("_ValidationSummary")
|
||||
@Html.EditorForModel()
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Submit" class="btn btn-primary" />
|
||||
</div>
|
||||
</fieldset>
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
<th>
|
||||
Type
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
@foreach (var log in Model)
|
||||
@@ -37,6 +38,9 @@
|
||||
<td>
|
||||
@Html.Encode(log.LogType.Enum.GetDisplayName())
|
||||
</td>
|
||||
<td>
|
||||
@Html.ActionLink("Edit", "EditPast", new { id = log.LogId }, new { @class = "btn btn-mini" })
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</table>
|
||||
|
||||
@@ -179,6 +179,7 @@
|
||||
<Compile Include="ViewModels\Account\NewPasswordViewModel.cs" />
|
||||
<Compile Include="ViewModels\Account\RegisterModel.cs" />
|
||||
<Compile Include="ViewModels\CheckBoxViewModel.cs" />
|
||||
<Compile Include="ViewModels\CreateLog\EditPastViewModel.cs" />
|
||||
<Compile Include="ViewModels\DriverMileageItem.cs" />
|
||||
<Compile Include="ViewModels\DriverMileageViewModel.cs" />
|
||||
<Compile Include="ViewModels\Log\LogViewModel.cs" />
|
||||
@@ -328,6 +329,7 @@
|
||||
<Content Include="Views\Purpose\Details.cshtml" />
|
||||
<Content Include="Views\Purpose\Create.cshtml" />
|
||||
<Content Include="Views\Purpose\Edit.cshtml" />
|
||||
<Content Include="Views\CreateLog\EditPast.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="packages.config">
|
||||
|
||||
Reference in New Issue
Block a user