diff --git a/src/ApplicationCore/Entities/Basket.cs b/src/ApplicationCore/Entities/Basket.cs index c0ab5d7..4792463 100644 --- a/src/ApplicationCore/Entities/Basket.cs +++ b/src/ApplicationCore/Entities/Basket.cs @@ -3,26 +3,26 @@ using System.Linq; namespace Microsoft.eShopWeb.ApplicationCore.Entities { - public class Basket : BaseEntity - { - public string BuyerId { get; set; } - private readonly List _items = new List(); - public IEnumerable Items => _items.ToList(); +public class Basket : BaseEntity +{ + public string BuyerId { get; set; } + private readonly List _items = new List(); + public IReadOnlyCollection Items => _items.AsReadOnly(); - public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1) + public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1) + { + if (!Items.Any(i => i.CatalogItemId == catalogItemId)) { - if (!Items.Any(i => i.CatalogItemId == catalogItemId)) + _items.Add(new BasketItem() { - _items.Add(new BasketItem() - { - CatalogItemId = catalogItemId, - Quantity = quantity, - UnitPrice = unitPrice - }); - return; - } - var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId); - existingItem.Quantity += quantity; + CatalogItemId = catalogItemId, + Quantity = quantity, + UnitPrice = unitPrice + }); + return; } + var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId); + existingItem.Quantity += quantity; } } +} diff --git a/src/ApplicationCore/Entities/OrderAggregate/Address.cs b/src/ApplicationCore/Entities/OrderAggregate/Address.cs index 8f395e9..8d214c0 100644 --- a/src/ApplicationCore/Entities/OrderAggregate/Address.cs +++ b/src/ApplicationCore/Entities/OrderAggregate/Address.cs @@ -25,15 +25,5 @@ namespace ApplicationCore.Entities.OrderAggregate Country = country; ZipCode = zipcode; } - - //protected override IEnumerable GetAtomicValues() - //{ - // yield return Street; - // yield return City; - // yield return State; - // yield return Country; - // yield return ZipCode; - //} - } } diff --git a/src/ApplicationCore/Entities/OrderAggregate/Order.cs b/src/ApplicationCore/Entities/OrderAggregate/Order.cs index 55a1807..e99de40 100644 --- a/src/ApplicationCore/Entities/OrderAggregate/Order.cs +++ b/src/ApplicationCore/Entities/OrderAggregate/Order.cs @@ -28,7 +28,7 @@ namespace ApplicationCore.Entities.OrderAggregate // but only through the method Order.AddOrderItem() which includes behavior. private readonly List _orderItems = new List(); - public IReadOnlyCollection OrderItems => _orderItems; + public IReadOnlyCollection OrderItems => _orderItems.AsReadOnly(); // Using List<>.AsReadOnly() // This will create a read only wrapper around the private list so is protected against "external updates". // It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance) diff --git a/src/Infrastructure/Data/CatalogContext.cs b/src/Infrastructure/Data/CatalogContext.cs index 07b0ba0..649e1ce 100644 --- a/src/Infrastructure/Data/CatalogContext.cs +++ b/src/Infrastructure/Data/CatalogContext.cs @@ -1,7 +1,7 @@ -using Microsoft.EntityFrameworkCore; +using ApplicationCore.Entities.OrderAggregate; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.eShopWeb.ApplicationCore.Entities; -using ApplicationCore.Entities.OrderAggregate; namespace Infrastructure.Data { @@ -11,10 +11,7 @@ namespace Infrastructure.Data public CatalogContext(DbContextOptions options) : base(options) { } - //public CatalogContext() - //{ - // // required by migrations - //} + public DbSet Baskets { get; set; } public DbSet CatalogItems { get; set; } public DbSet CatalogBrands { get; set; } @@ -95,10 +92,12 @@ namespace Infrastructure.Data .IsRequired() .HasMaxLength(100); } + private void ConfigureOrder(EntityTypeBuilder builder) { builder.OwnsOne(o => o.ShipToAddress); } + private void ConfigureOrderItem(EntityTypeBuilder builder) { builder.OwnsOne(i => i.ItemOrdered); diff --git a/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetById.cs b/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetById.cs index 3d6b659..f1ac15a 100644 --- a/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetById.cs +++ b/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetById.cs @@ -1,8 +1,5 @@ -using ApplicationCore.Entities.OrderAggregate; -using Infrastructure.Data; +using Infrastructure.Data; using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; using System.Linq; using UnitTests.Builders; using Xunit;