Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable #365

Merged
merged 53 commits into from
Nov 29, 2023
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ee2273e
Shared lib updated with Nullable context. Partially fixes issue #105.
EdCharbeneau Sep 6, 2023
d2669b4
Server updated with Nullable context. Partially fixes issue #105.
EdCharbeneau Sep 6, 2023
c8e742f
Razor projects updated with Nullable context. Partially fixes issue #…
EdCharbeneau Sep 6, 2023
b320741
01 updated with Nullable context, including instructions (md). Partia…
EdCharbeneau Sep 12, 2023
560bd5c
Updated 02. Dependencies to use Nullable
EdCharbeneau Sep 15, 2023
1e558ac
Updated code to include nullable considerations.
EdCharbeneau Sep 15, 2023
f9b6aa8
Updated instructions to align with nullable.
EdCharbeneau Sep 15, 2023
c240c05
Updated dependencies to use nullable context
EdCharbeneau Sep 15, 2023
1e5e283
Fixes issue #358 and Fixes issue #238
EdCharbeneau Sep 15, 2023
ad569ae
Sync with previous lesson
EdCharbeneau Sep 15, 2023
c4a0b11
Removed guard on GetUserId to allow workshop to flow without authoriz…
EdCharbeneau Sep 15, 2023
ecc0012
Sync changes across lessons
EdCharbeneau Sep 15, 2023
d106944
Updated 03 with nullability context
EdCharbeneau Sep 15, 2023
36184a4
Updated instructions with nullable code
EdCharbeneau Sep 15, 2023
e72e41b
Cleanup whitespace
EdCharbeneau Sep 15, 2023
38d08c1
Sync dependencies 04 with previous steps
EdCharbeneau Sep 15, 2023
6c77e8b
Fixed: Starting point 04's server code was actually from a later step…
EdCharbeneau Sep 15, 2023
8c85b72
Sync 04 client changes with previous steps
EdCharbeneau Sep 15, 2023
a13e07a
Updated client with nullable context enabled
EdCharbeneau Sep 15, 2023
f9ade8f
Sync dependencies 05 with previous changes
EdCharbeneau Sep 15, 2023
6fda4d3
Fixed step 05 controller is from step 06
EdCharbeneau Sep 15, 2023
acce7ac
Sync with client with previous step
EdCharbeneau Sep 15, 2023
2157016
Updated 05 with nullable context enabled
EdCharbeneau Sep 15, 2023
70052e7
Sync with previous steps
EdCharbeneau Sep 15, 2023
c7d2d48
Updated 06 with nullable context enabled.
EdCharbeneau Sep 15, 2023
1279c3f
Synced dependencies with previous lesson
EdCharbeneau Sep 18, 2023
dcd325d
Sync client changes with previous step
EdCharbeneau Sep 18, 2023
2773808
Updated 07 with nullable context enabled
EdCharbeneau Sep 18, 2023
2c76f8b
Synced dependencies with previous step
EdCharbeneau Sep 18, 2023
09cda9f
Synced client with previous lesson
EdCharbeneau Sep 18, 2023
038e53c
Updated 08 with nullable conetext enabled
EdCharbeneau Sep 18, 2023
c3ea048
Sync with prevous lesson
EdCharbeneau Sep 18, 2023
f3539c4
Updated 09 with nullable context enabled
EdCharbeneau Sep 18, 2023
9ea1c99
Updated src with nullable context enabled
EdCharbeneau Sep 19, 2023
def0f14
Updated docs
EdCharbeneau Sep 20, 2023
7747b65
Notification interop doesn't work with required.
EdCharbeneau Sep 20, 2023
7195eb2
Extra sln file
EdCharbeneau Sep 20, 2023
a2e5a47
Fixed link that moved for getting started
EdCharbeneau Sep 21, 2023
175f438
Removed extra comments.
EdCharbeneau Sep 21, 2023
c26f1af
Clairified the message about EditorRequired.
EdCharbeneau Sep 21, 2023
e7fb767
Trimmed whitespace
EdCharbeneau Sep 21, 2023
59125da
Supressed nullable warning on change event
EdCharbeneau Sep 21, 2023
5c1c94a
Added EditorRequired to step
EdCharbeneau Sep 21, 2023
6fe4eef
Refactored GetUserId. This code was duplicated in multiple places.
EdCharbeneau Sep 21, 2023
51ea18c
Updated `!=` to `is not` null
EdCharbeneau Sep 21, 2023
63efdf4
Fixes: SignoutSessionStateManager obsolete warning. See: https://lear…
EdCharbeneau Sep 21, 2023
b11af38
Added Nullable to orderWithStatus
EdCharbeneau Sep 21, 2023
f0bfc87
Updated Razor Class Library verbage, it was reminicent of .NET Core 3…
EdCharbeneau Sep 21, 2023
d551aed
Changed `!=` to `is not null`
EdCharbeneau Sep 21, 2023
474c164
The app was updated to include a Minimal API implementation of Notifi…
EdCharbeneau Sep 21, 2023
6fbd4cd
Pulling up some missed changes from the final exercise
EdCharbeneau Sep 21, 2023
d6a3d9e
Removed unused NavigationManager references
EdCharbeneau Sep 21, 2023
4a7346a
Updated code on Map component
EdCharbeneau Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored GetUserId. This code was duplicated in multiple places.
EdCharbeneau committed Sep 21, 2023
commit 6fe4eef05cabc086c13ea93c0db68ada99640698
68 changes: 37 additions & 31 deletions docs/06-authentication-and-authorization.md
Original file line number Diff line number Diff line change
@@ -232,40 +232,35 @@ To create the strongly typed client, add a new `OrdersClient` class to the clien
*BlazingPizza.Client/OrdersClient.cs*

