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
|
||||
{
|
||||
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
|
||||
{
|
||||
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);
|
||||
|
@ -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}")]
|
||||
[HttpGet]
|
||||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType(typeof(Order),(int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> GetOrder(int orderId)
|
||||
{
|
||||
@ -83,6 +83,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
||||
|
||||
[Route("")]
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<OrderSummary>), (int)HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetOrders()
|
||||
{
|
||||
var orderTask = _orderQueries.GetOrdersAsync();
|
||||
@ -94,6 +95,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
||||
|
||||
[Route("cardtypes")]
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<CardType>), (int)HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetCardTypes()
|
||||
{
|
||||
var cardTypes = await _orderQueries
|
||||
|
Loading…
x
Reference in New Issue
Block a user