Skip to content

Commit 8c349fe

Browse files
committed
Changed existing get method to retrieve count, added count to modeledresponse
1 parent 13a7647 commit 8c349fe

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

Postgrest/Interfaces/IPostgrestTable.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +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-
122-
/// <summary>
123-
/// Executes the query using the defined filters on the current instance, along with count from the specified query.
124-
/// </summary>
125-
/// <param name="type">The kind of count.</param>
126-
/// <param name="cancellationToken"></param>
127-
/// <returns></returns>
128-
Task<(ModeledResponse<TModel> Result, int Count)> GetWithCount(Constants.CountType type,
129-
CancellationToken cancellationToken = default);
121+
Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default, Constants.CountType countType = Constants.CountType.Estimated);
130122

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

Postgrest/Responses/ModeledResponse.cs

Lines changed: 8 additions & 0 deletions
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)
@@ -71,6 +76,9 @@ public ModeledResponse(BaseResponse baseResponse, JsonSerializerSettings seriali
7176
break;
7277
}
7378
}
79+
80+
var countStr = baseResponse.ResponseMessage?.Content.Headers.GetValues("Content-Range").FirstOrDefault();
81+
Count = int.Parse(countStr?.Split('/')[1] ?? throw new InvalidOperationException());
7482

7583
Debugger.Instance.Log(this, $"Response: [{baseResponse.ResponseMessage?.StatusCode}]\n" + $"Parsed Models <{typeof(T).Name}>:\n\t{JsonConvert.SerializeObject(Models)}\n");
7684
}

Postgrest/Table.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,7 @@ public async Task<int> Count(CountType type, CancellationToken cancellationToken
628628
}
629629

630630
/// <inheritdoc />
631-
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default)
632-
{
633-
var request = Send<TModel>(_method, null, null, cancellationToken);
634-
Clear();
635-
return request;
636-
}
637-
638-
/// <inheritdoc />
639-
public async Task<(ModeledResponse<TModel> Result, int Count)>GetWithCount(CountType type, CancellationToken cancellationToken = default)
631+
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default, CountType type = CountType.Estimated)
640632
{
641633
var attr = type.GetAttribute<MapToAttribute>();
642634

@@ -648,11 +640,7 @@ public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = d
648640
var request = Send<TModel>(_method, null, headers, cancellationToken);
649641
Clear();
650642

651-
var response = await request;
652-
var countStr = response.ResponseMessage?.Content.Headers.GetValues("Content-Range").FirstOrDefault();
653-
var count = int.Parse(countStr?.Split('/')[1] ?? throw new InvalidOperationException());
654-
655-
return (response, count);
643+
return request;
656644
}
657645

658646
/// <summary>

0 commit comments

Comments
 (0)