Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relative date time feature #212

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/Gridify/Builder/BaseQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,28 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que

object? value = valueExpression.ValueToken.Text;

var underlyingBodyType = Nullable.GetUnderlyingType(body.Type);
// execute user custom Convertor
if (convertor != null)
value = convertor.Invoke(valueExpression.ValueToken.Text);

// handle the `null` keyword in value
if (mapper.Configuration.AllowNullSearch && op.Kind is SyntaxKind.Equal or SyntaxKind.NotEqual && value.ToString() == "null")
else if (mapper.Configuration.AllowNullSearch && op.Kind is SyntaxKind.Equal or SyntaxKind.NotEqual && value.ToString() == "null")
value = null;

// type fixer
// Check if body.Type is a nullable type and get its underlying type issue #134
var underlyingBodyType = Nullable.GetUnderlyingType(body.Type);
if (value is string strValue && (underlyingBodyType ?? body.Type) != strValue.GetType())

else if (body.Type == typeof(DateTime) || underlyingBodyType == typeof(DateTime))
{
var dateTime = ParseRelativeDate(valueExpression.ValueToken.Text);
if (dateTime.HasValue)
{
value = dateTime.Value;
if (mapper.Configuration.DefaultDateTimeKind.HasValue)
{
value = DateTime.SpecifyKind((DateTime)value, mapper.Configuration.DefaultDateTimeKind.Value);
}
}
}
else if (value is string strValue && (underlyingBodyType ?? body.Type) != strValue.GetType())
{
// handle bool, github issue #71
if (body.Type == typeof(bool) && strValue is "true" or "false" or "1" or "0")
Expand All @@ -199,14 +209,6 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que
{
return BuildAlwaysFalseQuery(parameter);
}

if (value is DateTime dateTime)
{
if (mapper.Configuration.DefaultDateTimeKind.HasValue)
{
value = DateTime.SpecifyKind(dateTime, mapper.Configuration.DefaultDateTimeKind.Value);
}
}
}

// handle case-Insensitive search
Expand Down Expand Up @@ -240,4 +242,14 @@ private static LambdaExpression UpdateIndexerKey(LambdaExpression exp, string ke
var body = new ReplaceExpressionVisitor(exp.Parameters[1], newValue).Visit(exp.Body);
return Expression.Lambda(body, exp.Parameters);
}

private static DateTime? ParseRelativeDate(string input)
{
var parser = new Chronic.Core.Parser();
var result = parser.Parse(input);
if (result != null) return result.Start;

DateTime.TryParse(input, out var dateTime);
return dateTime;
}
}
1 change: 1 addition & 0 deletions src/Gridify/Gridify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Chronic.Core" Version="0.4.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />

</ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions test/Gridify.Tests/GridifyExtensionsShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,29 @@ public void ApplyFiltering_UnmappedFields_ShouldSkipWhenIgnored()
Assert.True(actual.Any());
}

[Fact]
public void ApplyFiltering_DateTime()
{
var actual = _fakeRepository.AsQueryable()
.ApplyFiltering($"MyDateTime>{DateTime.UtcNow.ToShortDateString()}")
.ToList();
var expected = _fakeRepository.Where(q => q.MyDateTime > DateTime.UtcNow.Date).ToList();

Assert.Equal(expected.Count, actual.Count);
}

[Fact]
public void ApplyFiltering_DateTime_Relative()
{
var actual = _fakeRepository.AsQueryable()
.ApplyFiltering("MyDateTime>today")
.ToList();
var expected = _fakeRepository.Where(q => q.MyDateTime > DateTime.UtcNow.Date).ToList();

Assert.Equal(expected.Count, actual.Count);
}


#endregion


Expand Down
Loading