Skip to content

Commit abbfc73

Browse files
committed
count query && usage
1 parent 438adb9 commit abbfc73

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

WeatherControl/Wissance.WeatherControl.WebApi.V2/Helpers/EqlResolver.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace Wissance.WeatherControl.WebApi.V2.Helpers
77
{
88
public class EqlResolver
99
{
10+
public string GetQueryToCountItems(ModelType model)
11+
{
12+
return String.Format(_selectCountQueries[model]);
13+
}
1014
public string GetQueryToFetchManyItems(ModelType model, int offset, int limit)
1115
{
1216
if (!_selectManyWithLimitsQueries.ContainsKey(model))
@@ -42,6 +46,14 @@ public string GetQueryToDeleteItem(ModelType model)
4246
return String.Format(_deleteQuery[model]);
4347
}
4448

49+
private readonly IDictionary<ModelType, string> _selectCountQueries = new Dictionary<ModelType, string>()
50+
{
51+
{ModelType.MeasureUnit, "SELECT COUNT (SELECT MeasureUnit {{id}})"},
52+
{ModelType.Measurement, "SELECT COUNT (SELECT Measurement {{id}})"},
53+
{ModelType.Sensor , "SELECT COUNT (SELECT Sensor {{id}})"},
54+
{ModelType.MeteoStation, "SELECT COUNT (SELECT MeteoStation {{id}})" }
55+
};
56+
4557
private readonly IDictionary<ModelType, string> _selectManyWithLimitsQueries = new Dictionary<ModelType, string>()
4658
{
4759
{ModelType.MeasureUnit, "SELECT MeasureUnit {{id, Name, Abbreviation, Description}} OFFSET {0} LIMIT {1}"},

WeatherControl/Wissance.WeatherControl.WebApi.V2/Managers/EdgeDbManager.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,18 @@ public async Task<OperationResultDto<Tuple<IList<TRes>,long>>> GetAsync(int page
107107
{
108108
try
109109
{
110-
// todo: mode to dictionary ...
111-
string query = _resolver.GetQueryToFetchManyItems(_model, (page - 1) * size, size);
112-
if (query == null)
113-
throw new NotSupportedException($"EQL queries for model {_model} are not ready");
114-
IReadOnlyCollection<TObj> items = await _edgeDbClient.QueryAsync<TObj>(query);
110+
string countQuery = _resolver.GetQueryToCountItems(_model);
111+
if (countQuery == null)
112+
throw new NotSupportedException($"EQL count query for model {_model} is not ready");
113+
long totalItems = await _edgeDbClient.QuerySingleAsync<long>(countQuery);
114+
// todo: modve to dictionary ...
115+
string getQuery = _resolver.GetQueryToFetchManyItems(_model, (page - 1) * size, size);
116+
if (getQuery == null)
117+
throw new NotSupportedException($"EQL get query for model {_model} is not ready");
118+
IReadOnlyCollection<TObj> items = await _edgeDbClient.QueryAsync<TObj>(getQuery);
115119
IList<TRes> dtoItems = items.Select(i => _factory(i)).ToList();
116120
return new OperationResultDto<Tuple<IList<TRes>, long>>(true, (int)HttpStatusCode.OK, String.Empty,
117-
new Tuple<IList<TRes>, long>(dtoItems, 0));
121+
new Tuple<IList<TRes>, long>(dtoItems, totalItems));
118122
}
119123
catch (Exception e)
120124
{

0 commit comments

Comments
 (0)