mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
58 lines
1.3 KiB
C#
58 lines
1.3 KiB
C#
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}");
|
|
}
|
|
}
|