From a7261c1555764b08cc409b826164274caceddcc2 Mon Sep 17 00:00:00 2001 From: berkaybyr Date: Wed, 23 Aug 2023 14:19:42 +0300 Subject: [PATCH] DateTime Constraints Builder, 0.2.1 --- .../ConstraintsBuilderExtensions.cs | 8 ++ .../DateTimeConstraintsBuilder.cs | 74 +++++++++++++ .../EfCore.ConstraintsBuilder.csproj | 2 +- README.md | 100 ++++++++++-------- 4 files changed, 141 insertions(+), 43 deletions(-) create mode 100644 EfCore.ConstraintsBuilder/DateTimeConstraintsBuilder.cs diff --git a/EfCore.ConstraintsBuilder/ConstraintsBuilderExtensions.cs b/EfCore.ConstraintsBuilder/ConstraintsBuilderExtensions.cs index 20f3775..5d77c8b 100644 --- a/EfCore.ConstraintsBuilder/ConstraintsBuilderExtensions.cs +++ b/EfCore.ConstraintsBuilder/ConstraintsBuilderExtensions.cs @@ -45,6 +45,14 @@ public static ByteConstraintsBuilder AddConstraintsFor( return new ByteConstraintsBuilder(builder, keySelector.GetPropertyAccess(), serverProvider); } + public static DateTimeConstraintsBuilder AddConstraintsFor( + this EntityTypeBuilder builder, + Expression> keySelector, + SqlServerProvider serverProvider = SqlServerProvider.SqlServer) + where TEntity : class { + return new DateTimeConstraintsBuilder(builder, keySelector.GetPropertyAccess(), serverProvider); + } + // public static void AddConstraintsFromDataAnnotations(this EntityTypeBuilder builder, // SupportedConstraintServerType serverType = SupportedConstraintServerType.SqlServer) where TEntity : class { // var properties = typeof(TEntity).GetProperties(); diff --git a/EfCore.ConstraintsBuilder/DateTimeConstraintsBuilder.cs b/EfCore.ConstraintsBuilder/DateTimeConstraintsBuilder.cs new file mode 100644 index 0000000..f723f29 --- /dev/null +++ b/EfCore.ConstraintsBuilder/DateTimeConstraintsBuilder.cs @@ -0,0 +1,74 @@ +using System.Reflection; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace EfCore.ConstraintsBuilder; + +public sealed class DateTimeConstraintsBuilder where TEntity : class +{ + private readonly EntityTypeBuilder _builder; + private readonly SqlServerProvider _serverProvider; + + private readonly string _columnName; + private readonly string _tableName; + + internal DateTimeConstraintsBuilder( + EntityTypeBuilder 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 DateInBetween(DateTime min, DateTime max) => DateInBetween(_builder.CreateUniqueConstraintName(_columnName, nameof(DateInBetween)), min, max); + + public DateTimeConstraintsBuilder 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 DateMin(DateTime min) => DateMin(_builder.CreateUniqueConstraintName(_columnName, nameof(DateMin)), min); + + public DateTimeConstraintsBuilder DateMin(string uniqueConstraintName, DateTime min) { + _builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= '{min:yyyy-MM-dd}'")); + return this; + } + + public DateTimeConstraintsBuilder DateMax(DateTime max) => DateMax(_builder.CreateUniqueConstraintName(_columnName, nameof(DateMax)), max); + + public DateTimeConstraintsBuilder DateMax(string uniqueConstraintName, DateTime max) { + _builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= '{max:yyyy-MM-dd}'")); + return this; + } + + public DateTimeConstraintsBuilder TimeInBetween(TimeSpan min, TimeSpan max) => TimeInBetween(_builder.CreateUniqueConstraintName(_columnName, nameof(TimeInBetween)), min, max); + + public DateTimeConstraintsBuilder 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 TimeMin(TimeSpan min) => TimeMin(_builder.CreateUniqueConstraintName(_columnName, nameof(TimeMin)), min); + + public DateTimeConstraintsBuilder TimeMin(string uniqueConstraintName, TimeSpan min) { + _builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] >= '{min:hh\\:mm\\:ss}'")); + return this; + } + + public DateTimeConstraintsBuilder TimeMax(TimeSpan max) => TimeMax(_builder.CreateUniqueConstraintName(_columnName, nameof(TimeMax)), max); + + public DateTimeConstraintsBuilder TimeMax(string uniqueConstraintName, TimeSpan max) { + _builder.ToTable(x => x.HasCheckConstraint(uniqueConstraintName, $"[{_columnName}] <= '{max:hh\\:mm\\:ss}'")); + return this; + } + + +} \ No newline at end of file diff --git a/EfCore.ConstraintsBuilder/EfCore.ConstraintsBuilder.csproj b/EfCore.ConstraintsBuilder/EfCore.ConstraintsBuilder.csproj index 2638d79..17c776d 100644 --- a/EfCore.ConstraintsBuilder/EfCore.ConstraintsBuilder.csproj +++ b/EfCore.ConstraintsBuilder/EfCore.ConstraintsBuilder.csproj @@ -4,7 +4,7 @@ net7.0 enable enable - 0.2.0 + 0.2.1 true bberka bberka diff --git a/README.md b/README.md index 59e98d3..145f5e3 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ dotnet add package EfCore.ConstraintsBuilder - [x] Long (Int64) - [x] Short (Int16) - [x] Byte (Int8) +- [x] DateTime ### Supported Database Providers @@ -91,48 +92,48 @@ Example: `User_Name_Regex_UnqiueGuid` ### StringConstraintsBuilder Methods ```csharp - StringConstraintsBuilder EmailAddress(); - StringConstraintsBuilder EmailAddress(string constraintName); - StringConstraintsBuilder Url(); - StringConstraintsBuilder Url(string constraintName); - StringConstraintsBuilder PhoneNumber(); - StringConstraintsBuilder PhoneNumber(string constraintName); - StringConstraintsBuilder RegexExpression(string regex); - StringConstraintsBuilder RegexExpression(string constraintName, string regex); - StringConstraintsBuilder MinLength(int minLength); - StringConstraintsBuilder MinLength(string constraintName, int minLength); - StringConstraintsBuilder MaxLength(int maxLength); - StringConstraintsBuilder MaxLength(string constraintName, int maxLength); - StringConstraintsBuilder LengthBetween(int minLength, int maxLength); - StringConstraintsBuilder LengthBetween(string constraintName, int minLength, int maxLength); - StringConstraintsBuilder EqualsOneOf(IEnumerable acceptedValues); - StringConstraintsBuilder EqualsOneOf(string constraintName, IEnumerable acceptedValues); - StringConstraintsBuilder NotEqualsOneOf(IEnumerable acceptedValues); - StringConstraintsBuilder NotEqualsOneOf(string constraintName, IEnumerable acceptedValues); - StringConstraintsBuilder Equals(string value); - StringConstraintsBuilder Equals(string constraintName, string value); - StringConstraintsBuilder NotEquals(string value); - StringConstraintsBuilder NotEquals(string constraintName, string value); - StringConstraintsBuilder StartsWith(string value); - StringConstraintsBuilder StartsWith(string constraintName, string value); - StringConstraintsBuilder EndsWith(string value); - StringConstraintsBuilder EndsWith(string constraintName, string value); - StringConstraintsBuilder Contains(string value); - StringConstraintsBuilder Contains(string constraintName, string value); - StringConstraintsBuilder NotContains(string value); - StringConstraintsBuilder NotContains(string constraintName, string value); - StringConstraintsBuilder Empty(); - StringConstraintsBuilder Empty(string constraintName); - StringConstraintsBuilder NotEmpty(); - StringConstraintsBuilder NotEmpty(string constraintName); - StringConstraintsBuilder NullOrWhiteSpace(); - StringConstraintsBuilder NullOrWhiteSpace(string constraintName); - StringConstraintsBuilder NotNullOrWhiteSpace(); - StringConstraintsBuilder NotNullOrWhiteSpace(string constraintName); - StringConstraintsBuilder EqualProperty(Expression> propertySelector); - StringConstraintsBuilder EqualProperty(string constraintName, Expression> propertySelector); - StringConstraintsBuilder NotEqualProperty(Expression> propertySelector); - StringConstraintsBuilder NotEqualProperty(string constraintName, Expression> propertySelector); +StringConstraintsBuilder EmailAddress(); +StringConstraintsBuilder EmailAddress(string constraintName); +StringConstraintsBuilder Url(); +StringConstraintsBuilder Url(string constraintName); +StringConstraintsBuilder PhoneNumber(); +StringConstraintsBuilder PhoneNumber(string constraintName); +StringConstraintsBuilder RegexExpression(string regex); +StringConstraintsBuilder RegexExpression(string constraintName, string regex); +StringConstraintsBuilder MinLength(int minLength); +StringConstraintsBuilder MinLength(string constraintName, int minLength); +StringConstraintsBuilder MaxLength(int maxLength); +StringConstraintsBuilder MaxLength(string constraintName, int maxLength); +StringConstraintsBuilder LengthBetween(int minLength, int maxLength); +StringConstraintsBuilder LengthBetween(string constraintName, int minLength, int maxLength); +StringConstraintsBuilder EqualsOneOf(IEnumerable acceptedValues); +StringConstraintsBuilder EqualsOneOf(string constraintName, IEnumerable acceptedValues); +StringConstraintsBuilder NotEqualsOneOf(IEnumerable acceptedValues); +StringConstraintsBuilder NotEqualsOneOf(string constraintName, IEnumerable acceptedValues); +StringConstraintsBuilder Equals(string value); +StringConstraintsBuilder Equals(string constraintName, string value); +StringConstraintsBuilder NotEquals(string value); +StringConstraintsBuilder NotEquals(string constraintName, string value); +StringConstraintsBuilder StartsWith(string value); +StringConstraintsBuilder StartsWith(string constraintName, string value); +StringConstraintsBuilder EndsWith(string value); +StringConstraintsBuilder EndsWith(string constraintName, string value); +StringConstraintsBuilder Contains(string value); +StringConstraintsBuilder Contains(string constraintName, string value); +StringConstraintsBuilder NotContains(string value); +StringConstraintsBuilder NotContains(string constraintName, string value); +StringConstraintsBuilder Empty(); +StringConstraintsBuilder Empty(string constraintName); +StringConstraintsBuilder NotEmpty(); +StringConstraintsBuilder NotEmpty(string constraintName); +StringConstraintsBuilder NullOrWhiteSpace(); +StringConstraintsBuilder NullOrWhiteSpace(string constraintName); +StringConstraintsBuilder NotNullOrWhiteSpace(); +StringConstraintsBuilder NotNullOrWhiteSpace(string constraintName); +StringConstraintsBuilder EqualProperty(Expression> propertySelector); +StringConstraintsBuilder EqualProperty(string constraintName, Expression> propertySelector); +StringConstraintsBuilder NotEqualProperty(Expression> propertySelector); +StringConstraintsBuilder NotEqualProperty(string constraintName, Expression> propertySelector); ``` ### IntConstraintsBuilder Methods @@ -186,3 +187,18 @@ ByteConstraintsBuilder EqualOneOf(string uniqueConstraintName, IEnumera ``` +### DateTimeConstraintsBuilder Methods +```csharp +DateTimeConstraintsBuilder DateInBetween(DateTime min, DateTime max); +DateTimeConstraintsBuilder DateInBetween(string uniqueConstraintName, DateTime min, DateTime max); +DateTimeConstraintsBuilder DateMin(DateTime min); +DateTimeConstraintsBuilder DateMin(string uniqueConstraintName, DateTime min); +DateTimeConstraintsBuilder DateMax(DateTime max); +DateTimeConstraintsBuilder DateMax(string uniqueConstraintName, DateTime max); +DateTimeConstraintsBuilder TimeInBetween(TimeSpan min, TimeSpan max); +DateTimeConstraintsBuilder TimeInBetween(string uniqueConstraintName, TimeSpan min, TimeSpan max); +DateTimeConstraintsBuilder TimeMin(TimeSpan min); +DateTimeConstraintsBuilder TimeMin(string uniqueConstraintName, TimeSpan min); +DateTimeConstraintsBuilder TimeMax(TimeSpan max); +DateTimeConstraintsBuilder TimeMax(string uniqueConstraintName, TimeSpan max); +``` \ No newline at end of file