ShopOnlineSolution/ShopOnline.Web/Services/ProductService.cs

70 lines
2.1 KiB
C#

using ShopOnline.Models.Dtos;
using ShopOnline.Web.Services.Contracts;
using System.Net.Http.Json;
namespace ShopOnline.Web.Services
{
public class ProductService : IProductService
{
private readonly HttpClient httpClient;
public ProductService(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public async Task<ProductDto> GetItem(int id)
{
try
{
var response = await httpClient.GetAsync($"api/Product/{id}");
if(response.IsSuccessStatusCode)
{
if(response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
return default(ProductDto);
}
return await response.Content.ReadFromJsonAsync<ProductDto>();
}
else
{
var message = await response.Content.ReadAsStringAsync();
throw new Exception(message);
}
}
catch (Exception)
{
throw;
}
}
public async Task<IEnumerable<ProductDto>> GetItems()
{
try
{
var response = await this.httpClient.GetAsync("api/Product");
//var products = await this.httpClient.GetFromJsonAsync<IEnumerable<ProductDto>>("api/Product");
//return products;
if (response.IsSuccessStatusCode)
{
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
return Enumerable.Empty<ProductDto>();
}
return await response.Content.ReadFromJsonAsync<IEnumerable<ProductDto>>();
}
else
{
var message = await response.Content.ReadAsStringAsync();
throw new Exception(message);
}
}
catch(Exception)
{
throw;
}
}
}
}