2018-05-18 14:02:18 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
|
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Newtonsoft.Json;
|
2018-05-18 14:02:18 +02:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|
|
|
|
{
|
|
|
|
|
public class BasketService : IBasketService
|
|
|
|
|
{
|
2018-05-18 14:02:18 +02:00
|
|
|
|
private readonly HttpClient _apiClient;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
private readonly ILogger<BasketService> _logger;
|
|
|
|
|
private readonly UrlsConfig _urls;
|
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
public BasketService(HttpClient httpClient,ILogger<BasketService> logger, IOptions<UrlsConfig> config)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
_apiClient = httpClient;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_urls = config.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<BasketData> GetByIdAsync(string id)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2018-05-18 14:02:18 +02:00
|
|
|
|
var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id));
|
2018-02-27 14:32:25 +01:00
|
|
|
|
var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject<BasketData>(data) : null;
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2018-02-27 14:32:25 +01:00
|
|
|
|
return basket;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task UpdateAsync(BasketData currentBasket)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2018-05-18 14:02:18 +02:00
|
|
|
|
var basketContent = new StringContent(JsonConvert.SerializeObject(currentBasket), System.Text.Encoding.UTF8, "application/json");
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
await _apiClient.PostAsync(_urls.Basket + UrlsConfig.BasketOperations.UpdateBasket(), basketContent);
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|