Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- master
env:
APP_NAME: DynamicExpression
VERSION: 10.0.1
VERSION: 10.0.2
NUGET_HOST: https://api.nuget.org/v3/index.json
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY }}
jobs:
Expand Down
3 changes: 1 addition & 2 deletions DynamicExpression/DynamicExpression.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<PackageTags>Lampda, Expression, Linq, Dynamic, Criteria, Query, Paging, Pagination, Sort, Sorting</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
- Updated NuGets.
- Added Geometry converter for query model binder.
- Fixed The QueryModelBinder for enum values in querystring criteria.
</PackageReleaseNotes>
<PackageLicense>https://raw.githubusercontent.com/vivet/DynamicExpression/master/LICENSE</PackageLicense>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
128 changes: 74 additions & 54 deletions DynamicExpression/ModelBinders/QueryModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,61 +182,81 @@ protected virtual TCriteria GetCriteria(HttpRequest httpRequest, Query<TCriteria

var criteria = new TCriteria();

typeof(TCriteria)
.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)
.ToList()
.ForEach(x =>
var propertyInfos = typeof(TCriteria)
.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);

foreach (var propertyInfo in propertyInfos)
{
var bodyValue = propertyInfo
.GetValue(query.Criteria);

if (bodyValue != null)
{
propertyInfo
.SetValue(criteria, bodyValue);

continue;
}

var success = httpRequest.Query.TryGetValue($"{nameof(Criteria)}.{propertyInfo.Name}", out var values);

if (!success)
{
continue;
}

var value = values
.FirstOrDefault();

if (value == null)
{
continue;
}

if (propertyInfo.PropertyType == typeof(TimeSpan) || propertyInfo.PropertyType == typeof(TimeSpan?))
{
propertyInfo
.SetValue(criteria, TimeSpan.Parse(value));
}
else if (propertyInfo.PropertyType == typeof(TimeOnly) || propertyInfo.PropertyType == typeof(TimeOnly?))
{
propertyInfo
.SetValue(criteria, TimeOnly.Parse(value));
}
else if (propertyInfo.PropertyType == typeof(DateOnly) || propertyInfo.PropertyType == typeof(DateOnly?))
{
propertyInfo
.SetValue(criteria, DateOnly.Parse(value));
}
else if (propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?))
{
propertyInfo
.SetValue(criteria, DateTime.Parse(value));
}
else if (propertyInfo.PropertyType == typeof(DateTimeOffset) || propertyInfo.PropertyType == typeof(DateTimeOffset?))
{
var bodyValue = x.GetValue(query.Criteria);

if (bodyValue != null)
{
x.SetValue(criteria, bodyValue);

return;
}

var success = httpRequest.Query.TryGetValue($"{nameof(Criteria)}.{x.Name}", out var values);
if (!success)
{
return;
}

var value = values.FirstOrDefault();
if (value == null)
{
return;
}

if (x.PropertyType == typeof(TimeSpan) || x.PropertyType == typeof(TimeSpan?))
{
x.SetValue(criteria, TimeSpan.Parse(value));
}
else if (x.PropertyType == typeof(TimeOnly) || x.PropertyType == typeof(TimeOnly?))
{
x.SetValue(criteria, TimeOnly.Parse(value));
}
else if (x.PropertyType == typeof(DateOnly) || x.PropertyType == typeof(DateOnly?))
{
x.SetValue(criteria, DateOnly.Parse(value));
}
else if (x.PropertyType == typeof(DateTime) || x.PropertyType == typeof(DateTime?))
{
x.SetValue(criteria, DateTime.Parse(value));
}
else if (x.PropertyType == typeof(DateTimeOffset) || x.PropertyType == typeof(DateTimeOffset?))
{
x.SetValue(criteria, DateTimeOffset.Parse(value));
}
else if (x.PropertyType == typeof(Guid) || x.PropertyType == typeof(Guid?))
{
x.SetValue(criteria, Guid.Parse(value));
}
else
{
x.SetValue(criteria, value);
}
});
propertyInfo
.SetValue(criteria, DateTimeOffset.Parse(value));
}
else if (propertyInfo.PropertyType == typeof(Guid) || propertyInfo.PropertyType == typeof(Guid?))
{
propertyInfo
.SetValue(criteria, Guid.Parse(value));
}
else if (propertyInfo.PropertyType.IsEnum || (Nullable.GetUnderlyingType(propertyInfo.PropertyType)?.IsEnum ?? false))
{
var enumType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
var enumValue = Enum.Parse(enumType, value, ignoreCase: true);

propertyInfo
.SetValue(criteria, enumValue);
}
else
{
propertyInfo
.SetValue(criteria, value);
}
}

return criteria;
}
Expand Down
Loading