42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BlazingPizza.Client
|
|
{
|
|
public class OrdersClient
|
|
{
|
|
private readonly HttpClient httpClient;
|
|
|
|
public OrdersClient(HttpClient httpClient)
|
|
{
|
|
this.httpClient = httpClient;
|
|
}
|
|
|
|
public async Task<IEnumerable<OrderWithStatus>> GetOrders() =>
|
|
await httpClient.GetFromJsonAsync<IEnumerable<OrderWithStatus>>("orders");
|
|
|
|
|
|
public async Task<OrderWithStatus> GetOrder(int orderId) =>
|
|
await httpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{orderId}");
|
|
|
|
|
|
public async Task<int> PlaceOrder(Order order)
|
|
{
|
|
var response = await httpClient.PostAsJsonAsync("orders", order);
|
|
response.EnsureSuccessStatusCode();
|
|
var orderId = await response.Content.ReadFromJsonAsync<int>();
|
|
return orderId;
|
|
}
|
|
|
|
public async Task SubscribeToNotifications(NotificationSubscription subscription)
|
|
{
|
|
var response = await httpClient.PutAsJsonAsync("notifications/subscribe", subscription);
|
|
response.EnsureSuccessStatusCode();
|
|
}
|
|
}
|
|
}
|