Initial commit

This commit is contained in:
Mark J Price 2022-03-13 16:17:01 +00:00
parent 1d9d051759
commit 9656378279
557 changed files with 182300 additions and 0 deletions

View file

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

View file

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