2019-08-19 09:24:58 +02:00
|
|
|
|
using Grpc.Core;
|
2019-08-26 13:57:43 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2019-08-19 09:24:58 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-08-26 13:57:43 +02:00
|
|
|
|
using GrpcBasket;
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
namespace GrpcBasket
|
2019-08-19 09:24:58 +02:00
|
|
|
|
{
|
|
|
|
|
public class BasketService : Basket.BasketBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IBasketRepository _repository;
|
|
|
|
|
private readonly ILogger<BasketService> _logger;
|
|
|
|
|
|
|
|
|
|
public BasketService(IBasketRepository repository, ILogger<BasketService> logger)
|
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public override Task<CustomerBasketResponse> GetBasketById(BasketRequest request, ServerCallContext context)
|
2019-08-19 09:24:58 +02:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Begin grpc call from method {context.Method} for basket id {request.Id}");
|
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
//context.ResponseTrailers.Add("grpc-status", "0");
|
|
|
|
|
//context.Status = Status.DefaultSuccess;
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
return Task.FromResult(new CustomerBasketResponse
|
2019-08-19 09:24:58 +02:00
|
|
|
|
{
|
2019-08-26 13:57:43 +02:00
|
|
|
|
Buyerid = "55"
|
|
|
|
|
});
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
// if (!context.Response.SupportsTrailers())
|
|
|
|
|
// {
|
|
|
|
|
// var headers = new HeaderDictionary();
|
|
|
|
|
// headers.Add("grpc-status", "0");
|
|
|
|
|
|
|
|
|
|
// Log.Logger.Information("Custom middleware headers {@headers}", headers);
|
|
|
|
|
// context.Features.Set<IHttpResponseTrailersFeature>(new TestHttpResponseTrailersFeature
|
|
|
|
|
// {
|
|
|
|
|
// Trailers = headers
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return next();
|
|
|
|
|
// var data = await _repository.GetBasketAsync(request.Id);
|
|
|
|
|
|
|
|
|
|
// if (data != null)
|
|
|
|
|
// {
|
|
|
|
|
// context.Status = new Status(StatusCode.OK, $"Basket with id {request.Id} do exist");
|
|
|
|
|
|
|
|
|
|
// return MapToCustomerBasketResponse(data);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// context.Status = new Status(StatusCode.NotFound, $"Basket with id {request.Id} do not exist");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return new CustomerBasketResponse();
|
2019-08-19 09:24:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<CustomerBasketResponse> UpdateBasket(CustomerBasketRequest request, ServerCallContext context)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Begin grpc call BasketService.UpdateBasketAsync for buyer id {request.Buyerid}");
|
|
|
|
|
|
|
|
|
|
var customerBasket = MapToCustomerBasket(request);
|
|
|
|
|
|
|
|
|
|
var response = await _repository.UpdateBasketAsync(customerBasket);
|
|
|
|
|
|
|
|
|
|
if (response != null)
|
|
|
|
|
{
|
|
|
|
|
return MapToCustomerBasketResponse(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Status = new Status(StatusCode.NotFound, $"Basket with buyer id {request.Buyerid} do not exist");
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CustomerBasketResponse MapToCustomerBasketResponse(CustomerBasket customerBasket)
|
|
|
|
|
{
|
|
|
|
|
var response = new CustomerBasketResponse
|
|
|
|
|
{
|
|
|
|
|
Buyerid = customerBasket.BuyerId
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
// customerBasket.Items.ForEach(item => response.Items.Add(new BasketItemResponse
|
|
|
|
|
// {
|
|
|
|
|
// Id = item.Id,
|
|
|
|
|
// Oldunitprice = (double)item.OldUnitPrice,
|
|
|
|
|
// Pictureurl = item.PictureUrl,
|
|
|
|
|
// Productid = item.ProductId,
|
|
|
|
|
// Productname = item.ProductName,
|
|
|
|
|
// Quantity = item.Quantity,
|
|
|
|
|
// Unitprice = (double)item.UnitPrice
|
|
|
|
|
// }));
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CustomerBasket MapToCustomerBasket(CustomerBasketRequest customerBasketRequest)
|
|
|
|
|
{
|
|
|
|
|
var response = new CustomerBasket
|
|
|
|
|
{
|
|
|
|
|
BuyerId = customerBasketRequest.Buyerid
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-26 13:57:43 +02:00
|
|
|
|
// customerBasketRequest.Items.ToList().ForEach(item => response.Items.Add(new BasketItem
|
|
|
|
|
// {
|
|
|
|
|
// Id = item.Id,
|
|
|
|
|
// OldUnitPrice = (decimal)item.Oldunitprice,
|
|
|
|
|
// PictureUrl = item.Pictureurl,
|
|
|
|
|
// ProductId = item.Productid,
|
|
|
|
|
// ProductName = item.Productname,
|
|
|
|
|
// Quantity = item.Quantity,
|
|
|
|
|
// UnitPrice = (decimal)item.Unitprice
|
|
|
|
|
// }));
|
2019-08-19 09:24:58 +02:00
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|