mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 13:57:37 +00:00
Initial commit
This commit is contained in:
parent
1d9d051759
commit
9656378279
557 changed files with 182300 additions and 0 deletions
|
|
@ -0,0 +1,11 @@
|
|||
namespace Packt.Shared;
|
||||
|
||||
public interface INorthwindService
|
||||
{
|
||||
Task<List<Customer>> GetCustomersAsync();
|
||||
Task<List<Customer>> GetCustomersAsync(string country);
|
||||
Task<Customer?> GetCustomerAsync(string id);
|
||||
Task<Customer> CreateCustomerAsync(Customer c);
|
||||
Task<Customer> UpdateCustomerAsync(Customer c);
|
||||
Task DeleteCustomerAsync(string id);
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
using System.Net.Http.Json; // GetFromJsonAsync, ReadFromJsonAsync
|
||||
using Packt.Shared; // Customer
|
||||
|
||||
namespace Northwind.BlazorWasm.Client.Data;
|
||||
|
||||
public class NorthwindService : INorthwindService
|
||||
{
|
||||
private readonly HttpClient http;
|
||||
|
||||
public NorthwindService(HttpClient http)
|
||||
{
|
||||
this.http = http;
|
||||
}
|
||||
|
||||
public Task<List<Customer>> GetCustomersAsync()
|
||||
{
|
||||
return http.GetFromJsonAsync
|
||||
<List<Customer>>("api/customers")!;
|
||||
}
|
||||
|
||||
public Task<List<Customer>> GetCustomersAsync(string country)
|
||||
{
|
||||
return http.GetFromJsonAsync
|
||||
<List<Customer>>($"api/customers/in/{country}")!;
|
||||
}
|
||||
|
||||
public Task<Customer?> GetCustomerAsync(string id)
|
||||
{
|
||||
return http.GetFromJsonAsync
|
||||
<Customer>($"api/customers/{id}");
|
||||
}
|
||||
|
||||
public async Task<Customer>
|
||||
CreateCustomerAsync(Customer c)
|
||||
{
|
||||
HttpResponseMessage response = await
|
||||
http.PostAsJsonAsync("api/customers", c);
|
||||
|
||||
return (await response.Content
|
||||
.ReadFromJsonAsync<Customer>())!;
|
||||
}
|
||||
|
||||
public async Task<Customer> UpdateCustomerAsync(Customer c)
|
||||
{
|
||||
HttpResponseMessage response = await
|
||||
http.PutAsJsonAsync("api/customers", c);
|
||||
|
||||
return (await response.Content
|
||||
.ReadFromJsonAsync<Customer>())!;
|
||||
}
|
||||
|
||||
public async Task DeleteCustomerAsync(string id)
|
||||
{
|
||||
HttpResponseMessage response = await
|
||||
http.DeleteAsync($"api/customers/{id}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue