2020-08-04 16:26:29 +05:30
|
|
|
|
using GrpcOrdering;
|
2019-08-27 15:08:39 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|
|
|
|
{
|
|
|
|
|
public class OrderingService : IOrderingService
|
|
|
|
|
{
|
2020-12-16 18:01:27 +01:00
|
|
|
|
private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
|
2019-08-27 15:08:39 +02:00
|
|
|
|
private readonly ILogger<OrderingService> _logger;
|
|
|
|
|
|
2020-12-16 18:01:27 +01:00
|
|
|
|
public OrderingService(OrderingGrpc.OrderingGrpcClient orderingGrpcClient, ILogger<OrderingService> logger)
|
2019-08-27 15:08:39 +02:00
|
|
|
|
{
|
2020-12-16 18:01:27 +01:00
|
|
|
|
_orderingGrpcClient = orderingGrpcClient;
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
|
|
|
|
|
{
|
2020-12-16 18:01:27 +01:00
|
|
|
|
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
2019-08-27 15:08:39 +02:00
|
|
|
|
|
2020-12-16 18:01:27 +01:00
|
|
|
|
var command = MapToOrderDraftCommand(basketData);
|
|
|
|
|
var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
|
|
|
|
|
_logger.LogDebug(" grpc response: {@response}", response);
|
2019-08-27 15:08:39 +02:00
|
|
|
|
|
2020-12-16 18:01:27 +01:00
|
|
|
|
return MapToResponse(response, basketData);
|
2019-08-27 15:08:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 09:13:02 +02:00
|
|
|
|
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)
|
2019-08-27 15:08:39 +02:00
|
|
|
|
{
|
|
|
|
|
if (orderDraft == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data = new OrderData
|
|
|
|
|
{
|
2019-08-28 09:13:02 +02:00
|
|
|
|
Buyer = basketData.BuyerId,
|
2019-08-27 15:08:39 +02:00
|
|
|
|
Total = (decimal)orderDraft.Total,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
orderDraft.OrderItems.ToList().ForEach(o => data.OrderItems.Add(new OrderItemData
|
|
|
|
|
{
|
|
|
|
|
Discount = (decimal)o.Discount,
|
|
|
|
|
PictureUrl = o.PictureUrl,
|
|
|
|
|
ProductId = o.ProductId,
|
|
|
|
|
ProductName = o.ProductName,
|
|
|
|
|
UnitPrice = (decimal)o.UnitPrice,
|
|
|
|
|
Units = o.Units,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CreateOrderDraftCommand MapToOrderDraftCommand(BasketData basketData)
|
|
|
|
|
{
|
|
|
|
|
var command = new CreateOrderDraftCommand
|
|
|
|
|
{
|
|
|
|
|
BuyerId = basketData.BuyerId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
basketData.Items.ForEach(i => command.Items.Add(new BasketItem
|
|
|
|
|
{
|
|
|
|
|
Id = i.Id,
|
|
|
|
|
OldUnitPrice = (double)i.OldUnitPrice,
|
|
|
|
|
PictureUrl = i.PictureUrl,
|
|
|
|
|
ProductId = i.ProductId,
|
|
|
|
|
ProductName = i.ProductName,
|
|
|
|
|
Quantity = i.Quantity,
|
|
|
|
|
UnitPrice = (double)i.UnitPrice,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|