Skip to content

Commit

Permalink
DateTime Constraints Builder, 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bberka committed Aug 23, 2023
1 parent b7c9a9d commit a7261c1
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 43 deletions.
8 changes: 8 additions & 0 deletions EfCore.ConstraintsBuilder/ConstraintsBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public static ByteConstraintsBuilder<TEntity> AddConstraintsFor<TEntity>(
return new ByteConstraintsBuilder<TEntity>(builder, keySelector.GetPropertyAccess(), serverProvider);
}

public static DateTimeConstraintsBuilder<TEntity> AddConstraintsFor<TEntity>(
this EntityTypeBuilder<TEntity> builder,
Expression<Func<TEntity, DateTime>> keySelector,
SqlServerProvider serverProvider = SqlServerProvider.SqlServer)
where TEntity : class {
return new DateTimeConstraintsBuilder<TEntity>(builder, keySelector.GetPropertyAccess(), serverProvider);
}

// public static void AddConstraintsFromDataAnnotations<TEntity>(this EntityTypeBuilder<TEntity> builder,
// SupportedConstraintServerType serverType = SupportedConstraintServerType.SqlServer) where TEntity : class {
// var properties = typeof(TEntity).GetProperties();
Expand Down
74 changes: 74 additions & 0 deletions EfCore.ConstraintsBuilder/DateTimeConstraintsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EfCore.ConstraintsBuilder;

public sealed class DateTimeConstraintsBuilder<TEntity> where TEntity : class
{
private readonly EntityTypeBuilder<TEntity> _builder;
private readonly SqlServerProvider _serverProvider;

private readonly string _columnName;
private readonly string _tableName;

internal DateTimeConstraintsBuilder(
EntityTypeBuilder<TEntity> builder,
PropertyInfo propertyInfo,
SqlServerProvider serverProvider) {
var isDataTypeMatch = propertyInfo.PropertyType == typeof(DateTime) ||
propertyInfo.PropertyType == typeof(DateTime?);
if (!isDataTypeMatch) {
throw new ArgumentException("Property type is not datetime. PropertyName: " + propertyInfo.Name, nameof(propertyInfo));
}

_builder = builder;
_serverProvider = serverProvider;
_tableName = _builder.Metadata.GetTableName() ?? typeof(TEntity).Name;
_columnName = _builder.Metadata.GetProperty(propertyInfo.Name).GetColumnName();
}

public DateTimeConstraintsBuilder<TEntity> DateInBetween(DateTime min, DateTime max) => DateInBetween(_builder.CreateUniqueConstraintName(_columnName, nameof(DateInBetween)), min, max);

public DateTimeConstraintsBuilder<TEntity> DateInBetween(string uniqueConstraintName, DateTime min, DateTime max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= '{min:yyyy-MM-dd}' AND [{_columnName}] <= '{max:yyyy-MM-dd}'"));
return this;
}

public DateTimeConstraintsBuilder<TEntity> DateMin(DateTime min) => DateMin(_builder.CreateUniqueConstraintName(_columnName, nameof(DateMin)), min);

public DateTimeConstraintsBuilder<TEntity> DateMin(string uniqueConstraintName, DateTime min) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= '{min:yyyy-MM-dd}'"));
return this;
}

public DateTimeConstraintsBuilder<TEntity> DateMax(DateTime max) => DateMax(_builder.CreateUniqueConstraintName(_columnName, nameof(DateMax)), max);

public DateTimeConstraintsBuilder<TEntity> DateMax(string uniqueConstraintName, DateTime max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= '{max:yyyy-MM-dd}'"));
return this;
}

public DateTimeConstraintsBuilder<TEntity> TimeInBetween(TimeSpan min, TimeSpan max) => TimeInBetween(_builder.CreateUniqueConstraintName(_columnName, nameof(TimeInBetween)), min, max);

public DateTimeConstraintsBuilder<TEntity> TimeInBetween(string uniqueConstraintName, TimeSpan min, TimeSpan max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= '{min:hh\\:mm\\:ss}' AND [{_columnName}] <= '{max:hh\\:mm\\:ss}'"));
return this;
}

public DateTimeConstraintsBuilder<TEntity> TimeMin(TimeSpan min) => TimeMin(_builder.CreateUniqueConstraintName(_columnName, nameof(TimeMin)), min);

