cs11dotnet7/vscode/PracticalApps/Northwind.BlazorWasm/Client/Data/NorthwindService.cs
2022-03-13 16:17:01 +00:00

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}");
}
}