Skip to content

Commit

Permalink
Issue #8 - domain object validation (#34)
Browse files Browse the repository at this point in the history
* issue #8 inital commit

* Issue #8 Cron validation in RecurringSchedule.cs

* more validation rules issue #8

* validation rules for default datetimes issue #8
  • Loading branch information
marblekirby authored and csharpfritz committed Jul 21, 2019
1 parent 720ce65 commit 8f2af12
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CronEspresso" Version="3.0.0" />
</ItemGroup>

</Project>
42 changes: 32 additions & 10 deletions src/Fritz.ResourceManagement.Domain/Person.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Data;

namespace Fritz.ResourceManagement.Domain
{
public class Person
{

public int Id { get; set; }
public int Id { get; set; }

public string GivenName { get; set; }
public string GivenName { get; set; }

public string MiddleName { get; set; }
public string MiddleName { get; set; }

public string SurName { get; set; }
public string SurName { get; set; }

public IList<PersonPersonType> PersonPersonType { get; set; }
public IList<PersonPersonType> PersonPersonType { get; set; }

public string PhoneNumber { get; set; }
public string PhoneNumber { get; set; }

public Schedule Schedule { get; set; }
public Schedule Schedule { get; set; }

#region Foreign Keys
#region Foreign Keys

public int ScheduleId { get; set; }
public int ScheduleId { get; set; }

#endregion
#endregion

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

var results = new List<ValidationResult>();

if (string.IsNullOrWhiteSpace(GivenName))
results.Add(new ValidationResult($"{nameof(GivenName)} cannot be null, empty or consist of whitespace only", new[] { nameof(GivenName) }));

if (string.IsNullOrEmpty(SurName))
results.Add(new ValidationResult($"{nameof(SurName)} cannot be null, empty or consist of whitespace only", new[] { nameof(GivenName) }));

if (string.IsNullOrWhiteSpace(PhoneNumber))
results.Add(new ValidationResult($"{nameof(PhoneNumber)} cannot be null, empty or consist of whitespace only", new[] { nameof(PhoneNumber) }));

if (PersonPersonType.Count == 0)
results.Add(new ValidationResult($"{nameof(PersonPersonType)} must contain atleast 1 item", new[] { nameof(PersonPersonType) }));

return results;
}

}

Expand Down
2 changes: 1 addition & 1 deletion src/Fritz.ResourceManagement.Domain/PersonPersonType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Fritz.ResourceManagement.Domain
namespace Fritz.ResourceManagement.Domain
{
public class PersonPersonType {

Expand Down
15 changes: 14 additions & 1 deletion src/Fritz.ResourceManagement.Domain/PersonType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Fritz.ResourceManagement.Domain
{
Expand All @@ -13,6 +14,18 @@ public class PersonType

public ICollection<PersonPersonType> PersonPersonTypes { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

var results = new List<ValidationResult>();

if (string.IsNullOrWhiteSpace(Name))
results.Add(new ValidationResult($"{nameof(Name)} cannot be null, empty or consist of only whitespace", new[] { nameof(Name) }));

return results;

}

}

}
60 changes: 46 additions & 14 deletions src/Fritz.ResourceManagement.Domain/RecurringSchedule.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using CronEspresso.NETCore.Utils;

namespace Fritz.ResourceManagement.Domain
{
/// <summary>
/// An event that re-occurs on the schedule given a recurrence pattern defined in CronPattern
/// </summary>
public class RecurringSchedule {
/// <summary>
/// An event that re-occurs on the schedule given a recurrence pattern defined in CronPattern
/// </summary>
public class RecurringSchedule
{

public int Id { get; set; }
public int Id { get; set; }

public int ScheduleId { get; set; }
public int ScheduleId { get; set; }

public ScheduleStatus Status { get; set; }
public ScheduleStatus Status { get; set; }

public string Name { get; set; }
public string Name { get; set; }

public string CronPattern { get; set; }
public string CronPattern { get; set; }

public TimeSpan Duration { get; set; }
public TimeSpan Duration { get; set; }

public DateTime MinStartDateTime { get; set; }
public DateTime MinStartDateTime { get; set; }

public DateTime MaxEndDateTime { get; set; }
public DateTime MaxEndDateTime { get; set; }

}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

var results = new List<ValidationResult>();

if (string.IsNullOrWhiteSpace(Name))
results.Add(new ValidationResult($"{nameof(Name)} cannot be null, empty or consist of only whitespace", new[] { nameof(Name) }));

if (string.IsNullOrWhiteSpace(CronPattern))
results.Add(new ValidationResult($"{nameof(CronPattern)} is required", new[] { nameof(CronPattern) }));
else
{
var cronValidationResult = CronHelpers.ValidateCron(CronPattern);
if (!cronValidationResult.IsValidCron)
results.Add(new ValidationResult($"{nameof(CronPattern) } is not a valid Cron pattern, {cronValidationResult.ValidationMessage}", new[] { nameof(CronPattern) }));
}

if (Duration.TotalMinutes < 30)
results.Add(new ValidationResult($"{nameof(Duration)} must be atleast 30 minutes", new[] { nameof(Duration) }));

if (MinStartDateTime == default)
results.Add(new ValidationResult($"{nameof(MinStartDateTime)} is required", new[] { nameof(MinStartDateTime) }));

if (MaxEndDateTime == default)
results.Add(new ValidationResult($"{nameof(MaxEndDateTime)} is required", new[] { nameof(MaxEndDateTime) }));

if (MaxEndDateTime < MinStartDateTime)
results.Add(new ValidationResult($"{nameof(MaxEndDateTime)} cannot be before {nameof(MinStartDateTime)}", new[] { nameof(MaxEndDateTime), nameof(MinStartDateTime) }));

return results;
}
}
}
32 changes: 25 additions & 7 deletions src/Fritz.ResourceManagement.Domain/ScheduleException.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Fritz.ResourceManagement.Domain
{
public class ScheduleException {
public class ScheduleException
{

public int Id { get; set; }
public int Id { get; set; }

public DateTime StartDateTime { get; set; }
public DateTime StartDateTime { get; set; }

public DateTime EndDateTime { get; set; }
public DateTime EndDateTime { get; set; }

public string Name { get; set; }
public string Name { get; set; }

}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

var results = new List<ValidationResult>();

if (string.IsNullOrWhiteSpace(Name))
results.Add(new ValidationResult($"{nameof(Name)} cannot be null, empty or consist of only whitespace", new[] { nameof(Name) }));

if (EndDateTime < StartDateTime)
results.Add(new ValidationResult($"{nameof(EndDateTime)} cannot be before {nameof(StartDateTime)}", new[] { nameof(StartDateTime), nameof(EndDateTime) }));
else if (EndDateTime < StartDateTime.AddDays(10))
results.Add(new ValidationResult($"{nameof(EndDateTime)} must be atleast 1 day after {nameof(StartDateTime)}", new[] { nameof(StartDateTime), nameof(EndDateTime) }));

return results;

}
}
}
18 changes: 17 additions & 1 deletion src/Fritz.ResourceManagement.Domain/ScheduleItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex

var results = new List<ValidationResult>();

if (Status == default)
results.Add(new ValidationResult($"{nameof(Status)} is required", new[] { nameof(Status) }));

if (string.IsNullOrWhiteSpace(Name))
results.Add(new ValidationResult($"{nameof(Name)} cannot be null, empty or consist of whitespace only", new[] { nameof(Name) }));
else if (Name.Length > 50)
results.Add(new ValidationResult($"{nameof(Name)} is greater than max length of 50", new[] { nameof(Name) }));

if(StartDateTime == default)
results.Add(new ValidationResult($"{nameof(StartDateTime)} is required", new[] { nameof(StartDateTime) }));

if (EndDateTime == default)
results.Add(new ValidationResult($"{nameof(EndDateTime)} is required", new[] { nameof(EndDateTime) }));

if (EndDateTime < StartDateTime)
results.Add(new ValidationResult("EndDateTime cannot be before StartDateTime", new[] { nameof(StartDateTime), nameof(EndDateTime) }));
results.Add(new ValidationResult($"{nameof(EndDateTime)} cannot be before {nameof(StartDateTime)}", new[] { nameof(StartDateTime), nameof(EndDateTime) }));
else if (EndDateTime < StartDateTime.AddMinutes(10))
results.Add(new ValidationResult($"{nameof(EndDateTime)} must be atleast 10 minutes after {nameof(StartDateTime)}", new[] { nameof(StartDateTime), nameof(EndDateTime) }));

return results;

Expand Down
33 changes: 27 additions & 6 deletions src/Fritz.ResourceManagement.Domain/TimeSlot.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Fritz.ResourceManagement.Domain
{
public class TimeSlot {
public class TimeSlot
{

public DateTime StartDateTime { get; set; }
public DateTime StartDateTime { get; set; }

public DateTime EndDateTime { get; set; }
public DateTime EndDateTime { get; set; }

public ScheduleStatus Status { get; set; }
public ScheduleStatus Status { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{

}
var results = new List<ValidationResult>();

if (Status == default)
results.Add(new ValidationResult($"{nameof(Status)} cannot have the default value of {default(ScheduleStatus)}", new[] { nameof(Status) }));

if (StartDateTime == default)
results.Add(new ValidationResult($"{nameof(StartDateTime)} is required", new[] { nameof(StartDateTime) }));

if (EndDateTime == default)
results.Add(new ValidationResult($"{nameof(EndDateTime)} is required", new[] { nameof(EndDateTime) }));

if (EndDateTime < StartDateTime)
results.Add(new ValidationResult($"{nameof(EndDateTime)} cannot be before {nameof(StartDateTime)}", new[] { nameof(StartDateTime), nameof(EndDateTime) }));

return results;

}
}
}

0 comments on commit 8f2af12

Please sign in to comment.