public DateTimeConstraintsBuilder<TEntity> TimeMin(string uniqueConstraintName, TimeSpan min) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= '{min:hh\\:mm\\:ss}'"));
return this;
}

public DateTimeConstraintsBuilder<TEntity> TimeMax(TimeSpan max) => TimeMax(_builder.CreateUniqueConstraintName(_columnName, nameof(TimeMax)), max);

public DateTimeConstraintsBuilder<TEntity> TimeMax(string uniqueConstraintName, TimeSpan max) {
_builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= '{max:hh\\:mm\\:ss}'"));
return this;
}


}
2 changes: 1 addition & 1 deletion EfCore.ConstraintsBuilder/EfCore.ConstraintsBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>bberka</Authors>
<Company>bberka</Company>
Expand Down
100 changes: 58 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dotnet add package EfCore.ConstraintsBuilder
- [x] Long (Int64)
- [x] Short (Int16)
- [x] Byte (Int8)
- [x] DateTime


### Supported Database Providers
Expand Down Expand Up @@ -91,48 +92,48 @@ Example: `User_Name_Regex_UnqiueGuid`

### StringConstraintsBuilder Methods
```csharp
StringConstraintsBuilder<TEntity> EmailAddress();
StringConstraintsBuilder<TEntity> EmailAddress(string constraintName);
StringConstraintsBuilder<TEntity> Url();
StringConstraintsBuilder<TEntity> Url(string constraintName);
StringConstraintsBuilder<TEntity> PhoneNumber();
StringConstraintsBuilder<TEntity> PhoneNumber(string constraintName);
StringConstraintsBuilder<TEntity> RegexExpression(string regex);
StringConstraintsBuilder<TEntity> RegexExpression(string constraintName, string regex);
StringConstraintsBuilder<TEntity> MinLength(int minLength);
StringConstraintsBuilder<TEntity> MinLength(string constraintName, int minLength);
StringConstraintsBuilder<TEntity> MaxLength(int maxLength);
StringConstraintsBuilder<TEntity> MaxLength(string constraintName, int maxLength);
StringConstraintsBuilder<TEntity> LengthBetween(int minLength, int maxLength);
StringConstraintsBuilder<TEntity> LengthBetween(string constraintName, int minLength, int maxLength);
StringConstraintsBuilder<TEntity> EqualsOneOf(IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> EqualsOneOf(string constraintName, IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> NotEqualsOneOf(IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> NotEqualsOneOf(string constraintName, IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> Equals(string value);
StringConstraintsBuilder<TEntity> Equals(string constraintName, string value);
StringConstraintsBuilder<TEntity> NotEquals(string value);
StringConstraintsBuilder<TEntity> NotEquals(string constraintName, string value);
StringConstraintsBuilder<TEntity> StartsWith(string value);
StringConstraintsBuilder<TEntity> StartsWith(string constraintName, string value);
StringConstraintsBuilder<TEntity> EndsWith(string value);
StringConstraintsBuilder<TEntity> EndsWith(string constraintName, string value);
StringConstraintsBuilder<TEntity> Contains(string value);
StringConstraintsBuilder<TEntity> Contains(string constraintName, string value);
StringConstraintsBuilder<TEntity> NotContains(string value);
StringConstraintsBuilder<TEntity> NotContains(string constraintName, string value);
StringConstraintsBuilder<TEntity> Empty();
StringConstraintsBuilder<TEntity> Empty(string constraintName);
StringConstraintsBuilder<TEntity> NotEmpty();
StringConstraintsBuilder<TEntity> NotEmpty(string constraintName);
StringConstraintsBuilder<TEntity> NullOrWhiteSpace();
StringConstraintsBuilder<TEntity> NullOrWhiteSpace(string constraintName);
StringConstraintsBuilder<TEntity> NotNullOrWhiteSpace();
StringConstraintsBuilder<TEntity> NotNullOrWhiteSpace(string constraintName);
StringConstraintsBuilder<TEntity> EqualProperty(Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> EqualProperty(string constraintName, Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> NotEqualProperty(Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> NotEqualProperty(string constraintName, Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> EmailAddress();
StringConstraintsBuilder<TEntity> EmailAddress(string constraintName);
StringConstraintsBuilder<TEntity> Url();
StringConstraintsBuilder<TEntity> Url(string constraintName);
StringConstraintsBuilder<TEntity> PhoneNumber();
StringConstraintsBuilder<TEntity> PhoneNumber(string constraintName);
StringConstraintsBuilder<TEntity> RegexExpression(string regex);
StringConstraintsBuilder<TEntity> RegexExpression(string constraintName, string regex);
StringConstraintsBuilder<TEntity> MinLength(int minLength);
StringConstraintsBuilder<TEntity> MinLength(string constraintName, int minLength);
StringConstraintsBuilder<TEntity> MaxLength(int maxLength);
StringConstraintsBuilder<TEntity> MaxLength(string constraintName, int maxLength);
StringConstraintsBuilder<TEntity> LengthBetween(int minLength, int maxLength);
StringConstraintsBuilder<TEntity> LengthBetween(string constraintName, int minLength, int maxLength);
StringConstraintsBuilder<TEntity> EqualsOneOf(IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> EqualsOneOf(string constraintName, IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> NotEqualsOneOf(IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> NotEqualsOneOf(string constraintName, IEnumerable<string> acceptedValues);
StringConstraintsBuilder<TEntity> Equals(string value);
StringConstraintsBuilder<TEntity> Equals(string constraintName, string value);
StringConstraintsBuilder<TEntity> NotEquals(string value);
StringConstraintsBuilder<TEntity> NotEquals(string constraintName, string value);
StringConstraintsBuilder<TEntity> StartsWith(string value);
StringConstraintsBuilder<TEntity> StartsWith(string constraintName, string value);
StringConstraintsBuilder<TEntity> EndsWith(string value);
StringConstraintsBuilder<TEntity> EndsWith(string constraintName, string value);
StringConstraintsBuilder<TEntity> Contains(string value);
StringConstraintsBuilder<TEntity> Contains(string constraintName, string value);
StringConstraintsBuilder<TEntity> NotContains(string value);
StringConstraintsBuilder<TEntity> NotContains(string constraintName, string value);
StringConstraintsBuilder<TEntity> Empty();
StringConstraintsBuilder<TEntity> Empty(string constraintName);
StringConstraintsBuilder<TEntity> NotEmpty();
StringConstraintsBuilder<TEntity> NotEmpty(string constraintName);
StringConstraintsBuilder<TEntity> NullOrWhiteSpace();
StringConstraintsBuilder<TEntity> NullOrWhiteSpace(string constraintName);
StringConstraintsBuilder<TEntity> NotNullOrWhiteSpace();
StringConstraintsBuilder<TEntity> NotNullOrWhiteSpace(string constraintName);
StringConstraintsBuilder<TEntity> EqualProperty(Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> EqualProperty(string constraintName, Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> NotEqualProperty(Expression<Func<TEntity, string>> propertySelector);
StringConstraintsBuilder<TEntity> NotEqualProperty(string constraintName, Expression<Func<TEntity, string>> propertySelector);
```

