Skip to content

Commit

Permalink
Support long, DateTime, and DateTimeOffset criteria in filter express…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
sbarnes-ellenbytech committed Nov 4, 2024
1 parent 3e234f5 commit a6462e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Postgrest/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Operator op
case int intCriterion:
_filters.Add(new QueryFilter(columnName, op, intCriterion));
return this;
case long longCriterion:
_filters.Add(new QueryFilter(columnName, op, longCriterion));
return this;
case float floatCriterion:
_filters.Add(new QueryFilter(columnName, op, floatCriterion));
return this;
Expand All @@ -159,9 +162,15 @@ public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Operator op
case FullTextSearchConfig fullTextSearchCriteria:
_filters.Add(new QueryFilter(columnName, op, fullTextSearchCriteria));
return this;
case DateTime dtSearchCriteria:
_filters.Add(new QueryFilter(columnName, op, dtSearchCriteria));
return this;
case DateTimeOffset dtoSearchCriteria:
_filters.Add(new QueryFilter(columnName, op, dtoSearchCriteria));
return this;
default:
throw new PostgrestException(
"Unknown criterion type, is it of type `string`, `int`, `float`, `List`, `Dictionary<string, object>`, `FullTextSearchConfig`, or `Range`?")
"Unknown criterion type, is it of type `string`, `int`, `long`, `float`, `List`, `DateTime`, `DateTimeOffset`, `Dictionary<string, object>`, `FullTextSearchConfig`, or `Range`?")
{
Reason = FailureHint.Reason.InvalidArgument
};
Expand Down
31 changes: 31 additions & 0 deletions PostgrestTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,37 @@ public async Task TestMatchFilter()
CollectionAssert.AreEqual(expected, filteredResponse.Models);
}

[TestMethod("filters: dt")]
public async Task TestDateTimeFilter()
{
var client = new Client(BaseUrl);
var filteredResponse = await client.Table<Movie>().Filter("created_at", Operator.GreaterThan, new DateTime(2022, 08, 20))
.Filter("created_at", Operator.LessThan, new DateTime(2022, 08, 21))
.Get();
Assert.AreEqual(1, filteredResponse.Models.Count);
Assert.AreEqual("ea07bd86-a507-4c68-9545-b848bfe74c90", filteredResponse.Models[0].Id);
}

[TestMethod("filters: dto")]
public async Task TestDateTimeOffsetFilter()
{
var client = new Client(BaseUrl);
var filteredResponse = await client.Table<Movie>().Filter("created_at", Operator.GreaterThan, new DateTimeOffset(new DateTime(2022, 08, 20)))
.Filter("created_at", Operator.LessThan, new DateTimeOffset(new DateTime(2022, 08, 21)))
.Get();
Assert.AreEqual(1, filteredResponse.Models.Count);
Assert.AreEqual("ea07bd86-a507-4c68-9545-b848bfe74c90", filteredResponse.Models[0].Id);
}

[TestMethod("filters: long")]
public async Task TestLongIntFilter()
{
var client = new Client(BaseUrl);
var filteredResponse = await client.Table<KitchenSink>().Filter("long_value", Operator.Equals, 2147483648L)
.Get();
Assert.AreEqual(1, filteredResponse.Models.Count);
}

[TestMethod("select: basic")]
public async Task TestSelect()
{
Expand Down

0 comments on commit a6462e4

Please sign in to comment.