Skip to content

Updated filters and sorting logic to use field instead of columnName #1627

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fixed filtering for DateTimeOffset
  • Loading branch information
gogo4ds committed May 13, 2025
commit 0699cdc831d8399a1a09b85b71337c2609134ccc
1 change: 1 addition & 0 deletions Servers/UI/OJS.Servers.Ui/ClientApp/src/common/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const TITLE = 'Title';
// Users Mentors
export const QUOTA_RESET_TIME = 'Quota Reset Time';
export const REQUESTS_MADE = 'Requests Made';
export const TOTAL_REQUESTS_MADE = 'Total Requests Made';
export const QUOTA_LIMIT = 'Quota Limit';

// Mentor Prompt Templates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
QUOTA_LIMIT,
QUOTA_RESET_TIME,
REQUESTS_MADE,
TOTAL_REQUESTS_MADE,
USER_ID, USERNAME,
} from '../../../common/labels';
import { AdministrationGridColDef } from '../../../components/administration/utils/mui-utils';
Expand Down Expand Up @@ -54,6 +55,17 @@ const usersMentorsFilterableColumns: AdministrationGridColDef[] = [
headerAlign: 'center',
valueFormatter: (params) => params.value.toString(),
},
{
field: 'totalRequestsMade',
headerName: TOTAL_REQUESTS_MADE,
flex: 1,
type: 'number',
filterable: false,
sortable: false,
align: 'center',
headerAlign: 'center',
valueFormatter: (params) => params.value.toString(),
},
{
field: 'quotaLimit',
headerName: QUOTA_LIMIT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class UserMentorInListModel : IMapFrom<UserMentor>

public int? QuotaLimit { get; set; }

public int TotalRequestsMade { get; set; }

public DateTime CreatedOn { get; set; }

public DateTime? ModifiedOn { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,22 @@ private static Expression<Func<T, bool>> BuildFilteringExpression<T>(FilteringMo
else if (propertyType == typeof(DateTime) ||
Nullable.GetUnderlyingType(propertyType) == typeof(DateTime))
{
expression = BuildDateTimeExpression(filter.OperatorType, filter.Value, memberExpression);
expression = BuildDateTimeExpression<DateTime>(
filter.OperatorType,
filter.Value,
memberExpression,
DateTime.TryParse,
nameof(DateTime));
}
else if (propertyType == typeof(DateTimeOffset) ||
Nullable.GetUnderlyingType(propertyType) == typeof(DateTimeOffset))
{
expression = BuildDateTimeExpression<DateTimeOffset>(
filter.OperatorType,
filter.Value,
memberExpression,
DateTimeOffset.TryParse,
nameof(DateTimeOffset));
}

if (expression == null)
Expand Down Expand Up @@ -180,50 +195,42 @@ private static Expression BuildEnumExpression(string filterValue, Type propertyT
return expression;
}

private static Expression? BuildDateTimeExpression(OperatorType operatorType, string? value, MemberExpression property)
{
Expression? expression;
private delegate bool TryParseDelegate<T>(string input, out T result);

if (value == null || value.Equals(NullValue, StringComparison.OrdinalIgnoreCase))
{
if (!IsNullableType(property.Type))
{
throw new ArgumentException($"Cannot assign null to a non-nullable integer property: {property.Member.Name}");
}

expression = GetNullableTypesOperation(property, operatorType);
}
else if (DateTime.TryParse(value, out var dateTimeValue))
private static Expression BuildDateTimeExpression<T>(
OperatorType operatorType,
string? value,
MemberExpression property,
TryParseDelegate<T> tryParse,
string typeName)
where T : struct
{
if (string.IsNullOrWhiteSpace(value) || value.Equals(NullValue, StringComparison.OrdinalIgnoreCase))
{
var constant = Expression.Constant(dateTimeValue, IsNullableType(property.Type) ? typeof(DateTime?) : typeof(DateTime));
switch (operatorType)
{
case OperatorType.Equals:
expression = Expression.Equal(property, constant);
break;
case OperatorType.GreaterThan:
expression = Expression.GreaterThan(property, constant);
break;
case OperatorType.LessThan:
expression = Expression.LessThan(property, constant);
break;
case OperatorType.LessThanOrEqual:
expression = Expression.LessThanOrEqual(property, constant);
break;
case OperatorType.GreaterThanOrEqual:
expression = Expression.GreaterThanOrEqual(property, constant);
break;
default:
throw new ArgumentOutOfRangeException(
$"Property of type int cannot have {operatorType} operator");
}
return !IsNullableType(property.Type)
? throw new ArgumentException($"Cannot assign null to a non-nullable {typeName} property: {property.Member.Name}")
: GetNullableTypesOperation(property, operatorType);
}
else

if (!tryParse(value, out var parsedValue))
{
throw new ArgumentException($"Invalid value for integer property: {value}");
throw new ArgumentException($"Invalid value for {typeName} property: {value}");
}

return expression;
var targetType = IsNullableType(property.Type) ? typeof(T?) : typeof(T);
var constant = Expression.Constant(parsedValue, targetType);

return operatorType switch
{
OperatorType.Equals => Expression.Equal(property, constant),
OperatorType.GreaterThan => Expression.GreaterThan(property, constant),
OperatorType.LessThan => Expression.LessThan(property, constant),
OperatorType.LessThanOrEqual => Expression.LessThanOrEqual(property, constant),
OperatorType.GreaterThanOrEqual => Expression.GreaterThanOrEqual(property, constant),
_ => throw new ArgumentOutOfRangeException(
nameof(operatorType),
$"Property of type {typeName} cannot have {operatorType} operator"),
};
}

private static Expression? BuildBooleanExpression(OperatorType operatorType, string value, MemberExpression property)
Expand Down