2017-01-25 17:10:08 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
|
|
|
|
public class Order
|
2017-01-25 17:10:08 +01:00
|
|
|
|
: Entity
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2017-01-25 17:10:08 +01:00
|
|
|
|
private string _street;
|
|
|
|
|
private string _city;
|
|
|
|
|
private string _state;
|
|
|
|
|
private string _country;
|
|
|
|
|
private string _zipCode;
|
|
|
|
|
private DateTime _orderDate;
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
public Buyer Buyer { get; private set; }
|
|
|
|
|
int _buyerId;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
public OrderStatus OrderStatus { get; private set; }
|
|
|
|
|
int _orderStatusId;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
HashSet<OrderItem> _orderItems;
|
|
|
|
|
public IEnumerable<OrderItem> OrderItems => _orderItems.ToList().AsEnumerable();
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2017-01-27 11:38:23 +01:00
|
|
|
|
public PaymentMethod PaymentMethod { get; private set; }
|
|
|
|
|
int _paymentMethodId;
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2016-11-21 12:41:36 +01:00
|
|
|
|
protected Order() { }
|
2016-11-24 14:59:25 +01:00
|
|
|
|
|
2017-01-27 11:38:23 +01:00
|
|
|
|
public Order(int buyerId, int paymentMethodId, Address address)
|
2016-11-24 14:59:25 +01:00
|
|
|
|
{
|
2017-01-25 17:10:08 +01:00
|
|
|
|
|
|
|
|
|
_buyerId = buyerId;
|
2017-01-27 11:38:23 +01:00
|
|
|
|
_paymentMethodId = paymentMethodId;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
_orderStatusId = OrderStatus.InProcess.Id;
|
|
|
|
|
_orderDate = DateTime.UtcNow;
|
|
|
|
|
_street = address.Street;
|
|
|
|
|
_city = address.City;
|
|
|
|
|
_state = address.State;
|
|
|
|
|
_country = address.Country;
|
|
|
|
|
_zipCode = address.ZipCode;
|
|
|
|
|
|
|
|
|
|
_orderItems = new HashSet<OrderItem>();
|
2016-11-24 14:59:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
|
|
|
|
|
public void AddOrderItem(int productId, string productName, decimal unitPrice, decimal discount, int units = 1)
|
2016-11-24 14:59:25 +01:00
|
|
|
|
{
|
2017-01-25 17:10:08 +01:00
|
|
|
|
var existingOrderForProduct = _orderItems.Where(o => o.ProductId == productId)
|
|
|
|
|
.SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
if (existingOrderForProduct != null)
|
2016-11-24 14:59:25 +01:00
|
|
|
|
{
|
2017-01-25 17:10:08 +01:00
|
|
|
|
//if previous line exist modify it with higher discount and units..
|
|
|
|
|
|
|
|
|
|
if (discount > existingOrderForProduct.GetCurrentDiscount())
|
|
|
|
|
{
|
|
|
|
|
existingOrderForProduct.SetNewDiscount(discount);
|
|
|
|
|
existingOrderForProduct.AddUnits(units);
|
|
|
|
|
}
|
2016-11-24 14:59:25 +01:00
|
|
|
|
}
|
2017-01-25 17:10:08 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//add validated new order item
|
2016-11-24 14:59:25 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
var orderItem = new OrderItem(productId, productName, unitPrice, discount, units);
|
2016-12-14 18:23:57 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
_orderItems.Add(orderItem);
|
|
|
|
|
}
|
2016-12-14 18:23:57 +01:00
|
|
|
|
}
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|