74 lines
2.5 KiB
C#
74 lines
2.5 KiB
C#
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<PaginatedResult<Customer>> Query(int pageIndex = 0, int resultsPerPage = 10)
|
|
{
|
|
PaginatedResult<Customer> models = new PaginatedResult<Customer>();
|
|
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<PaginatedResult<Customer>>();
|
|
}
|
|
|
|
return models;
|
|
}
|
|
|
|
public async Task<bool> Save(Customer model)
|
|
{
|
|
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
|
|
var response = await client.PostAsJsonAsync("customer/save", model);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> Update(Customer model)
|
|
{
|
|
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
|
|
var response = await client.PatchAsJsonAsync("customer/update", model);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> Delete(string id)
|
|
{
|
|
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
|
|
var response = await client.DeleteAsync($"customer/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<Customer> Get(string id)
|
|
{
|
|
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
|
|
var response = await client.GetAsync($"customer/{id}");
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return await response.Content.ReadFromJsonAsync<Customer>();
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Customer with id: {id} not found");
|
|
}
|
|
}
|
|
}
|
|
}
|