using BlazorApp.Client.Interfaces; using BlazorApp.Shared.Models; using BlazorApp.Shared.Models.Pagination; using BlazorApp.Shared.Queries; using System.Net.Http.Json; namespace BlazorApp.Client.Proxies { public class CustomerProxy : ICustomerProxy { private readonly IHttpClientFactory _httpClientFactory; public CustomerProxy(IHttpClientFactory httpClientFactory) { this._httpClientFactory = httpClientFactory; } public async Task> Query(int pageIndex = 0, int resultsPerPage = 10) { PaginatedResult models = new PaginatedResult(); var client = this._httpClientFactory.CreateClient("BlazorAppAPI"); var query = new CustomerQuery { PageIndex = pageIndex, ResultsPerPage = resultsPerPage }; var response = await client.PostAsJsonAsync("customer/query", query); if (response.IsSuccessStatusCode) { models = await response.Content.ReadFromJsonAsync>(); } return models; } public async Task Save(Customer model) { var client = this._httpClientFactory.CreateClient("BlazorAppAPI"); var response = await client.PostAsJsonAsync("customer/save", model); return response.IsSuccessStatusCode; } public async Task Update(Customer model) { var client = this._httpClientFactory.CreateClient("BlazorAppAPI"); var response = await client.PatchAsJsonAsync("customer/update", model); return response.IsSuccessStatusCode; } public async Task Delete(string id) { var client = this._httpClientFactory.CreateClient("BlazorAppAPI"); var response = await client.DeleteAsync($"customer/{id}"); return response.IsSuccessStatusCode; } public async Task Get(string id) { var client = this._httpClientFactory.CreateClient("BlazorAppAPI"); var response = await client.GetAsync($"customer/{id}"); if (response.IsSuccessStatusCode) { return await response.Content.ReadFromJsonAsync(); } else { throw new Exception($"Customer with id: {id} not found"); } } } }