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.Contracts.MassData;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
|
||||
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)
|
||||
{
|
||||
var query = new List<string>();
|
||||
if (skip.HasValue)
|
||||
{
|
||||
query.Add($"skip={skip.Value}");
|
||||
}
|
||||
if (take.HasValue)
|
||||
{
|
||||
query.Add($"take={take.Value}");
|
||||
}
|
||||
var query = new Dictionary<string, string?>();
|
||||
if (skip.HasValue) query["skip"] = skip.Value.ToString();
|
||||
if (take.HasValue) query["take"] = take.Value.ToString();
|
||||
|
||||
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);
|
||||
return result ?? [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user