Refactor query string construction in GetAllAsync
Refactored the GetAllAsync method to use a Dictionary and QueryHelpers.AddQueryString for building query strings, replacing manual string concatenation. This improves code clarity and reduces the risk of formatting errors.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using DbFirst.BlazorWebApp.Models;
|
using DbFirst.BlazorWebApp.Models;
|
||||||
using DbFirst.Contracts.MassData;
|
using DbFirst.Contracts.MassData;
|
||||||
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
|
|
||||||
namespace DbFirst.BlazorWebApp.Services;
|
namespace DbFirst.BlazorWebApp.Services;
|
||||||
|
|
||||||
@@ -21,17 +22,11 @@ public class MassDataApiClient : IMassDataApiClient
|
|||||||
|
|
||||||
public async Task<List<MassDataReadDto>> GetAllAsync(int? skip, int? take, CancellationToken ct = default)
|
public async Task<List<MassDataReadDto>> GetAllAsync(int? skip, int? take, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var query = new List<string>();
|
var query = new Dictionary<string, string?>();
|
||||||
if (skip.HasValue)
|
if (skip.HasValue) query["skip"] = skip.Value.ToString();
|
||||||
{
|
if (take.HasValue) query["take"] = take.Value.ToString();
|
||||||
query.Add($"skip={skip.Value}");
|
|
||||||
}
|
|
||||||
if (take.HasValue)
|
|
||||||
{
|
|
||||||
query.Add($"take={take.Value}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var url = query.Count == 0 ? Endpoint : $"{Endpoint}?{string.Join("&", query)}";
|
var url = QueryHelpers.AddQueryString(Endpoint, query);
|
||||||
var result = await _httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url, ct);
|
var result = await _httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url, ct);
|
||||||
return result ?? [];
|
return result ?? [];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user