eShopOnContainers/src/Web/WebMVC/Services/OrderingService.cs

93 lines
3.8 KiB
C#
Raw Normal View History

2016-10-21 05:46:30 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.eShopOnContainers.WebMVC.Models;
2016-10-31 17:56:24 +01:00
using Microsoft.AspNetCore.Http;
2016-10-21 05:46:30 +02:00
namespace Microsoft.eShopOnContainers.WebMVC.Services
{
public class OrderingService : IOrderingService
{
private List<Order> _orders;
2016-10-31 17:56:24 +01:00
private int _itemsInCart;
private IHttpContextAccessor _httpContextAccessor;
public int ItemsInCart { get { return _itemsInCart; } }
2016-10-31 09:25:47 +01:00
//var ordersUrl = _settings.OrderingUrl + "/api/ordering/orders";
//var dataString = await _http.GetStringAsync(ordersUrl);
//var items = JsonConvert.DeserializeObject<List<Order>>(dataString);
2016-10-21 05:46:30 +02:00
2016-10-31 17:56:24 +01:00
public OrderingService(IHttpContextAccessor httpContextAccessor)
2016-10-21 05:46:30 +02:00
{
2016-10-31 17:56:24 +01:00
_httpContextAccessor = httpContextAccessor;
2016-10-21 05:46:30 +02:00
_orders = new List<Order>()
{
new Order()
{
2016-10-31 17:56:24 +01:00
BuyerId = new Guid("ebcbcb4c-b032-4baa-834b-7fd66d37bc95").ToString(),
OrderDate = DateTime.Now,
State = OrderState.Active,
OrderItems = new List<OrderItem>()
{
new OrderItem() { UnitPrice = 12, PicsUrl = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt", Quantity = 1, ProductName="Roslyn Red T-Shirt" }
}
},
new Order()
{
BuyerId = new Guid("ebcbcb4c-b032-4baa-834b-7fd66d37bc95").ToString(),
OrderDate = DateTime.Now,
State = OrderState.InProcess,
OrderItems = new List<OrderItem>()
{
new OrderItem() { UnitPrice = 12, PicsUrl = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt", Quantity = 1, ProductName="Roslyn Red T-Shirt" }
}
},
new Order()
{
BuyerId = new Guid("ebcbcb4c-b032-4baa-834b-7fd66d37bc95").ToString(),
OrderDate = DateTime.Now,
State = OrderState.InProcess,
2016-10-21 05:46:30 +02:00
OrderItems = new List<OrderItem>()
{
2016-10-31 17:56:24 +01:00
new OrderItem() { UnitPrice = 12, PicsUrl = "https://fakeimg.pl/370x240/EEEEEE/000/?text=RoslynRedT-Shirt", Quantity = 1, ProductName="Roslyn Red T-Shirt" }
2016-10-21 05:46:30 +02:00
}
}
};
}
2016-10-31 17:56:24 +01:00
public Order GetActiveOrder(ApplicationUser user)
{
//I think could be a good aproach to work with session to store the active order.
//....
Order activeOrder;
activeOrder = _httpContextAccessor.HttpContext.Session.GetObject<Order>("MyActiveOrder");
if (activeOrder == null)
activeOrder = _orders.Where(x => x.BuyerId.Equals(user.Id) && x.State == OrderState.Active).FirstOrDefault();
_itemsInCart = (activeOrder != null) ? activeOrder.OrderItems.Count() : 0;
return activeOrder;
}
public Order GetOrder(ApplicationUser user, Guid Id)
2016-10-21 05:46:30 +02:00
{
2016-10-31 17:56:24 +01:00
return _orders.Where(x => x.BuyerId.Equals(user.Id) && x.Id.Equals(Id)).FirstOrDefault();
2016-10-21 05:46:30 +02:00
}
2016-10-31 17:56:24 +01:00
public List<Order> GetMyOrders(ApplicationUser user)
2016-10-21 05:46:30 +02:00
{
2016-10-31 17:56:24 +01:00
return _orders.Where(x => x.BuyerId.Equals(user.Id)).ToList();
2016-10-21 05:46:30 +02:00
}
2016-10-31 17:56:24 +01:00
public void AddToCart(ApplicationUser user, OrderItem product)
2016-10-21 05:46:30 +02:00
{
2016-10-31 17:56:24 +01:00
var activeOrder = GetActiveOrder(user);
activeOrder.OrderItems.Add(product);
//CCE: lacks and httpcall to persist in the real back.end service.
_httpContextAccessor.HttpContext.Session.SetObject("MyActiveOrder", activeOrder);
2016-10-21 05:46:30 +02:00
}
}
}