YPS-Beer/backend/YPS.Beer/Services/PunkService.cs

45 lines
1.0 KiB
C#
Raw Normal View History

2023-12-07 00:20:59 +00:00
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)
{
2023-12-07 00:29:11 +00:00
var beer = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"beers/{id}");
2023-12-07 00:20:59 +00:00
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(' ', '_');
2023-12-07 00:29:11 +00:00
var beers = await _httpClient.GetFromJsonAsync<Models.Beer[]>($"beers?beer_name={search}");
2023-12-07 00:20:59 +00:00
if (beers is null)
throw new Exception();
return beers;
}
}