Skip to content

Commit dac41d2

Browse files
committed
Added EF compiled models
1 parent 6bf9947 commit dac41d2

36 files changed

+2186
-586
lines changed

save-points/00-get-started/BlazingPizza.Client/BlazingPizza.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
5+
<ImplicitUsings>true</ImplicitUsings>
56
</PropertyGroup>
67

78
<ItemGroup>
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2-
using Microsoft.Extensions.DependencyInjection;
3-
using System;
4-
using System.Net.Http;
5-
using System.Threading.Tasks;
1+
using BlazingPizza.Client;
2+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
63

7-
namespace BlazingPizza.Client
8-
{
9-
public class Program
10-
{
11-
public static async Task Main(string[] args)
12-
{
13-
var builder = WebAssemblyHostBuilder.CreateDefault(args);
14-
builder.RootComponents.Add<App>("#app");
4+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
5+
builder.RootComponents.Add<App>("#app");
156

16-
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
7+
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
178

18-
await builder.Build().RunAsync();
19-
}
20-
}
21-
}
9+
await builder.Build().RunAsync();

save-points/00-get-started/BlazingPizza.ComponentsLibrary/BlazingPizza.ComponentsLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
5+
<ImplicitUsings>true</ImplicitUsings>
56
</PropertyGroup>
67

78
<ItemGroup>
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using Microsoft.JSInterop;
2-
using System.Threading.Tasks;
32

4-
namespace BlazingPizza.ComponentsLibrary
3+
namespace BlazingPizza.ComponentsLibrary;
4+
5+
public static class LocalStorage
56
{
6-
public static class LocalStorage
7-
{
8-
public static ValueTask<T> GetAsync<T>(IJSRuntime jsRuntime, string key)
9-
=> jsRuntime.InvokeAsync<T>("blazorLocalStorage.get", key);
7+
public static ValueTask<T> GetAsync<T>(IJSRuntime jsRuntime, string key)
8+
=> jsRuntime.InvokeAsync<T>("blazorLocalStorage.get", key);
109

11-
public static ValueTask SetAsync(IJSRuntime jsRuntime, string key, object value)
12-
=> jsRuntime.InvokeVoidAsync("blazorLocalStorage.set", key, value);
10+
public static ValueTask SetAsync(IJSRuntime jsRuntime, string key, object value)
11+
=> jsRuntime.InvokeVoidAsync("blazorLocalStorage.set", key, value);
1312

14-
public static ValueTask DeleteAsync(IJSRuntime jsRuntime, string key)
15-
=> jsRuntime.InvokeVoidAsync("blazorLocalStorage.delete", key);
16-
}
13+
public static ValueTask DeleteAsync(IJSRuntime jsRuntime, string key)
14+
=> jsRuntime.InvokeVoidAsync("blazorLocalStorage.delete", key);
1715
}

save-points/00-get-started/BlazingPizza.Server/BlazingPizza.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
5+
<ImplicitUsings>true</ImplicitUsings>
56
</PropertyGroup>
67

78
<ItemGroup>

save-points/00-get-started/BlazingPizza.Server/NotificationsController.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
22
using Microsoft.AspNetCore.Mvc;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
73

8-
namespace BlazingPizza.Server
4+
namespace BlazingPizza.Server;
5+
6+
public class OidcConfigurationController : Controller
97
{
10-
public class OidcConfigurationController : Controller
11-
{
12-
public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider)
13-
{
14-
ClientRequestParametersProvider = clientRequestParametersProvider;
15-
}
8+
public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider)
9+
{
10+
ClientRequestParametersProvider = clientRequestParametersProvider;
11+
}
1612

17-
public IClientRequestParametersProvider ClientRequestParametersProvider { get; }
13+
public IClientRequestParametersProvider ClientRequestParametersProvider { get; }
1814

