Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Postgrest/Interfaces/IPostgrestTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ IPostgrestTable<TModel> Filter<TCriterion>(Expression<Func<TModel, object>> pred
/// Executes the query using the defined filters on the current instance.
/// </summary>
/// <param name="cancellationToken"></param>
/// <param name="countType"></param>
/// <returns></returns>
Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default);
Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default, Constants.CountType countType = Constants.CountType.Estimated);

/// <summary>
/// Executes a BULK INSERT query using the defined query params on the current instance.
Expand Down
17 changes: 17 additions & 0 deletions Postgrest/Responses/ModeledResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ namespace Supabase.Postgrest.Responses
/// A list of models in the response.
/// </summary>
public List<T> Models { get; } = new();

/// <summary>
/// The number of results matching the specified filters
/// </summary>
public int Count = 0;

/// <inheritdoc />
public ModeledResponse(BaseResponse baseResponse, JsonSerializerSettings serializerSettings, Func<Dictionary<string, string>>? getHeaders = null, bool shouldParse = true) : base(baseResponse.ClientOptions, baseResponse.ResponseMessage, baseResponse.Content)
Expand Down Expand Up @@ -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");
}
}
Expand Down
12 changes: 10 additions & 2 deletions Postgrest/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,18 @@ public async Task<int> Count(CountType type, CancellationToken cancellationToken
}

/// <inheritdoc />
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default)
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default, CountType type = CountType.Estimated)
{
var request = Send<TModel>(_method, null, null, cancellationToken);
var attr = type.GetAttribute<MapToAttribute>();

var headers = new Dictionary<string, string>
{
{ "Prefer", $"count={attr?.Mapping}" }
};

var request = Send<TModel>(_method, null, headers, cancellationToken);
Clear();

return request;
}

Expand Down
18 changes: 18 additions & 0 deletions PostgrestTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>().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<User>().Filter("status", Operator.Equals, "ONLINE").Get(default, CountType.Exact);
Assert.IsTrue(resp.Count > -1);
}

[TestMethod("support: int arrays")]
public async Task TestSupportIntArraysAsLists()
{
Expand Down
Loading