11namespace Microsoft . eShopOnContainers . Services . Ordering . API . Application . Queries
22{
33 using Dapper ;
4- using Microsoft . Extensions . Configuration ;
54 using System . Data . SqlClient ;
65 using System . Threading . Tasks ;
76 using System ;
8- using System . Dynamic ;
97 using System . Collections . Generic ;
108
119 public class OrderQueries
@@ -19,7 +17,7 @@ public OrderQueries(string constr)
1917 }
2018
2119
22- public async Task < dynamic > GetOrderAsync ( int id )
20+ public async Task < Order > GetOrderAsync ( int id )
2321 {
2422 using ( var connection = new SqlConnection ( _connectionString ) )
2523 {
@@ -44,13 +42,13 @@ FROM ordering.Orders o
4442 }
4543 }
4644
47- public async Task < IEnumerable < dynamic > > GetOrdersAsync ( )
45+ public async Task < IEnumerable < OrderSummary > > GetOrdersAsync ( )
4846 {
4947 using ( var connection = new SqlConnection ( _connectionString ) )
5048 {
5149 connection . Open ( ) ;
5250
53- return await connection . QueryAsync < dynamic > ( @"SELECT o.[Id] as ordernumber,o.[OrderDate] as [date],os.[Name] as [status],SUM(oi.units*oi.unitprice) as total
51+ return await connection . QueryAsync < OrderSummary > ( @"SELECT o.[Id] as ordernumber,o.[OrderDate] as [date],os.[Name] as [status],SUM(oi.units*oi.unitprice) as total
5452 FROM [ordering].[Orders] o
5553 LEFT JOIN[ordering].[orderitems] oi ON o.Id = oi.orderid
5654 LEFT JOIN[ordering].[orderstatus] os on o.OrderStatusId = os.Id
@@ -59,39 +57,41 @@ FROM [ordering].[Orders] o
5957 }
6058 }
6159
62- public async Task < IEnumerable < dynamic > > GetCardTypesAsync ( )
60+ public async Task < IEnumerable < CardType > > GetCardTypesAsync ( )
6361 {
6462 using ( var connection = new SqlConnection ( _connectionString ) )
6563 {
6664 connection . Open ( ) ;
6765
68- return await connection . QueryAsync < dynamic > ( "SELECT * FROM ordering.cardtypes" ) ;
66+ return await connection . QueryAsync < CardType > ( "SELECT * FROM ordering.cardtypes" ) ;
6967 }
7068 }
7169
72- private dynamic MapOrderItems ( dynamic result )
70+ private Order MapOrderItems ( dynamic result )
7371 {
74- dynamic order = new ExpandoObject ( ) ;
75-
76- order . ordernumber = result [ 0 ] . ordernumber ;
77- order . date = result [ 0 ] . date ;
78- order . status = result [ 0 ] . status ;
79- order . description = result [ 0 ] . description ;
80- order . street = result [ 0 ] . street ;
81- order . city = result [ 0 ] . city ;
82- order . zipcode = result [ 0 ] . zipcode ;
83- order . country = result [ 0 ] . country ;
84-
85- order . orderitems = new List < dynamic > ( ) ;
86- order . total = 0 ;
72+ var order = new Order
73+ {
74+ ordernumber = result [ 0 ] . ordernumber ,
75+ date = result [ 0 ] . date ,
76+ status = result [ 0 ] . status ,
77+ description = result [ 0 ] . description ,
78+ street = result [ 0 ] . street ,
79+ city = result [ 0 ] . city ,
80+ zipcode = result [ 0 ] . zipcode ,
81+ country = result [ 0 ] . country ,
82+ orderitems = new List < Orderitem > ( ) ,
83+ total = 0
84+ } ;
8785
8886 foreach ( dynamic item in result )
8987 {
90- dynamic orderitem = new ExpandoObject ( ) ;
91- orderitem . productname = item . productname ;
92- orderitem . units = item . units ;
93- orderitem . unitprice = item . unitprice ;
94- orderitem . pictureurl = item . pictureurl ;
88+ var orderitem = new Orderitem
89+ {
90+ productname = item . productname ,
91+ units = item . units ,
92+ unitprice = ( double ) item . unitprice ,
93+ pictureurl = item . pictureurl
94+ } ;
9595
9696 order . total += item . units * item . unitprice ;
9797 order . orderitems . Add ( orderitem ) ;
0 commit comments