2017-03-17 10:00:18 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
2016-11-16 10:19:00 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Newtonsoft.Json;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using WebMVC.Services.Utilities;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Services
|
|
|
|
|
{
|
|
|
|
|
public class BasketService : IBasketService
|
|
|
|
|
{
|
2017-02-06 13:53:36 +01:00
|
|
|
|
private readonly IOptionsSnapshot<AppSettings> _settings;
|
2017-03-17 13:12:34 +01:00
|
|
|
|
private IHttpClient _apiClient;
|
2016-11-16 10:19:00 +01:00
|
|
|
|
private readonly string _remoteServiceBaseUrl;
|
2016-11-29 15:10:16 +01:00
|
|
|
|
private IHttpContextAccessor _httpContextAccesor;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
|
2017-03-17 13:12:34 +01:00
|
|
|
|
public BasketService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient)
|
2016-11-02 20:41:12 +01:00
|
|
|
|
{
|
2016-11-16 10:19:00 +01:00
|
|
|
|
_settings = settings;
|
|
|
|
|
_remoteServiceBaseUrl = _settings.Value.BasketUrl;
|
2016-11-29 15:10:16 +01:00
|
|
|
|
_httpContextAccesor = httpContextAccesor;
|
2017-03-17 13:12:34 +01:00
|
|
|
|
_apiClient = httpClient;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public async Task<Basket> GetBasket(ApplicationUser user)
|
2016-11-02 20:41:12 +01:00
|
|
|
|
{
|
2016-11-29 15:10:16 +01:00
|
|
|
|
var context = _httpContextAccesor.HttpContext;
|
|
|
|
|
var token = await context.Authentication.GetTokenAsync("access_token");
|
|
|
|
|
|
2017-03-17 10:00:18 +01:00
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
2016-11-29 15:10:16 +01:00
|
|
|
|
|
2017-03-20 14:18:20 -04:00
|
|
|
|
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id}";
|
2016-11-16 10:19:00 +01:00
|
|
|
|
var dataString = await _apiClient.GetStringAsync(basketUrl);
|
2017-03-20 14:18:20 -04:00
|
|
|
|
// Use the ?? Null conditional operator to simplify the initialization of response
|
|
|
|
|
var response = JsonConvert.DeserializeObject<Basket>(dataString) ??
|
|
|
|
|
new Basket()
|
2016-11-16 10:19:00 +01:00
|
|
|
|
{
|
|
|
|
|
BuyerId = user.Id
|
|
|
|
|
};
|
2016-11-02 20:41:12 +01:00
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
return response;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public async Task<Basket> UpdateBasket(Basket basket)
|
2016-11-02 20:41:12 +01:00
|
|
|
|
{
|
2016-11-29 15:10:16 +01:00
|
|
|
|
var context = _httpContextAccesor.HttpContext;
|
|
|
|
|
var token = await context.Authentication.GetTokenAsync("access_token");
|
2017-03-17 13:12:34 +01:00
|
|
|
|
|
2017-03-17 10:00:18 +01:00
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
2016-11-29 15:10:16 +01:00
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
var basketUrl = _remoteServiceBaseUrl;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
var response = await _apiClient.PostAsync(basketUrl, basket);
|
2016-11-16 10:19:00 +01:00
|
|
|
|
|
|
|
|
|
return basket;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public async Task<Basket> SetQuantities(ApplicationUser user, Dictionary<string, int> quantities)
|
2016-11-02 20:41:12 +01:00
|
|
|
|
{
|
2016-11-16 10:19:00 +01:00
|
|
|
|
var basket = await GetBasket(user);
|
2016-11-02 20:41:12 +01:00
|
|
|
|
|
|
|
|
|
basket.Items.ForEach(x =>
|
|
|
|
|
{
|
2017-03-20 14:18:20 -04:00
|
|
|
|
// Simplify this logic by using the
|
|
|
|
|
// new out variable initializer.
|
|
|
|
|
if (quantities.TryGetValue(x.Id, out var quantity))
|
|
|
|
|
{
|
|
|
|
|
x.Quantity = quantity;
|
|
|
|
|
}
|
2016-11-02 20:41:12 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return basket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Order MapBasketToOrder(Basket basket)
|
|
|
|
|
{
|
2016-12-17 14:41:16 +01:00
|
|
|
|
var order = new Order();
|
|
|
|
|
order.Total = 0;
|
2016-11-02 20:41:12 +01:00
|
|
|
|
|
|
|
|
|
basket.Items.ForEach(x =>
|
|
|
|
|
{
|
|
|
|
|
order.OrderItems.Add(new OrderItem()
|
|
|
|
|
{
|
2016-12-17 14:41:16 +01:00
|
|
|
|
ProductId = int.Parse(x.ProductId),
|
|
|
|
|
|
2016-11-02 20:41:12 +01:00
|
|
|
|
PictureUrl = x.PictureUrl,
|
|
|
|
|
ProductName = x.ProductName,
|
2016-12-17 14:41:16 +01:00
|
|
|
|
Units = x.Quantity,
|
2016-11-02 20:41:12 +01:00
|
|
|
|
UnitPrice = x.UnitPrice
|
|
|
|
|
});
|
2016-12-17 14:41:16 +01:00
|
|
|
|
order.Total += (x.Quantity * x.UnitPrice);
|
2016-11-02 20:41:12 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return order;
|
|
|
|
|
}
|
2016-11-03 17:17:26 +01:00
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public async Task AddItemToBasket(ApplicationUser user, BasketItem product)
|
2016-11-03 17:17:26 +01:00
|
|
|
|
{
|
2016-11-16 10:19:00 +01:00
|
|
|
|
Basket basket = await GetBasket(user);
|
|
|
|
|
if (basket == null)
|
2016-11-03 17:17:26 +01:00
|
|
|
|
{
|
2016-11-16 10:19:00 +01:00
|
|
|
|
basket = new Basket()
|
2016-11-03 17:17:26 +01:00
|
|
|
|
{
|
|
|
|
|
BuyerId = user.Id,
|
2016-12-17 14:41:16 +01:00
|
|
|
|
Items = new List<BasketItem>()
|
2016-11-03 17:17:26 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
basket.Items.Add(product);
|
|
|
|
|
await UpdateBasket(basket);
|
2016-11-03 17:17:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 10:19:00 +01:00
|
|
|
|
public async Task CleanBasket(ApplicationUser user)
|
2016-11-03 17:17:26 +01:00
|
|
|
|
{
|
2016-11-29 15:10:16 +01:00
|
|
|
|
var context = _httpContextAccesor.HttpContext;
|
|
|
|
|
var token = await context.Authentication.GetTokenAsync("access_token");
|
|
|
|
|
|
2017-03-17 10:00:18 +01:00
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
2017-03-20 14:18:20 -04:00
|
|
|
|
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id}";
|
2016-11-16 10:19:00 +01:00
|
|
|
|
var response = await _apiClient.DeleteAsync(basketUrl);
|
|
|
|
|
|
|
|
|
|
//CCE: response status code...
|
|
|
|
|
|
2016-11-03 17:17:26 +01:00
|
|
|
|
}
|
2016-11-02 20:41:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|