Feature/respect encapsulation (#349)
* resolve osbsolete method * put all properties as private, align unit test * fix version of version in MD, add instruction to install ef tool * fix url stored
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
{
|
||||
// This can easily be modified to be BaseEntity<T> and public T Id to support different key types.
|
||||
// Using non-generic integer types for simplicity and to ease caching logic
|
||||
public class BaseEntity
|
||||
public abstract class BaseEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public virtual int Id { get; protected set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,29 +6,34 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate
|
||||
{
|
||||
public class Basket : BaseEntity, IAggregateRoot
|
||||
{
|
||||
public string BuyerId { get; set; }
|
||||
public string BuyerId { get; private set; }
|
||||
private readonly List<BasketItem> _items = new List<BasketItem>();
|
||||
public IReadOnlyCollection<BasketItem> Items => _items.AsReadOnly();
|
||||
|
||||
public Basket(string buyerId)
|
||||
{
|
||||
BuyerId = buyerId;
|
||||
}
|
||||
|
||||
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1)
|
||||
{
|
||||
if (!Items.Any(i => i.CatalogItemId == catalogItemId))
|
||||
{
|
||||
_items.Add(new BasketItem()
|
||||
{
|
||||
CatalogItemId = catalogItemId,
|
||||
Quantity = quantity,
|
||||
UnitPrice = unitPrice
|
||||
});
|
||||
_items.Add(new BasketItem(catalogItemId, quantity, unitPrice));
|
||||
return;
|
||||
}
|
||||
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
|
||||
existingItem.Quantity += quantity;
|
||||
existingItem.AddQuantity(quantity);
|
||||
}
|
||||
|
||||
public void RemoveEmptyItems()
|
||||
{
|
||||
_items.RemoveAll(i => i.Quantity == 0);
|
||||
}
|
||||
|
||||
public void SetNewBuyerId(string buyerId)
|
||||
{
|
||||
BuyerId = buyerId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,27 @@
|
||||
{
|
||||
public class BasketItem : BaseEntity
|
||||
{
|
||||
public decimal UnitPrice { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
public int CatalogItemId { get; set; }
|
||||
|
||||
public decimal UnitPrice { get; private set; }
|
||||
public int Quantity { get; private set; }
|
||||
public int CatalogItemId { get; private set; }
|
||||
public int BasketId { get; private set; }
|
||||
|
||||
public BasketItem(int catalogItemId, int quantity, decimal unitPrice)
|
||||
{
|
||||
CatalogItemId = catalogItemId;
|
||||
Quantity = quantity;
|
||||
UnitPrice = unitPrice;
|
||||
}
|
||||
|
||||
public void AddQuantity(int quantity)
|
||||
{
|
||||
Quantity += quantity;
|
||||
}
|
||||
|
||||
public void SetNewQuantity(int quantity)
|
||||
{
|
||||
Quantity = quantity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
{
|
||||
public class PaymentMethod : BaseEntity
|
||||
{
|
||||
public string Alias { get; set; }
|
||||
public string CardId { get; set; } // actual card data must be stored in a PCI compliant system, like Stripe
|
||||
public string Last4 { get; set; }
|
||||
public string Alias { get; private set; }
|
||||
public string CardId { get; private set; } // actual card data must be stored in a PCI compliant system, like Stripe
|
||||
public string Last4 { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities
|
||||
{
|
||||
public class CatalogBrand : BaseEntity, IAggregateRoot
|
||||
{
|
||||
public string Brand { get; set; }
|
||||
public string Brand { get; private set; }
|
||||
public CatalogBrand(string brand)
|
||||
{
|
||||
Brand = brand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,34 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Ardalis.GuardClauses;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
|
||||
namespace Microsoft.eShopWeb.ApplicationCore.Entities
|
||||
{
|
||||
public class CatalogItem : BaseEntity, IAggregateRoot
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public string PictureUri { get; set; }
|
||||
public int CatalogTypeId { get; set; }
|
||||
public CatalogType CatalogType { get; set; }
|
||||
public int CatalogBrandId { get; set; }
|
||||
public CatalogBrand CatalogBrand { get; set; }
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public decimal Price { get; private set; }
|
||||
public string PictureUri { get; private set; }
|
||||
public int CatalogTypeId { get; private set; }
|
||||
public CatalogType CatalogType { get; private set; }
|
||||
public int CatalogBrandId { get; private set; }
|
||||
public CatalogBrand CatalogBrand { get; private set; }
|
||||
|
||||
public CatalogItem(int catalogTypeId, int catalogBrandId, string description, string name, decimal price, string pictureUri)
|
||||
{
|
||||
CatalogTypeId = catalogTypeId;
|
||||
CatalogBrandId = catalogBrandId;
|
||||
Description = description;
|
||||
Name = name;
|
||||
Price = price;
|
||||
PictureUri = pictureUri;
|
||||
}
|
||||
|
||||
public void Update(string name, decimal price)
|
||||
{
|
||||
Guard.Against.NullOrEmpty(name, nameof(name));
|
||||
Name = name;
|
||||
Price = price;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities
|
||||
{
|
||||
public class CatalogType : BaseEntity, IAggregateRoot
|
||||
{
|
||||
public string Type { get; set; }
|
||||
public string Type { get; private set; }
|
||||
public CatalogType(string type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate
|
||||
{
|
||||
public class Address // ValueObject
|
||||
{
|
||||
public String Street { get; private set; }
|
||||
public string Street { get; private set; }
|
||||
|
||||
public String City { get; private set; }
|
||||
public string City { get; private set; }
|
||||
|
||||
public String State { get; private set; }
|
||||
public string State { get; private set; }
|
||||
|
||||
public String Country { get; private set; }
|
||||
public string Country { get; private set; }
|
||||
|
||||
public String ZipCode { get; private set; }
|
||||
public string ZipCode { get; private set; }
|
||||
|
||||
private Address() { }
|
||||
|
||||
|
||||
@@ -59,8 +59,8 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
{
|
||||
if (quantities.TryGetValue(item.Id.ToString(), out var quantity))
|
||||
{
|
||||
if(_logger != null) _logger.LogInformation($"Updating quantity of item ID:{item.Id} to {quantity}.");
|
||||
item.Quantity = quantity;
|
||||
if (_logger != null) _logger.LogInformation($"Updating quantity of item ID:{item.Id} to {quantity}.");
|
||||
item.SetNewQuantity(quantity);
|
||||
}
|
||||
}
|
||||
basket.RemoveEmptyItems();
|
||||
@@ -74,7 +74,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
var basketSpec = new BasketWithItemsSpecification(anonymousId);
|
||||
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();
|
||||
if (basket == null) return;
|
||||
basket.BuyerId = userName;
|
||||
basket.SetNewBuyerId(userName);
|
||||
await _basketRepository.UpdateAsync(basket);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,17 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
public class OrderService : IOrderService
|
||||
{
|
||||
private readonly IAsyncRepository<Order> _orderRepository;
|
||||
private readonly IUriComposer _uriComposer;
|
||||
private readonly IAsyncRepository<Basket> _basketRepository;
|
||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
||||
|
||||
public OrderService(IAsyncRepository<Basket> basketRepository,
|
||||
IAsyncRepository<CatalogItem> itemRepository,
|
||||
IAsyncRepository<Order> orderRepository)
|
||||
IAsyncRepository<Order> orderRepository,
|
||||
IUriComposer uriComposer)
|
||||
{
|
||||
_orderRepository = orderRepository;
|
||||
_uriComposer = uriComposer;
|
||||
_basketRepository = basketRepository;
|
||||
_itemRepository = itemRepository;
|
||||
}
|
||||
@@ -31,7 +34,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
||||
foreach (var item in basket.Items)
|
||||
{
|
||||
var catalogItem = await _itemRepository.GetByIdAsync(item.CatalogItemId);
|
||||
var itemOrdered = new CatalogItemOrdered(catalogItem.Id, catalogItem.Name, catalogItem.PictureUri);
|
||||
var itemOrdered = new CatalogItemOrdered(catalogItem.Id, catalogItem.Name,_uriComposer.ComposePicUri(catalogItem.PictureUri));
|
||||
var orderItem = new OrderItem(itemOrdered, item.UnitPrice, item.Quantity);
|
||||
items.Add(orderItem);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user