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

Support GUID type #18

Closed
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Expand Up @@ -45,20 +45,20 @@ Supported operators for type are below;

Fop uses these query sign for preparing expression.

|Operators |Query Sign |Int |String | Char |DateTime|
|-------------------|------------|----|-------|------|--------|
|Equal |`==` | ✔️ | ✔️ | ✔️ | ✔️ |
|NotEqual |`!=` | ✔️ | ✔️ | ✔️ | ✔️ |
|GreaterThan |`>` | ✔️ | ❌ | ❌ | ✔️ |
|GreaterOrEqualThan |`>=` | ✔️ | ❌ | ❌ | ✔️ |
|LessThan |`<` | ✔️ | ❌ | ❌ | ✔️ |
|LessOrEqualThan |`<=` | ✔️ | ❌ | ❌ | ✔️ |
|Contains |`~=` | ❌ | ✔️ | ❌ | ❌ |
|NotContains |`!~=` | ❌ | ✔️ | ❌ | ❌ |
|StartsWith |`_=` | ❌ | ✔️ | ❌ | ❌ |
|NotStartsWith |`!_=` | ❌ | ✔️ | ❌ | ❌ |
|EndsWith |`\|=` | ❌ | ✔️ | ❌ | ❌ |
|NotEndsWith |`!\|=` | ❌ | ✔️ | ❌ | ❌ |
|Operators |Query Sign |Int |String | Char |DateTime|Guid|
|-------------------|------------|----|-------|------|--------|----|
|Equal |`==` | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
|NotEqual |`!=` | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
|GreaterThan |`>` | ✔️ | ❌ | ❌ | ✔️ | ❌ |
|GreaterOrEqualThan |`>=` | ✔️ | ❌ | ❌ | ✔️ | ❌ |
|LessThan |`<` | ✔️ | ❌ | ❌ | ✔️ | ❌ |
|LessOrEqualThan |`<=` | ✔️ | ❌ | ❌ | ✔️ | ❌ |
|Contains |`~=` | ❌ | ✔️ | ❌ | ❌ | ❌ |
|NotContains |`!~=` | ❌ | ✔️ | ❌ | ❌ | ❌ |
|StartsWith |`_=` | ❌ | ✔️ | ❌ | ❌ | ❌ |
|NotStartsWith |`!_=` | ❌ | ✔️ | ❌ | ❌ | ❌ |
|EndsWith |`\|=` | ❌ | ✔️ | ❌ | ❌ | ❌ |
|NotEndsWith |`!\|=` | ❌ | ✔️ | ❌ | ❌ | ❌ |

##### Example
api/students/
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/GuidDataTypeNotSupportedException.cs
@@ -0,0 +1,9 @@
namespace Fop.Exceptions
{
public class GuidDataTypeNotSupportedException : FopException
{

public GuidDataTypeNotSupportedException(string message) : base(message) { }

}
}
1 change: 1 addition & 0 deletions src/Extensions.cs
Expand Up @@ -26,6 +26,7 @@ static Extensions()
DataTypeStrategies.Add(FilterDataTypes.DateTime, new DateTimeDataTypeStrategy());
DataTypeStrategies.Add(FilterDataTypes.Boolean, new BooleanDataTypeStrategy());
DataTypeStrategies.Add(FilterDataTypes.Enum, new EnumDataTypeStrategy());
DataTypeStrategies.Add(FilterDataTypes.Guid, new GuidTypeStrategy());
}

public static (IQueryable<T>, int) ApplyFop<T>(this IQueryable<T> source, IFopRequest request)
Expand Down
3 changes: 2 additions & 1 deletion src/Filter/FilterEnumerations.cs
Expand Up @@ -33,6 +33,7 @@ public enum FilterDataTypes : byte
Char,
DateTime,
Boolean,
Enum
Enum,
Guid
}
}
5 changes: 5 additions & 0 deletions src/FopExpression/FopExpressionBuilder.cs
Expand Up @@ -185,6 +185,11 @@ private static FilterDataTypes GetFilterDataTypes(PropertyInfo pi)
return FilterDataTypes.Boolean;
}

if (propertyName == "Guid")
{
return FilterDataTypes.Guid;
}

throw new ArgumentOutOfRangeException();
}

Expand Down
31 changes: 31 additions & 0 deletions src/Strategies/GuidTypeStrategy.cs
@@ -0,0 +1,31 @@
using Fop.Exceptions;
using Fop.Filter;

namespace Fop.Strategies
{
public class GuidTypeStrategy : IFilterDataTypeStrategy
{
public string ConvertFilterToText(IFilter filter)
{
switch (filter.Operator)
{
case FilterOperators.Equal:
return filter.Key + " == new Guid(\"" + filter.Value + "\")";
case FilterOperators.NotEqual:
return filter.Key + " != new Guid(\"" + filter.Value + "\")";
case FilterOperators.Contains:
case FilterOperators.NotContains:
case FilterOperators.StartsWith:
case FilterOperators.NotStartsWith:
case FilterOperators.EndsWith:
case FilterOperators.NotEndsWith:
case FilterOperators.GreaterThan:
case FilterOperators.GreaterOrEqualThan:
case FilterOperators.LessThan:
case FilterOperators.LessOrEqualThan:
default:
throw new GuidDataTypeNotSupportedException($"Guid filter does not support {filter.Operator}");
}
}
}
}