45 lines
1.0 KiB
C#
45 lines
1.0 KiB
C#
namespace YPS.Beer.Services;
|
|
|
|
public class PunkService : IPunkService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public PunkService(HttpClient httpClient)
|
|
{
|
|
_httpClient = httpClient;
|
|
}
|
|
|
|
public async Task<Models.Beer?> GetBeer(int id)
|
|
{
|
|
var beer = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"beers/{id}");
|
|
|
|
return beer?.SingleOrDefault();
|
|
}
|
|
|
|
public async Task<IEnumerable<Models.Beer>> GetBeers(params int[] ids)
|
|
{
|
|
var beers = new List<Models.Beer>();
|
|
|
|
foreach (var id in ids)
|
|
{
|
|
var beer = await GetBeer(id);
|
|
|
|
if (beer is not null)
|
|
beers.Add(beer);
|
|
}
|
|
|
|
return beers;
|
|
}
|
|
|
|
public async Task<IEnumerable<Models.Beer>> FindBeers(string search)
|
|
{
|
|
search = search.Replace(' ', '_');
|
|
|
|
var beers = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"beers?beer_name={search}");
|
|
|
|
if (beers is null)
|
|
throw new Exception();
|
|
|
|
return beers;
|
|
}
|
|
} |