124 lines
4.0 KiB
C#
124 lines
4.0 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;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<ProductDto>> GetItemsByCategory(int categoryId)
|
|
{
|
|
try
|
|
{
|
|
var response = await httpClient.GetAsync($"api/Product/{categoryId}/GetItemsByCategory");
|
|
|
|
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($"Http Status Code - {response.StatusCode} Message - {message}");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<ProductCategoryDto>> GetProductCategories()
|
|
{
|
|
try
|
|
{
|
|
var response = await httpClient.GetAsync("api/Product/GetProductCategories");
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
if(response.StatusCode == System.Net.HttpStatusCode.NoContent)
|
|
{
|
|
return Enumerable.Empty<ProductCategoryDto>();
|
|
}
|
|
return await response.Content.ReadFromJsonAsync<IEnumerable<ProductCategoryDto>>();
|
|
}
|
|
else
|
|
{
|
|
var message = await response.Content.ReadAsStringAsync();
|
|
throw new Exception($"Http Status Code - {response.StatusCode} Message - {message}");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|