### IntConstraintsBuilder Methods
Expand Down Expand Up @@ -186,3 +187,18 @@ ByteConstraintsBuilder<TEntity> EqualOneOf(string uniqueConstraintName, IEnumera
```


### DateTimeConstraintsBuilder Methods
```csharp
DateTimeConstraintsBuilder<TEntity> DateInBetween(DateTime min, DateTime max);
DateTimeConstraintsBuilder<TEntity> DateInBetween(string uniqueConstraintName, DateTime min, DateTime max);
DateTimeConstraintsBuilder<TEntity> DateMin(DateTime min);
DateTimeConstraintsBuilder<TEntity> DateMin(string uniqueConstraintName, DateTime min);
DateTimeConstraintsBuilder<TEntity> DateMax(DateTime max);
DateTimeConstraintsBuilder<TEntity> DateMax(string uniqueConstraintName, DateTime max);
DateTimeConstraintsBuilder<TEntity> TimeInBetween(TimeSpan min, TimeSpan max);
DateTimeConstraintsBuilder<TEntity> TimeInBetween(string uniqueConstraintName, TimeSpan min, TimeSpan max);
DateTimeConstraintsBuilder<TEntity> TimeMin(TimeSpan min);
DateTimeConstraintsBuilder<TEntity> TimeMin(string uniqueConstraintName, TimeSpan min);
DateTimeConstraintsBuilder<TEntity> TimeMax(TimeSpan max);
DateTimeConstraintsBuilder<TEntity> TimeMax(string uniqueConstraintName, TimeSpan max);
```

0 comments on commit a7261c1

Please sign in to comment.