Skip to content

Commit

Permalink
refactor: simplify page interval parsing in RepositoryQueryTests
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderbird committed Nov 4, 2024
1 parent 8cf51d5 commit 2874398
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/SaveEnergy.Tests/Adapters/Outbound/RepositoryQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,27 @@ CancellationToken cancellationToken
var queryString = request.RequestUri?.Query[1..] ?? "";
var queryParameters = queryString.Split(separator);

const string pageParameter = "page=";
var page = int.Parse(
Array
.Find(
queryParameters,
x => x.StartsWith(pageParameter, StringComparison.OrdinalIgnoreCase)
)
?.Split('=')[1] ?? "1"
);
var page = ParseQueryParameter("page=", queryParameters);
var perPage = ParseQueryParameter("per_page=", queryParameters);

var start = (page - 1) * perPage + 1;
var count = Math.Min(perPage, availableRepositories - start + 1);
count = Math.Max(count, 0);

const string perPageParameter = "per_page=";
var perPage = int.Parse(
return (start, count);
}

private static int ParseQueryParameter(string parameterName, string[] queryParameters)
{
var page = int.Parse(
Array
.Find(
queryParameters,
x => x.StartsWith(perPageParameter, StringComparison.OrdinalIgnoreCase)
x => x.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase)
)
?.Split('=')[1] ?? "1"
);

var start = (page - 1) * perPage + 1;
var count = Math.Min(perPage, availableRepositories - start + 1);
count = Math.Max(count, 0);

return (start, count);
return page;
}
}
}
Expand Down

0 comments on commit 2874398

Please sign in to comment.