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 ;
6
2
using Microsoft . AspNetCore . Mvc ;
7
3
using Microsoft . EntityFrameworkCore ;
8
4
9
- namespace BlazingPizza . Server
5
+ namespace BlazingPizza . Server ;
6
+
7
+ [ Route ( "orders" ) ]
8
+ [ ApiController ]
9
+ // [Authorize]
10
+ public class OrdersController : Controller
10
11
{
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