no more dynamics as a return types from controllers
This commit is contained in:
parent
338d911621
commit
5e13467315
@ -5,10 +5,10 @@
|
|||||||
|
|
||||||
public interface IOrderQueries
|
public interface IOrderQueries
|
||||||
{
|
{
|
||||||
Task<dynamic> GetOrderAsync(int id);
|
Task<Order> GetOrderAsync(int id);
|
||||||
|
|
||||||
Task<IEnumerable<dynamic>> GetOrdersAsync();
|
Task<IEnumerable<OrderSummary>> GetOrdersAsync();
|
||||||
|
|
||||||
Task<IEnumerable<dynamic>> GetCardTypesAsync();
|
Task<IEnumerable<CardType>> GetCardTypesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries
|
||||||
{
|
{
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System;
|
using System;
|
||||||
using System.Dynamic;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public class OrderQueries
|
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))
|
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))
|
using (var connection = new SqlConnection(_connectionString))
|
||||||
{
|
{
|
||||||
connection.Open();
|
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
|
FROM [ordering].[Orders] o
|
||||||
LEFT JOIN[ordering].[orderitems] oi ON o.Id = oi.orderid
|
LEFT JOIN[ordering].[orderitems] oi ON o.Id = oi.orderid
|
||||||
LEFT JOIN[ordering].[orderstatus] os on o.OrderStatusId = os.Id
|
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))
|
using (var connection = new SqlConnection(_connectionString))
|
||||||
{
|
{
|
||||||
connection.Open();
|
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();
|
var order = new Order
|
||||||
|
{
|
||||||
order.ordernumber = result[0].ordernumber;
|
ordernumber = result[0].ordernumber,
|
||||||
order.date = result[0].date;
|
date = result[0].date,
|
||||||
order.status = result[0].status;
|
status = result[0].status,
|
||||||
order.description = result[0].description;
|
description = result[0].description,
|
||||||
order.street = result[0].street;
|
street = result[0].street,
|
||||||
order.city = result[0].city;
|
city = result[0].city,
|
||||||
order.zipcode = result[0].zipcode;
|
zipcode = result[0].zipcode,
|
||||||
order.country = result[0].country;
|
country = result[0].country,
|
||||||
|
orderitems = new List<Orderitem>(),
|
||||||
order.orderitems = new List<dynamic>();
|
total = 0
|
||||||
order.total = 0;
|
};
|
||||||
|
|
||||||
foreach (dynamic item in result)
|
foreach (dynamic item in result)
|
||||||
{
|
{
|
||||||
dynamic orderitem = new ExpandoObject();
|
var orderitem = new Orderitem
|
||||||
orderitem.productname = item.productname;
|
{
|
||||||
orderitem.units = item.units;
|
productname = item.productname,
|
||||||
orderitem.unitprice = item.unitprice;
|
units = item.units,
|
||||||
orderitem.pictureurl = item.pictureurl;
|
unitprice = (double)item.unitprice,
|
||||||
|
pictureurl = item.pictureurl
|
||||||
|
};
|
||||||
|
|
||||||
order.total += item.units * item.unitprice;
|
order.total += item.units * item.unitprice;
|
||||||
order.orderitems.Add(orderitem);
|
order.orderitems.Add(orderitem);
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries
|
||||||
|
{
|
||||||
|
public class Orderitem
|
||||||
|
{
|
||||||
|
public string productname { get; set; }
|
||||||
|
public int units { get; set; }
|
||||||
|
public double unitprice { get; set; }
|
||||||
|
public string pictureurl { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Order
|
||||||
|
{
|
||||||
|
public int ordernumber { get; set; }
|
||||||
|
public DateTime date { get; set; }
|
||||||
|
public string status { get; set; }
|
||||||
|
public string description { get; set; }
|
||||||
|
public string street { get; set; }
|
||||||
|
public string city { get; set; }
|
||||||
|
public string zipcode { get; set; }
|
||||||
|
public string country { get; set; }
|
||||||
|
public List<Orderitem> orderitems { get; set; }
|
||||||
|
public decimal total { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OrderSummary
|
||||||
|
{
|
||||||
|
public int ordernumber { get; set; }
|
||||||
|
public DateTime date { get; set; }
|
||||||
|
public string status { get; set; }
|
||||||
|
public double total { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CardType
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -64,7 +64,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|||||||
|
|
||||||
[Route("{orderId:int}")]
|
[Route("{orderId:int}")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(Order),(int)HttpStatusCode.OK)]
|
||||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||||
public async Task<IActionResult> GetOrder(int orderId)
|
public async Task<IActionResult> GetOrder(int orderId)
|
||||||
{
|
{
|
||||||
@ -83,6 +83,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|||||||
|
|
||||||
[Route("")]
|
[Route("")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<OrderSummary>), (int)HttpStatusCode.OK)]
|
||||||
public async Task<IActionResult> GetOrders()
|
public async Task<IActionResult> GetOrders()
|
||||||
{
|
{
|
||||||
var orderTask = _orderQueries.GetOrdersAsync();
|
var orderTask = _orderQueries.GetOrdersAsync();
|
||||||
@ -94,6 +95,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|||||||
|
|
||||||
[Route("cardtypes")]
|
[Route("cardtypes")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<CardType>), (int)HttpStatusCode.OK)]
|
||||||
public async Task<IActionResult> GetCardTypes()
|
public async Task<IActionResult> GetCardTypes()
|
||||||
{
|
{
|
||||||
var cardTypes = await _orderQueries
|
var cardTypes = await _orderQueries
|
||||||
|
Loading…
x
Reference in New Issue
Block a user