Skip to content

Commit

Permalink
#14, close #18: added appropriate display names and constraints on Le…
Browse files Browse the repository at this point in the history
…g model
  • Loading branch information
dmnisson committed Nov 20, 2018
1 parent 1786f8f commit d0222b2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Binary file modified DriverTracker/DriverTracker.db
Binary file not shown.
36 changes: 36 additions & 0 deletions DriverTracker/Models/Leg.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace DriverTracker.Models
{
Expand All @@ -9,15 +11,49 @@ public class Leg
public int LegID { get; set; }
public int DriverID { get; set; }

[Display(Name = "Pickup Address")]
[Required(ErrorMessage = "A leg must have a pickup address.")]
public string StartAddress { get; set; }

[Display(Name = "Requested Pickup Time")]
public DateTime? PickupRequestTime { get; set; }

[Display(Name = "Pickup Time")]
[Required(ErrorMessage = "A leg must have a pickup time.")]
public DateTime StartTime { get; set; }

[Display(Name = "Destination Address")]
[Required(ErrorMessage = "A leg must end somewhere.")]
public string DestinationAddress { get; set; }

[Display(Name = "Arrival Time")]
[Required(ErrorMessage = "A leg must end some time.")]
public DateTime ArrivalTime { get; set; }

[Display(Name = "Distance")]
[Required(ErrorMessage = "A leg must have a distance.")]
[Range(0, int.MaxValue, ErrorMessage = "Distance cannot be negative.")]
public decimal Distance { get; set; }

[Display(Name = "Fare")]
[Required(ErrorMessage = "A fare value is required. Enter 0 for free rides.")]
[Range(0, int.MaxValue, ErrorMessage = "Fare cannot be negative.")]
public decimal Fare { get; set; } // per-passenger fare

[Display(Name = "Passengers Aboard")]
[Required(ErrorMessage = "A number of passengers is required. Enter 0 for non-passenger legs.")]
[Range(0, int.MaxValue, ErrorMessage = "Number of passengers cannot be negative.")]
public int NumOfPassengersAboard { get; set; }

[Display(Name = "Pickups")]
[Required(ErrorMessage = "A number of passengers is required. Enter 0 for non-passenger legs.")]
[Range(0, int.MaxValue, ErrorMessage = "Number of pickups cannot be negative.")]
[LessThanOrEqualTo("NumOfPassengersAboard", ErrorMessage = "More passengers cannot have been picked up than are now aboard.")]
public int NumOfPassengersPickedUp { get; set; }

[Display(Name = "Fuel Cost")]
[Required(ErrorMessage = "A fuel cost is required.")]
[Range(0, Double.PositiveInfinity, ErrorMessage = "Fuel cost cannot be negative.")]
public decimal FuelCost { get; set; } // fuel cost per mile

public Driver Driver { get; set; }
Expand Down
24 changes: 24 additions & 0 deletions DriverTracker/Models/LessThanOrEqualToAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace DriverTracker.Models
{
public class LessThanOrEqualToAttribute : ValidationAttribute
{
private readonly string _dependentProperty;

public LessThanOrEqualToAttribute(string dependentProperty)
{
_dependentProperty = dependentProperty;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object model = validationContext.ObjectInstance;

return Convert.ToDecimal(value) > Convert.ToDecimal(model.GetType().GetProperty(_dependentProperty).GetValue(model))
? new ValidationResult(this.ErrorMessage)
: ValidationResult.Success;
}
}
}

0 comments on commit d0222b2

Please sign in to comment.