Skip to content

Commit

Permalink
string concatination
Browse files Browse the repository at this point in the history
  • Loading branch information
m0hamad-j committed Nov 17, 2023
1 parent 00f6fda commit f9f1919
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 48 deletions.
2 changes: 1 addition & 1 deletion EFSorter/EFSorter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Description>Ef soreter for IQuery</Description>
<FileVersion>2.0.1.1</FileVersion>
<Version>2.1.5</Version>
<Version>2.1.6</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
98 changes: 51 additions & 47 deletions EFSorter/Filters/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,84 @@ public class Condition
public string Type { get; set; }
public string Operator { get; set; }
public string Value { get; set; }

public Condition() { }

Check warning on line 10 in EFSorter/Filters/Condition.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Field' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in EFSorter/Filters/Condition.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Type' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in EFSorter/Filters/Condition.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Operator' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in EFSorter/Filters/Condition.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public Condition(string field, string type, string @operator, string value)
{
Field = field;
Type = type;
Operator = @operator;
Value = value;
Value = type == "number" ? value : "\"" + value + "\"";
}

public string Build() => Type.ToLowerInvariant() switch
{
"text" => mapTextCondition(),
"number" => mapNumberCondition(),
"date" => mapDateCondition(),
"guids" => mapGuidsCondition(),
"object" => mapObjectCondition(),
"text" => MapTextCondition(),
"number" => MapNumberCondition(),
"date" => MapDateCondition(),
"guids" => MapGuidsCondition(),
"object" => MapObjectCondition(),
_ => "",
};

private string mapDateCondition()
private string MapDateCondition()
{
var isDate = DateTime.TryParse(Value, out var validDate);
if (isDate)
return Operator switch
{
"equals" => Field + "= \"" + validDate.ToString("s") + "\"",
"notEqual" => Field + "!= \"" + validDate.ToString("s") + "\"",
"lessThan" => Field + "< \"" + validDate.ToString("s") + "\"",
"greaterThan" => Field + "> \"" + validDate.ToString("s") + "\"",
"blank" => Field + "==null",
"notBlank" => Field + "!=null",
_ => "",
};
return "";
if (!isDate) return "";

return Operator switch
{
"equals" => $"{Field} = {validDate:s}",
"notEqual" => $"{Field} != {validDate:s}",
"lessThan" => $"{Field} < {validDate:s}",
"greaterThan" => $"{Field} > {validDate:s}",
"blank" => $"{Field} == null",
"notBlank" => $"{Field} != null",
_ => "",
};
}

private string mapNumberCondition() => Operator switch
private string MapNumberCondition() => Operator switch
{
"equals" => Field + "=" + Value,
"notEqual" => Field + "!=" + Value,
"lessThan" => Field + "<" + Value,
"lessThanOrEqual" => Field + "<=" + Value,
"greaterThan" => Field + ">" + Value,
"greaterThanOrEqual" => Field + ">=" + Value,
"blank" => Field + "==null",
"notBlank" => Field + "!=null",
"equals" => $"{Field} = {Value}",
"notEqual" => $"{Field} != {Value}",
"lessThan" => $"{Field} < {Value}",
"lessThanOrEqual" => $"{Field} <= {Value}",
"greaterThan" => $"{Field} > {Value}",
"greaterThanOrEqual" => $"{Field} >= {Value}",
"blank" => $"{Field} == null",
"notBlank" => $"{Field} != null",
_ => "",
};
private string mapTextCondition() => Operator switch

private string MapTextCondition() => Operator switch
{
"equals" => Field + "=\"" + Value + "\"",
"notEqual" => Field + "!=\"" + Value + "\"",
"contains" => Field + ".Contains(\"" + Value + "\")",
"contains_i" => Field + ".ToLower().Contains(\"" + Value.ToLower() + "\")",
"notContains" => "!" + Field + ".Contains(\"" + Value + "\")",
"notContains_i" => "!" + Field + ".ToLower().Contains(\"" + Value.ToLower() + "\")",
"startsWith" => Field + ".StartsWith(\"" + Value + "\")",
"endsWith" => Field + ".EndsWith(\"" + Value + "\")",
"blank" => "string.IsNullOrEmpty(" + Field + ")",
"notBlank" => "!string.IsNullOrEmpty(" + Field + ")",
"equals" => $"{Field} = {Value}",
"notEqual" => $"{Field} != {Value}",
"contains" => $"{Field}.Contains({Value})",
"contains_i" => $"{Field}.ToLower().Contains({Value.ToLower()})",
"notContains" => $"!{Field}.Contains({Value})",
"notContains_i" => $"!{Field}.ToLower().Contains({Value.ToLower()})",
"startsWith" => $"{Field}.StartsWith({Value})",
"endsWith" => $"{Field}.EndsWith({Value})",
"blank" => $"string.IsNullOrEmpty({Field})",
"notBlank" => $"!string.IsNullOrEmpty({Field})",
_ => "",
};
private string mapGuidsCondition() => Operator switch

private string MapGuidsCondition() => Operator switch
{
"in" => Field + ".Any(x => x.Guid ==\"" + Value + "\")",
"notIn" => "!" + Field + ".Any(x => x.Guid == \"" + Value + "\")",
_ => ""
"in" => $"{Field}.Any(x => x.Guid == {Value})",
"notIn" => $"!{Field}.Any(x => x.Guid == {Value})",
_ => "",
};
private string mapObjectCondition() => Operator switch

private string MapObjectCondition() => Operator switch
{
"blank" => "" + Field + "==null",
"notBlank" => "" + Field + "!=null",
_ => ""
"blank" => $"{Field} == null",
"notBlank" => $"{Field} != null",
_ => "",
};
}
}

0 comments on commit f9f1919

Please sign in to comment.