Skip to content

Commit e708b36

Browse files
authored
Added count to ModeledResponse (#103)
* Added GetWithCount method * Made improvements * Changed existing get method to retrieve count, added count to modeledresponse * Fixed issue when making non get requests * Added tests
1 parent d812544 commit e708b36

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

Postgrest/Interfaces/IPostgrestTable.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ IPostgrestTable<TModel> Filter<TCriterion>(Expression<Func<TModel, object>> pred
116116
/// Executes the query using the defined filters on the current instance.
117117
/// </summary>
118118
/// <param name="cancellationToken"></param>
119+
/// <param name="countType"></param>
119120
/// <returns></returns>
120-
Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default);
121+
Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default, Constants.CountType countType = Constants.CountType.Estimated);
121122

122123
/// <summary>
123124
/// Executes a BULK INSERT query using the defined query params on the current instance.

Postgrest/Responses/ModeledResponse.cs

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ namespace Supabase.Postgrest.Responses
2424
/// A list of models in the response.
2525
/// </summary>
2626
public List<T> Models { get; } = new();
27+
28+
/// <summary>
29+
/// The number of results matching the specified filters
30+
/// </summary>
31+
public int Count = 0;
2732

2833
/// <inheritdoc />
2934
public ModeledResponse(BaseResponse baseResponse, JsonSerializerSettings serializerSettings, Func<Dictionary<string, string>>? getHeaders = null, bool shouldParse = true) : base(baseResponse.ClientOptions, baseResponse.ResponseMessage, baseResponse.Content)
@@ -72,6 +77,18 @@ public ModeledResponse(BaseResponse baseResponse, JsonSerializerSettings seriali
7277
}
7378
}
7479

80+
try
81+
{
82+
var countStr = baseResponse.ResponseMessage?.Content.Headers.GetValues("Content-Range")
83+
.FirstOrDefault();
84+
Count = int.Parse(countStr?.Split('/')[1] ?? throw new InvalidOperationException());
85+
}
86+
catch (Exception e)
87+
{
88+
Debugger.Instance.Log(this, e.Message);
89+
Count = -1;
90+
}
91+
7592
Debugger.Instance.Log(this, $"Response: [{baseResponse.ResponseMessage?.StatusCode}]\n" + $"Parsed Models <{typeof(T).Name}>:\n\t{JsonConvert.SerializeObject(Models)}\n");
7693
}
7794
}

Postgrest/Table.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,18 @@ public async Task<int> Count(CountType type, CancellationToken cancellationToken
628628
}
629629

630630
/// <inheritdoc />
631-
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default)
631+
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default, CountType type = CountType.Estimated)
632632
{
633-
var request = Send<TModel>(_method, null, null, cancellationToken);
633+
var attr = type.GetAttribute<MapToAttribute>();
634+
635+
var headers = new Dictionary<string, string>
636+
{
637+
{ "Prefer", $"count={attr?.Mapping}" }
638+
};
639+
640+
var request = Send<TModel>(_method, null, headers, cancellationToken);
634641
Clear();
642+
635643
return request;
636644
}
637645

PostgrestTests/ClientTests.cs

+18
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,24 @@ public async Task TestCountWithFilter()
10411041
Assert.IsNotNull(resp);
10421042
}
10431043

1044+
[TestMethod("response count")]
1045+
public async Task TestCountInResponse()
1046+
{
1047+
var client = new Client(BaseUrl);
1048+
1049+
var resp = await client.Table<User>().Get(default, CountType.Exact);
1050+
Assert.IsTrue(resp.Count > -1);
1051+
}
1052+
1053+
[TestMethod("response count: with filter")]
1054+
public async Task TestCountInResponseWithFilter()
1055+
{
1056+
var client = new Client(BaseUrl);
1057+
1058+
var resp = await client.Table<User>().Filter("status", Operator.Equals, "ONLINE").Get(default, CountType.Exact);
1059+
Assert.IsTrue(resp.Count > -1);
1060+
}
1061+
10441062
[TestMethod("support: int arrays")]
10451063
public async Task TestSupportIntArraysAsLists()
10461064
{

0 commit comments

Comments
 (0)