```csharp
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
namespace BlazingPizza.Client;

public class OrdersClient
{
public class OrdersClient
{
private readonly HttpClient httpClient;
private readonly HttpClient httpClient;

public OrdersClient(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public OrdersClient(HttpClient httpClient)
{
this.httpClient = httpClient;
}

public async Task<IEnumerable<OrderWithStatus>> GetOrders() =>
await httpClient.GetFromJsonAsync<IEnumerable<OrderWithStatus>>("orders");
public async Task<IEnumerable<OrderWithStatus>> GetOrders() =>
await httpClient.GetFromJsonAsync("orders", OrderContext.Default.ListOrderWithStatus) ?? new();


public async Task<OrderWithStatus> GetOrder(int orderId) =>
await httpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{orderId}");
public async Task<OrderWithStatus> GetOrder(int orderId) =>
await httpClient.GetFromJsonAsync($"orders/{orderId}", OrderContext.Default.OrderWithStatus) ?? new();


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<int> PlaceOrder(Order order)
{
var response = await httpClient.PostAsJsonAsync("orders", order, OrderContext.Default.Order);
response.EnsureSuccessStatusCode();
var orderId = await response.Content.ReadFromJsonAsync<int>();
return orderId;
}

}
```

@@ -295,24 +290,27 @@ We have already written this context for you and it is located in the `BlazingPi
[JsonSerializable(typeof(List<PizzaSpecial>))]
[JsonSerializable(typeof(List<Topping>))]
[JsonSerializable(typeof(Topping))]
[JsonSerializable(typeof(Dictionary<string, string>))]
public partial class OrderContext : JsonSerializerContext {}
```

You can now optimize the calls to the HttpClient in the `OrdersClient` class by passing an `OrderContext.Default` parameter pointing to the type sought as the second parameter. Updating the methods in the `OrdersClient` class should look like the following:

```csharp
public async Task<IEnumerable<OrderWithStatus>> GetOrders() =>
await httpClient.GetFromJsonAsync("orders", OrderContext.Default.ListOrderWithStatus);
await httpClient.GetFromJsonAsync("orders", OrderContext.Default.ListOrderWithStatus) ?? new();


public async Task<OrderWithStatus> GetOrder(int orderId) =>
await httpClient.GetFromJsonAsync($"orders/{orderId}", OrderContext.Default.OrderWithStatus);
await httpClient.GetFromJsonAsync($"orders/{orderId}", OrderContext.Default.OrderWithStatus) ?? new();


public async Task<int> PlaceOrder(Order order)
{
var response = await httpClient.PostAsJsonAsync("orders", order, OrderContext.Default.Order);
response.EnsureSuccessStatusCode();
var orderId = await response.Content.ReadFromJsonAsync<int>();
return orderId;
var response = await httpClient.PostAsJsonAsync("orders", order, OrderContext.Default.Order);
response.EnsureSuccessStatusCode();
var orderId = await response.Content.ReadFromJsonAsync<int>();
return orderId;
}
```

@@ -369,7 +367,15 @@ private async void PollForUpdates()
{
orderWithStatus = await OrdersClient.GetOrder(OrderId);
StateHasChanged();
await Task.Delay(4000);

if (orderWithStatus.IsDelivered)
{
pollingCancellationToken.Cancel();
}
else
{
await Task.Delay(4000);
}
}
catch (AccessTokenNotAvailableException ex)
{
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public OrdersController(PizzaStoreContext db)
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
{
var orders = await _db.Orders
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -35,7 +35,7 @@ public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
{
var order = await _db.Orders
.Where(o => o.OrderId == orderId)
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -54,7 +54,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
// order.UserId = GetUserId();
// order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public OrdersController(PizzaStoreContext db)
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
{
var orders = await _db.Orders
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -35,7 +35,7 @@ public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
{
var order = await _db.Orders
.Where(o => o.OrderId == orderId)
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -54,7 +54,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
// order.UserId = GetUserId();
// order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public OrdersController(PizzaStoreContext db)
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
{
var orders = await _db.Orders
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -35,7 +35,7 @@ public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
{
var order = await _db.Orders
.Where(o => o.OrderId == orderId)
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -54,7 +54,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
// order.UserId = GetUserId();
// order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public OrdersController(PizzaStoreContext db)
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
{
var orders = await _db.Orders
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -35,7 +35,7 @@ public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
{
var order = await _db.Orders
.Where(o => o.OrderId == orderId)
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -54,7 +54,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
// order.UserId = GetUserId();
// order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public OrdersController(PizzaStoreContext db)
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
{
var orders = await _db.Orders
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -35,7 +35,7 @@ public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
{
var order = await _db.Orders
.Where(o => o.OrderId == orderId)
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -54,7 +54,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
// order.UserId = GetUserId();
// order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public OrdersController(PizzaStoreContext db)
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
{
var orders = await _db.Orders
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -35,7 +35,7 @@ public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
{
var order = await _db.Orders
.Where(o => o.OrderId == orderId)
// .Where(o => o.UserId == GetUserId())
// .Where(o => o.UserId == PizzaApiExtensions.GetUserId(HttpContext))
.Include(o => o.DeliveryLocation)
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
@@ -54,7 +54,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
// order.UserId = GetUserId();
// order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ public async Task<ActionResult<int>> PlaceOrder(Order order)
{
order.CreatedTime = DateTime.Now;
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
order.UserId = PizzaApiExtensions.GetUserId(HttpContext);
order.UserId = PizzaApiExtensions.GetUserId(HttpContext);

// Enforce existence of Pizza.SpecialId and Topping.ToppingId
// in the database - prevent the submitter from making up