@ -1,11 +1,9 @@
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries
{
using Dapper ;
using Microsoft.Extensions.Configuration ;
using System.Data.SqlClient ;
using System.Threading.Tasks ;
using System ;
using System.Dynamic ;
using System.Collections.Generic ;
public class OrderQueries
@ -19,7 +17,7 @@
}
public async Task < dynamic > GetOrderAsync ( int id )
public async Task < Order > GetOrderAsync ( int id )
{
using ( var connection = new SqlConnection ( _connectionString ) )
{
@ -44,13 +42,13 @@
}
}
public async Task < IEnumerable < dynamic > > GetOrdersAsync ( )
public async Task < IEnumerable < OrderSummary > > GetOrdersAsync ( )
{
using ( var connection = new SqlConnection ( _connectionString ) )
{
connection . Open ( ) ;
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
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
FROM [ ordering ] . [ Orders ] o
LEFT JOIN [ ordering ] . [ orderitems ] oi ON o . Id = oi . orderid
LEFT JOIN [ ordering ] . [ orderstatus ] os on o . OrderStatusId = os . Id
@ -59,39 +57,41 @@
}
}
public async Task < IEnumerable < dynamic > > GetCardTypesAsync ( )
public async Task < IEnumerable < CardType > > GetCardTypesAsync ( )
{
using ( var connection = new SqlConnection ( _connectionString ) )
{
connection . Open ( ) ;
return await connection . QueryAsync < dynamic > ( "SELECT * FROM ordering.cardtypes" ) ;
return await connection . QueryAsync < CardType > ( "SELECT * FROM ordering.cardtypes" ) ;
}
}
private dynamic MapOrderItems ( dynamic result )
private Order MapOrderItems ( dynamic result )
{
dynamic order = new ExpandoObject ( ) ;
order . ordernumber = result [ 0 ] . ordernumber ;
order . date = result [ 0 ] . date ;
order . status = result [ 0 ] . status ;
order . description = result [ 0 ] . description ;
order . street = result [ 0 ] . street ;
order . city = result [ 0 ] . city ;
order . zipcode = result [ 0 ] . zipcode ;
order . country = result [ 0 ] . country ;
order . orderitems = new List < dynamic > ( ) ;
order . total = 0 ;
var order = new Order
{
ordernumber = result [ 0 ] . ordernumber ,
date = result [ 0 ] . date ,
status = result [ 0 ] . status ,
description = result [ 0 ] . description ,
street = result [ 0 ] . street ,
city = result [ 0 ] . city ,
zipcode = result [ 0 ] . zipcode ,
country = result [ 0 ] . country ,
orderitems = new List < Orderitem > ( ) ,
total = 0
} ;
foreach ( dynamic item in result )
{
dynamic orderitem = new ExpandoObject ( ) ;
orderitem . productname = item . productname ;
orderitem . units = item . units ;
orderitem . unitprice = item . unitprice ;
orderitem . pictureurl = item . pictureurl ;
var orderitem = new Orderitem
{
productname = item . productname ,
units = item . units ,
unitprice = ( double ) item . unitprice ,
pictureurl = item . pictureurl
} ;
order . total + = item . units * item . unitprice ;
order . orderitems . Add ( orderitem ) ;