diff --git a/Postgrest/Interfaces/IPostgrestTable.cs b/Postgrest/Interfaces/IPostgrestTable.cs index ca06d5d..eb480ef 100644 --- a/Postgrest/Interfaces/IPostgrestTable.cs +++ b/Postgrest/Interfaces/IPostgrestTable.cs @@ -116,8 +116,9 @@ IPostgrestTable Filter(Expression> pred /// Executes the query using the defined filters on the current instance. /// /// + /// /// - Task> Get(CancellationToken cancellationToken = default); + Task> Get(CancellationToken cancellationToken = default, Constants.CountType countType = Constants.CountType.Estimated); /// /// Executes a BULK INSERT query using the defined query params on the current instance. diff --git a/Postgrest/Responses/ModeledResponse.cs b/Postgrest/Responses/ModeledResponse.cs index 7a79742..02644f6 100644 --- a/Postgrest/Responses/ModeledResponse.cs +++ b/Postgrest/Responses/ModeledResponse.cs @@ -24,6 +24,11 @@ namespace Supabase.Postgrest.Responses /// A list of models in the response. /// public List Models { get; } = new(); + + /// + /// The number of results matching the specified filters + /// + public int Count = 0; /// public ModeledResponse(BaseResponse baseResponse, JsonSerializerSettings serializerSettings, Func>? getHeaders = null, bool shouldParse = true) : base(baseResponse.ClientOptions, baseResponse.ResponseMessage, baseResponse.Content) @@ -72,6 +77,18 @@ public ModeledResponse(BaseResponse baseResponse, JsonSerializerSettings seriali } } + try + { + var countStr = baseResponse.ResponseMessage?.Content.Headers.GetValues("Content-Range") + .FirstOrDefault(); + Count = int.Parse(countStr?.Split('/')[1] ?? throw new InvalidOperationException()); + } + catch (Exception e) + { + Debugger.Instance.Log(this, e.Message); + Count = -1; + } + Debugger.Instance.Log(this, $"Response: [{baseResponse.ResponseMessage?.StatusCode}]\n" + $"Parsed Models <{typeof(T).Name}>:\n\t{JsonConvert.SerializeObject(Models)}\n"); } } diff --git a/Postgrest/Table.cs b/Postgrest/Table.cs index 58b3f8c..47c2732 100644 --- a/Postgrest/Table.cs +++ b/Postgrest/Table.cs @@ -628,10 +628,18 @@ public async Task Count(CountType type, CancellationToken cancellationToken } /// - public Task> Get(CancellationToken cancellationToken = default) + public Task> Get(CancellationToken cancellationToken = default, CountType type = CountType.Estimated) { - var request = Send(_method, null, null, cancellationToken); + var attr = type.GetAttribute(); + + var headers = new Dictionary + { + { "Prefer", $"count={attr?.Mapping}" } + }; + + var request = Send(_method, null, headers, cancellationToken); Clear(); + return request; } diff --git a/PostgrestTests/ClientTests.cs b/PostgrestTests/ClientTests.cs index 47bd4c6..b3541b3 100644 --- a/PostgrestTests/ClientTests.cs +++ b/PostgrestTests/ClientTests.cs @@ -1041,6 +1041,24 @@ public async Task TestCountWithFilter() Assert.IsNotNull(resp); } + [TestMethod("response count")] + public async Task TestCountInResponse() + { + var client = new Client(BaseUrl); + + var resp = await client.Table().Get(default, CountType.Exact); + Assert.IsTrue(resp.Count > -1); + } + + [TestMethod("response count: with filter")] + public async Task TestCountInResponseWithFilter() + { + var client = new Client(BaseUrl); + + var resp = await client.Table().Filter("status", Operator.Equals, "ONLINE").Get(default, CountType.Exact); + Assert.IsTrue(resp.Count > -1); + } + [TestMethod("support: int arrays")] public async Task TestSupportIntArraysAsLists() {