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 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(); } else { var message = await response.Content.ReadAsStringAsync(); throw new Exception(message); } } catch (Exception) { throw; } } public async Task> GetItems() { try { var response = await this.httpClient.GetAsync("api/Product"); //var products = await this.httpClient.GetFromJsonAsync>("api/Product"); //return products; if (response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NoContent) { return Enumerable.Empty(); } return await response.Content.ReadFromJsonAsync>(); } else { var message = await response.Content.ReadAsStringAsync(); throw new Exception(message); } } catch(Exception) { throw; } } } }