19-
[HttpGet("_configuration/{clientId}")]
20-
public IActionResult GetClientRequestParameters([FromRoute]string clientId)
21-
{
22-
var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
23-
return Ok(parameters);
24-
}
25-
}
26-
}
15+
[HttpGet("_configuration/{clientId}")]
16+
public IActionResult GetClientRequestParameters([FromRoute] string clientId)
17+
{
18+
var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
19+
return Ok(parameters);
20+
}
21+
}
Lines changed: 105 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,109 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Security.Claims;
5-
using System.Threading.Tasks;
1+
using System.Security.Claims;
62
using Microsoft.AspNetCore.Mvc;
73
using Microsoft.EntityFrameworkCore;
84

9-
namespace BlazingPizza.Server
5+
namespace BlazingPizza.Server;
6+
7+
[Route("orders")]
8+
[ApiController]
9+
// [Authorize]
10+
public class OrdersController : Controller
1011
{
11-
[Route("orders")]
12-
[ApiController]
13-
// [Authorize]
14-
public class OrdersController : Controller
15-
{
16-
private readonly PizzaStoreContext _db;
17-
18-
public OrdersController(PizzaStoreContext db)
19-
{
20-
_db = db;
21-
}
22-
23-
[HttpGet]
24-
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
25-
{
26-
var orders = await _db.Orders
27-
// .Where(o => o.UserId == GetUserId())
28-
.Include(o => o.DeliveryLocation)
29-
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
30-
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
31-
.OrderByDescending(o => o.CreatedTime)
32-
.ToListAsync();
33-
34-
return orders.Select(o => OrderWithStatus.FromOrder(o)).ToList();
35-
}
36-
37-
[HttpGet("{orderId}")]
38-
public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
39-
{
40-
var order = await _db.Orders
41-
.Where(o => o.OrderId == orderId)
42-
// .Where(o => o.UserId == GetUserId())
43-
.Include(o => o.DeliveryLocation)
44-
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
45-
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
46-
.SingleOrDefaultAsync();
47-
48-
if (order == null)
49-
{
50-
return NotFound();
51-
}
52-
53-
return OrderWithStatus.FromOrder(order);
54-
}
55-
56-
[HttpPost]
57-
public async Task<ActionResult<int>> PlaceOrder(Order order)
58-
{
59-
order.CreatedTime = DateTime.Now;
60-
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
61-
// order.UserId = GetUserId();
62-
63-
// Enforce existence of Pizza.SpecialId and Topping.ToppingId
64-
// in the database - prevent the submitter from making up
65-
// new specials and toppings
66-
foreach (var pizza in order.Pizzas)
67-
{
68-
pizza.SpecialId = pizza.Special.Id;
69-
pizza.Special = null;
70-
71-
foreach (var topping in pizza.Toppings)
72-
{
73-
topping.ToppingId = topping.Topping.Id;
74-
topping.Topping = null;
75-
}
76-
}
77-
78-
_db.Orders.Attach(order);
79-
await _db.SaveChangesAsync();
80-
81-
// In the background, send push notifications if possible
82-
var subscription = await _db.NotificationSubscriptions.Where(e => e.UserId == GetUserId()).SingleOrDefaultAsync();
83-
if (subscription != null)
84-
{
85-
_ = TrackAndSendNotificationsAsync(order, subscription);
86-
}
87-
88-
return order.OrderId;
89-
}
90-
91-
private string GetUserId()
92-
{
93-
return HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
94-
}
95-
96-
private static async Task TrackAndSendNotificationsAsync(Order order, NotificationSubscription subscription)
97-
{
98-
// In a realistic case, some other backend process would track
99-
// order delivery progress and send us notifications when it
100-
// changes. Since we don't have any such process here, fake it.
101-
await Task.Delay(OrderWithStatus.PreparationDuration);
102-
await SendNotificationAsync(order, subscription, "Your order has been dispatched!");
103-
104-
await Task.Delay(OrderWithStatus.DeliveryDuration);
105-
await SendNotificationAsync(order, subscription, "Your order is now delivered. Enjoy!");
106-
}
107-
108-
private static Task SendNotificationAsync(Order order, NotificationSubscription subscription, string message)
109-
{
110-
// This will be implemented later
111-
return Task.CompletedTask;
112-
}
113-
}
114-
}
12+
private readonly PizzaStoreContext _db;
13+
14+
public OrdersController(PizzaStoreContext db)
15+
{
16+
_db = db;
17+
}
18+
19+
[HttpGet]
20+
public async Task<ActionResult<List<OrderWithStatus>>> GetOrders()
21+
{
22+
var orders = await _db.Orders
23+
// .Where(o => o.UserId == GetUserId())
24+
.Include(o => o.DeliveryLocation)
25+
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
26+
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
27+
.OrderByDescending(o => o.CreatedTime)
28+
.ToListAsync();
29+
30+
return orders.Select(o => OrderWithStatus.FromOrder(o)).ToList();
31+
}
32+
33+
[HttpGet("{orderId}")]
34+
public async Task<ActionResult<OrderWithStatus>> GetOrderWithStatus(int orderId)
35+
{
36+
var order = await _db.Orders
37+
.Where(o => o.OrderId == orderId)
38+
// .Where(o => o.UserId == GetUserId())
39+
.Include(o => o.DeliveryLocation)
40+
.Include(o => o.Pizzas).ThenInclude(p => p.Special)
41+
.Include(o => o.Pizzas).ThenInclude(p => p.Toppings).ThenInclude(t => t.Topping)
42+
.SingleOrDefaultAsync();
43+
44+
if (order == null)
45+
{
46+
return NotFound();
47+
}
48+
49+
return OrderWithStatus.FromOrder(order);
50+
}
51+
52+
[HttpPost]
53+
public async Task<ActionResult<int>> PlaceOrder(Order order)
54+
{
55+
order.CreatedTime = DateTime.Now;
56+
order.DeliveryLocation = new LatLong(51.5001, -0.1239);
57+
// order.UserId = GetUserId();
58+
59+
// Enforce existence of Pizza.SpecialId and Topping.ToppingId
60+
// in the database - prevent the submitter from making up
61+
// new specials and toppings
62+
foreach (var pizza in order.Pizzas)
63+
{
64+
pizza.SpecialId = pizza.Special.Id;
65+
pizza.Special = null;
66+
67+
foreach (var topping in pizza.Toppings)
68+
{
69+
topping.ToppingId = topping.Topping.Id;
70+
topping.Topping = null;
71+
}
72+
}
73+
74+
_db.Orders.Attach(order);
75+
await _db.SaveChangesAsync();
76+
77+
// In the background, send push notifications if possible
78+
var subscription = await _db.NotificationSubscriptions.Where(e => e.UserId == GetUserId()).SingleOrDefaultAsync();
79+
if (subscription != null)
80+
{
81+
_ = TrackAndSendNotificationsAsync(order, subscription);
82+
}
83+
84+
return order.OrderId;
85+
}
86+
87+
private string GetUserId()
88+
{
89+
return HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
90+
}
91+
92+
private static async Task TrackAndSendNotificationsAsync(Order order, NotificationSubscription subscription)
93+
{
94+
// In a realistic case, some other backend process would track
95+
// order delivery progress and send us notifications when it
96+
// changes. Since we don't have any such process here, fake it.
97+
await Task.Delay(OrderWithStatus.PreparationDuration);
98+
await SendNotificationAsync(order, subscription, "Your order has been dispatched!");
99+
100+
await Task.Delay(OrderWithStatus.DeliveryDuration);
101+
await SendNotificationAsync(order, subscription, "Your order is now delivered. Enjoy!");
102+
}
103+
104+
private static Task SendNotificationAsync(Order order, NotificationSubscription subscription, string message)
105+
{
106+
// This will be implemented later
107+
return Task.CompletedTask;
108+
}
109+
}

0 commit comments

Comments
 (0)