signumsoftware / framework

Open Source framework for writing data-centric applications using the latest versions of .Net Core, C# (not-nullable), ASP.NET Web API, Typescript (strict), React, D3 and Sql Server or PostgreeSQL
https://www.signumsoftware.com/en/Framework
MIT License
222 stars 84 forks source link

How to use [CurrentEntity] to filter current entity #574

Closed ce-kkoziel closed 1 year ago

ce-kkoziel commented 2 years ago

Dear @olmobrutall ,

Can you explain, or share some documentation, how to use [CurrentEntity] keyword while building custom user queries?

With regards, Kozieł Karol

doganc commented 2 years ago

hi, i've just use this expression my UserQuery like this, I hope it's explain your question,

CurrentUser

ce-kkoziel commented 2 years ago

I have something like this. I have selected entity type and I want bind his Id property to filter value, but filter property is a string type. It is possible?

obraz

olmobrutall commented 2 years ago

You could use RestLog.QueryString.Any.Value EqualsTo [CurrentEntity].Id

Also using [+ Add Group] with an AND and RestLog.QueryString.Any as token you could filter by:

ce-kkoziel commented 2 years ago

@olmobrutall i was trying like you said.

I have currently concrete entity type selected and filter like this, but still not working.

obraz

Client app in default filters do not sends ID rather than this expression.

obraz

When I change this value to concrete ID through dev tools and resend the request, executeQuery returns result.

olmobrutall commented 2 years ago

I can not see in the screenshot if you are in a UserQuery or in a SearchControl

If in a UserQuery it should work.

The code that does the [CurrentEntity].Id evaluation is this one:

https://github.com/signumsoftware/framework/blob/004aa1dd50fc8ab161fe634d2be5fc196a165275/Signum.Entities.Extensions/UserAssets/FilterValueConverters/CurrentEntityConverter.cs#L40

ce-kkoziel commented 2 years ago

@olmobrutall thanks for pointing place in code. We will try to find why it does not work for us.

ce-kkoziel commented 2 years ago

@olmobrutall The expression is not working when the left operand is a string type value. Then expression "[CurrentEntity].Id" is treated just as a string. In my case the Value property from QueryString is a string, but Entity Id property is Guid.

olmobrutall commented 2 years ago

in your Starter.cs try adding the following:

FilterValueConverter.SpecificConverters.Add(FilterType.String, new CurrentEntityConverter())

ce-kkoziel commented 2 years ago

Converter is executing for string left operand, but actually [CurrentEntity].Id is PrimaryKey type, and type convertion does not work for string <-> PrimaryKey pair, becouse type converter for PrimaryKey type allow convertion only for int, long, guid.

Unable to convert '2faeeb27-b96b-4588-be67-891776382851' (of type PrimaryKey) to type string

I was trying to use [CurrentEntity].Id.ToString expression but .NET throws AmbiguousMatchException. PrimaryKey have 2 ToString method overloads.

Further, I was trying to use [CurrentEntity].Id.Object.ToString but, PrimaryKey Object is a field.

Finally, I was trying to implement custom IFilterValueConverter implementation based on CurrentEntityConverter and use custom implementation of SimpleMemberEvaluator where evaluator will also check fields, but CurrentEntityConverter have usage of private static currentEntityVariable.

@olmobrutall any other hint how to use it without Framework changes?

olmobrutall commented 2 years ago

Hi @ce-kkoziel, to me looks like this line is not doing its work:

https://github.com/signumsoftware/framework/blob/9f7ad587eae697e4f3e28c09a658bb93b7fc4be2/Signum.Entities.Extensions/UserAssets/FilterValueConverter.cs#L164

The FilterValueConverter already has a targetType and the fact that a PrimaryKey can not be converted to a string is a bug/missing feature.

Feel free to extend the implementation of CanConvertTo / ConvertTo in PrimaryKeyTypeConverter calling ToString.

Changing ConvertFrom / CanConvertFrom could be more problematic because you don't have context to know that is the expected value (int, long, Guid)

You don't need to avoid changing the framework at all costs, you can make any reasonable change or create an extension point for the unreasonable ones.

olmobrutall commented 1 year ago

I think this error should not exist since 0cec7f7992602ead8db4a3c663389765cc534390 because now the FilterConverters are registered in a flat list instead of per FilterType.

Closing the issue.