2016-10-21 05:46:30 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-03-03 16:00:15 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
2016-10-31 17:56:24 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2016-11-28 12:58:51 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using Newtonsoft.Json;
|
2016-12-12 10:15:24 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2017-04-06 16:59:17 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http;
|
2016-10-21 05:46:30 +02:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Services
|
|
|
|
|
{
|
|
|
|
|
public class OrderingService : IOrderingService
|
|
|
|
|
{
|
2017-03-17 13:12:34 +01:00
|
|
|
|
private IHttpClient _apiClient;
|
2016-11-28 12:58:51 +01:00
|
|
|
|
private readonly string _remoteServiceBaseUrl;
|
2017-02-06 13:53:36 +01:00
|
|
|
|
private readonly IOptionsSnapshot<AppSettings> _settings;
|
2016-12-12 10:15:24 +01:00
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccesor;
|
2016-10-21 05:46:30 +02:00
|
|
|
|
|
2017-03-17 13:12:34 +01:00
|
|
|
|
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient)
|
2016-10-21 05:46:30 +02:00
|
|
|
|
{
|
2016-11-28 12:58:51 +01:00
|
|
|
|
_remoteServiceBaseUrl = $"{settings.Value.OrderingUrl}/api/v1/orders";
|
|
|
|
|
_settings = settings;
|
2016-12-12 10:15:24 +01:00
|
|
|
|
_httpContextAccesor = httpContextAccesor;
|
2017-03-17 13:12:34 +01:00
|
|
|
|
_apiClient = httpClient;
|
2016-10-21 05:46:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
async public Task<Order> GetOrder(ApplicationUser user, string Id)
|
2016-10-21 05:46:30 +02:00
|
|
|
|
{
|
2016-12-12 10:15:24 +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-12-12 10:15:24 +01:00
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
var ordersUrl = $"{_remoteServiceBaseUrl}/{Id}";
|
|
|
|
|
var dataString = await _apiClient.GetStringAsync(ordersUrl);
|
2016-12-17 14:41:16 +01:00
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
var response = JsonConvert.DeserializeObject<Order>(dataString);
|
|
|
|
|
|
|
|
|
|
return response;
|
2016-10-21 05:46:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
async public Task<List<Order>> GetMyOrders(ApplicationUser user)
|
2016-10-21 05:46:30 +02:00
|
|
|
|
{
|
2016-12-12 10:15:24 +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-12-12 10:15:24 +01:00
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
var ordersUrl = _remoteServiceBaseUrl;
|
|
|
|
|
var dataString = await _apiClient.GetStringAsync(ordersUrl);
|
|
|
|
|
var response = JsonConvert.DeserializeObject<List<Order>>(dataString);
|
|
|
|
|
|
2016-12-12 10:15:24 +01:00
|
|
|
|
return response;
|
2016-10-21 05:46:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 17:17:26 +01:00
|
|
|
|
public Order MapUserInfoIntoOrder(ApplicationUser user, Order order)
|
2016-10-21 05:46:30 +02:00
|
|
|
|
{
|
2016-12-17 14:41:16 +01:00
|
|
|
|
order.City = user.City;
|
|
|
|
|
order.Street = user.Street;
|
|
|
|
|
order.State = user.State;
|
|
|
|
|
order.Country = user.Country;
|
2017-01-30 15:46:43 +01:00
|
|
|
|
order.ZipCode = user.ZipCode;
|
|
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
|
order.CardNumber = user.CardNumber;
|
|
|
|
|
order.CardHolderName = user.CardHolderName;
|
2016-12-20 12:22:28 +01:00
|
|
|
|
order.CardExpiration = new DateTime(int.Parse("20" + user.Expiration.Split('/')[1]),int.Parse(user.Expiration.Split('/')[0]), 1);
|
2016-12-17 14:41:16 +01:00
|
|
|
|
order.CardSecurityNumber = user.SecurityNumber;
|
2016-11-03 17:17:26 +01:00
|
|
|
|
|
|
|
|
|
return order;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
|
async public Task CreateOrder(Order order)
|
2016-11-03 17:17:26 +01:00
|
|
|
|
{
|
2016-12-12 10:15:24 +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);
|
|
|
|
|
_apiClient.Inst.DefaultRequestHeaders.Add("x-requestid", order.RequestId.ToString());
|
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
var ordersUrl = $"{_remoteServiceBaseUrl}/new";
|
2016-12-17 14:41:16 +01:00
|
|
|
|
order.CardTypeId = 1;
|
|
|
|
|
order.CardExpirationApiFormat();
|
2017-01-30 15:46:43 +01:00
|
|
|
|
SetFakeIdToProducts(order);
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
var response = await _apiClient.PostAsync(ordersUrl, order);
|
2016-11-03 17:17:26 +01:00
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
|
|
|
|
|
throw new Exception("Error creating order, try later");
|
2017-03-27 14:05:28 +02:00
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
2016-11-03 17:17:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OverrideUserInfoIntoOrder(Order original, Order destination)
|
|
|
|
|
{
|
2016-12-17 14:41:16 +01:00
|
|
|
|
destination.City = original.City;
|
|
|
|
|
destination.Street = original.Street;
|
|
|
|
|
destination.State = original.State;
|
|
|
|
|
destination.Country = original.Country;
|
2017-01-30 15:46:43 +01:00
|
|
|
|
destination.ZipCode = original.ZipCode;
|
2016-12-17 14:41:16 +01:00
|
|
|
|
|
|
|
|
|
destination.CardNumber = original.CardNumber;
|
|
|
|
|
destination.CardHolderName = original.CardHolderName;
|
|
|
|
|
destination.CardExpiration = original.CardExpiration;
|
|
|
|
|
destination.CardSecurityNumber = original.CardSecurityNumber;
|
2016-10-21 05:46:30 +02:00
|
|
|
|
}
|
2017-01-30 15:46:43 +01:00
|
|
|
|
|
|
|
|
|
private void SetFakeIdToProducts(Order order)
|
|
|
|
|
{
|
|
|
|
|
var id = 1;
|
|
|
|
|
order.OrderItems.ForEach(x => { x.ProductId = id; id++; });
|
|
|
|
|
}
|
2016-10-21 05:46:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|