diff --git a/eShopOnContainers-ServicesAndWebApps.sln b/eShopOnContainers-ServicesAndWebApps.sln index 57cd2f366..97244df31 100644 --- a/eShopOnContainers-ServicesAndWebApps.sln +++ b/eShopOnContainers-ServicesAndWebApps.sln @@ -39,12 +39,12 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "eShopOnContainers.WebMVC", EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "eShopOnContainers.WebSPA", "src\Web\WebSPA\eShopOnContainers.WebSPA\eShopOnContainers.WebSPA.xproj", "{9842DB3A-1391-48C7-A49C-2FABD0A18AC2}" EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Ordering.Application", "src\Services\Ordering\Ordering.Application\Ordering.Application.xproj", "{4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}" -EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Ordering.Infrastructure", "src\Services\Ordering\Ordering.Infrastructure\Ordering.Infrastructure.xproj", "{95F1F07C-4D92-4742-BD07-E5B805AAB651}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FunctionalTests", "test\Services\FunctionalTests\FunctionalTests.xproj", "{621E7211-58D0-45FD-9600-1CB490BD930E}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "UnitTest", "test\Services\UnitTest\UnitTest.xproj", "{7796F5D8-31FC-45A4-B673-19DE5BA194CF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Ad-Hoc|Any CPU = Ad-Hoc|Any CPU @@ -251,54 +251,6 @@ Global {9842DB3A-1391-48C7-A49C-2FABD0A18AC2}.Release|x64.Build.0 = Release|Any CPU {9842DB3A-1391-48C7-A49C-2FABD0A18AC2}.Release|x86.ActiveCfg = Release|Any CPU {9842DB3A-1391-48C7-A49C-2FABD0A18AC2}.Release|x86.Build.0 = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|x64.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Ad-Hoc|x86.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|Any CPU.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|ARM.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|ARM.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|iPhone.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|iPhone.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|x64.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|x64.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|x86.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.AppStore|x86.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|ARM.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|ARM.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|iPhone.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|iPhone.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|x64.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|x64.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|x86.ActiveCfg = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Debug|x86.Build.0 = Debug|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|Any CPU.Build.0 = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|ARM.ActiveCfg = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|ARM.Build.0 = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|iPhone.ActiveCfg = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|iPhone.Build.0 = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|iPhoneSimulator.Build.0 = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|x64.ActiveCfg = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|x64.Build.0 = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|x86.ActiveCfg = Release|Any CPU - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7}.Release|x86.Build.0 = Release|Any CPU {95F1F07C-4D92-4742-BD07-E5B805AAB651}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU {95F1F07C-4D92-4742-BD07-E5B805AAB651}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU {95F1F07C-4D92-4742-BD07-E5B805AAB651}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU @@ -395,6 +347,54 @@ Global {621E7211-58D0-45FD-9600-1CB490BD930E}.Release|x64.Build.0 = Release|Any CPU {621E7211-58D0-45FD-9600-1CB490BD930E}.Release|x86.ActiveCfg = Release|Any CPU {621E7211-58D0-45FD-9600-1CB490BD930E}.Release|x86.Build.0 = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|x64.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Ad-Hoc|x86.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|Any CPU.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|ARM.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|ARM.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|iPhone.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|iPhone.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|x64.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|x64.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|x86.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.AppStore|x86.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|ARM.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|ARM.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|iPhone.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|x64.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|x64.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|x86.ActiveCfg = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Debug|x86.Build.0 = Debug|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|Any CPU.Build.0 = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|ARM.ActiveCfg = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|ARM.Build.0 = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|iPhone.ActiveCfg = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|iPhone.Build.0 = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|x64.ActiveCfg = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|x64.Build.0 = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|x86.ActiveCfg = Release|Any CPU + {7796F5D8-31FC-45A4-B673-19DE5BA194CF}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -412,8 +412,8 @@ Global {EF0337F2-ED00-4643-89FD-EE10863F1870} = {A857AD10-40FF-4303-BEC2-FF1C58D5735E} {F0333D8E-0B27-42B7-B2C6-78F3657624E2} = {E279BF0F-7F66-4F3A-A3AB-2CDA66C1CD04} {9842DB3A-1391-48C7-A49C-2FABD0A18AC2} = {E279BF0F-7F66-4F3A-A3AB-2CDA66C1CD04} - {4193CAA3-A1C3-4818-A06F-A2D85FDE77E7} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B} {95F1F07C-4D92-4742-BD07-E5B805AAB651} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B} {621E7211-58D0-45FD-9600-1CB490BD930E} = {EF0337F2-ED00-4643-89FD-EE10863F1870} + {7796F5D8-31FC-45A4-B673-19DE5BA194CF} = {EF0337F2-ED00-4643-89FD-EE10863F1870} EndGlobalSection EndGlobal diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/NewOrderRequest.cs b/src/Services/Ordering/Ordering.API/Application/Commands/NewOrderRequest.cs deleted file mode 100644 index 23fab0ad6..000000000 --- a/src/Services/Ordering/Ordering.API/Application/Commands/NewOrderRequest.cs +++ /dev/null @@ -1,48 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Commands -{ - using System; - using MediatR; - using Domain; - using System.Collections; - using System.Collections.Generic; - - public class NewOrderRequest - :IAsyncRequest - { - - private readonly List _orderItems; - public string City { get; set; } - - public string Street { get; set; } - - public string State { get; set; } - - public string Country { get; set; } - - public string ZipCode { get; set; } - - public string CardNumber { get; set; } - - public string CardHolderName { get; set; } - - public DateTime CardExpiration { get; set; } - - public string CardSecurityNumber { get; set; } - - public int CardTypeId { get; set; } - - public string Buyer { get; set; } - - public IEnumerable OrderItems => _orderItems; - - public void AddOrderItem(OrderItem item) - { - _orderItems.Add(item); - } - - public NewOrderRequest() - { - _orderItems = new List(); - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/NewOrderRequestHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/NewOrderRequestHandler.cs deleted file mode 100644 index ac5c90b86..000000000 --- a/src/Services/Ordering/Ordering.API/Application/Commands/NewOrderRequestHandler.cs +++ /dev/null @@ -1,118 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Commands -{ - using Domain.Repositories; - using MediatR; - using System.Linq; - using System; - using System.Threading.Tasks; - using Domain; - - public class NewOrderRequestHandler - : IAsyncRequestHandler - { - private readonly IBuyerRepository _buyerRepository; - private readonly IOrderRepository _orderRepository; - - public NewOrderRequestHandler(IBuyerRepository buyerRepository,IOrderRepository orderRepository) - { - if (buyerRepository == null) - { - throw new ArgumentNullException(nameof(buyerRepository)); - } - - if (orderRepository == null) - { - throw new ArgumentNullException(nameof(orderRepository)); - } - - _buyerRepository = buyerRepository; - _orderRepository = orderRepository; - } - public async Task Handle(NewOrderRequest message) - { - //find buyer/payment or add a new one buyer/payment - - var buyer = await _buyerRepository.FindAsync(message.Buyer); - - if (buyer == null) - { - buyer = CreateBuyer(message); - } - - var payment = GetExistingPaymentOrAddANewOne(buyer, message); - - await _buyerRepository.UnitOfWork - .SaveChangesAsync(); - - //create order for buyer and payment method - - var order = CreateOrder(buyer.Id, payment.Id, 0); - order.SetAddress( new Address() - { - City = message.City, - State = message.State, - Street = message.Street, - ZipCode = message.ZipCode - }); - - foreach (var item in message.OrderItems) - { - order.AddOrderItem(item); - } - - _orderRepository.Add(order); - - var result = await _orderRepository.UnitOfWork - .SaveChangesAsync(); - - return result > 0; - } - - - - Payment GetExistingPaymentOrAddANewOne(Buyer buyer, NewOrderRequest message) - { - Payment payment = PaymentAlreadyExist(buyer, message); - - if (payment == null) - { - payment = CreatePayment(message); - buyer.Payments.Add(payment); - } - - return payment; - - } - - Payment PaymentAlreadyExist(Domain.Buyer buyer, NewOrderRequest message) - { - return buyer.Payments - .SingleOrDefault(p => - { - return p.CardHolderName == message.CardHolderName - && - p.CardNumber == message.CardNumber - && - p.Expiration == message.CardExpiration - && - p.SecurityNumber == message.CardSecurityNumber; - }); - } - - Buyer CreateBuyer(NewOrderRequest message) - { - return _buyerRepository.Add( - new Buyer(message.Buyer)); - } - - Order CreateOrder(int buyerId, int paymentId, int addressId) - { - return new Order(buyerId, paymentId); - } - - Payment CreatePayment(NewOrderRequest message) - { - return new Payment(message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration, message.CardTypeId); - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs index 7ca26f9ba..43c170427 100644 --- a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs +++ b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs @@ -43,7 +43,7 @@ [Route("new")] [HttpPost] - public async Task AddOrder([FromBody]NewOrderRequest order) + public async Task AddOrder([FromBody]NewOrderCommand order) { if (order.CardTypeId == 0) order.CardTypeId = 1; diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs index 0163ccca4..2af7a286a 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs @@ -16,7 +16,7 @@ builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly) .AsImplementedInterfaces(); - builder.RegisterAssemblyTypes(typeof(NewOrderRequest).GetTypeInfo().Assembly) + builder.RegisterAssemblyTypes(typeof(NewOrderCommand).GetTypeInfo().Assembly) .As(o => o.GetInterfaces() .Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>))) .Select(i => new KeyedService("IAsyncRequestHandler", i))); diff --git a/src/Services/Ordering/Ordering.Application/Commands/NewOrderREquestHandler.cs b/src/Services/Ordering/Ordering.Application/Commands/NewOrderREquestHandler.cs deleted file mode 100644 index 186301dc5..000000000 --- a/src/Services/Ordering/Ordering.Application/Commands/NewOrderREquestHandler.cs +++ /dev/null @@ -1,118 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Commands -{ - using Domain.Repositories; - using MediatR; - using System.Linq; - using System; - using System.Threading.Tasks; - using Domain; - - public class NewOrderRequestHandler - : IAsyncRequestHandler - { - private readonly IBuyerRepository _buyerRepository; - private readonly IOrderRepository _orderRepository; - - public NewOrderRequestHandler(IBuyerRepository buyerRepository,IOrderRepository orderRepository) - { - if (buyerRepository == null) - { - throw new ArgumentNullException(nameof(buyerRepository)); - } - - if (orderRepository == null) - { - throw new ArgumentNullException(nameof(orderRepository)); - } - - _buyerRepository = buyerRepository; - _orderRepository = orderRepository; - } - public async Task Handle(NewOrderRequest message) - { - //find buyer/payment or add a new one buyer/payment - - var buyer = await _buyerRepository.FindAsync(message.Buyer); - - if (buyer == null) - { - buyer = CreateBuyer(message); - } - - var payment = GetExistingPaymentOrAddANewOne(buyer, message); - - await _buyerRepository.UnitOfWork - .SaveChangesAsync(); - - //create order for buyer and payment method - - var order = CreateOrder(buyer.Id, payment.Id, 0); - order.SetAddress( new Address() - { - City = message.City, - State = message.State, - Street = message.Street, - ZipCode = message.ZipCode - }); - - foreach (var item in message.OrderItems) - { - order.AddOrderItem(item); - } - - _orderRepository.Add(order); - - var result = await _orderRepository.UnitOfWork - .SaveChangesAsync(); - - return result > 0; - } - - - - Payment GetExistingPaymentOrAddANewOne(Buyer buyer, NewOrderRequest message) - { - Payment payment = PaymentAlreadyExist(buyer, message); - - if (payment == null) - { - payment = CreatePayment(message); - buyer.Payments.Add(payment); - } - - return payment; - - } - - Payment PaymentAlreadyExist(Domain.Buyer buyer, NewOrderRequest message) - { - return buyer.Payments - .SingleOrDefault(p => - { - return p.CardHolderName == message.CardHolderName - && - p.CardNumber == message.CardNumber - && - p.Expiration == message.CardExpiration - && - p.SecurityNumber == message.CardSecurityNumber; - }); - } - - Buyer CreateBuyer(NewOrderRequest message) - { - return _buyerRepository.Add( - new Buyer(message.Buyer)); - } - - Order CreateOrder(int buyerId, int paymentId, int addressId) - { - return new Order(buyerId, paymentId); - } - - Payment CreatePayment(NewOrderRequest message) - { - return new Payment(message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration, message.CardTypeId); - } - } -} diff --git a/src/Services/Ordering/Ordering.Application/Commands/NewOrderRequest.cs b/src/Services/Ordering/Ordering.Application/Commands/NewOrderRequest.cs deleted file mode 100644 index 113aa552e..000000000 --- a/src/Services/Ordering/Ordering.Application/Commands/NewOrderRequest.cs +++ /dev/null @@ -1,48 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Commands -{ - using System; - using MediatR; - using Domain; - using System.Collections; - using System.Collections.Generic; - - public class NewOrderRequest - :IAsyncRequest - { - - private readonly List _orderItems; - public string City { get; set; } - - public string Street { get; set; } - - public string State { get; set; } - - public string Country { get; set; } - - public string ZipCode { get; set; } - - public string CardNumber { get; set; } - - public string CardHolderName { get; set; } - - public DateTime CardExpiration { get; set; } - - public string CardSecurityNumber { get; set; } - - public int CardTypeId { get; set; } - - public string Buyer { get; set; } - - public IEnumerable OrderItems => _orderItems; - - public void AddOrderItem(OrderItem item) - { - _orderItems.Add(item); - } - - public NewOrderRequest() - { - _orderItems = new List(); - } - } -} diff --git a/src/Services/Ordering/Ordering.Application/Decorators/LogDecorator.cs b/src/Services/Ordering/Ordering.Application/Decorators/LogDecorator.cs deleted file mode 100644 index bc095a81e..000000000 --- a/src/Services/Ordering/Ordering.Application/Decorators/LogDecorator.cs +++ /dev/null @@ -1,34 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Decorators -{ - using Extensions.Logging; - using MediatR; - using System.Threading.Tasks; - - public class LogDecorator - : IAsyncRequestHandler - where TRequest : IAsyncRequest - { - private readonly IAsyncRequestHandler _inner; - private readonly ILogger> _logger; - - - public LogDecorator( - IAsyncRequestHandler inner, - ILogger> logger) - { - _inner = inner; - _logger = logger; - } - - public async Task Handle(TRequest message) - { - _logger.LogInformation($"Executing command {_inner.GetType().FullName}"); - - var response = await _inner.Handle(message); - - _logger.LogInformation($"Succedded executed command {_inner.GetType().FullName}"); - - return response; - } - } -} diff --git a/src/Services/Ordering/Ordering.Application/Ordering.Application.xproj b/src/Services/Ordering/Ordering.Application/Ordering.Application.xproj deleted file mode 100644 index 2c6ff9897..000000000 --- a/src/Services/Ordering/Ordering.Application/Ordering.Application.xproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - 4193caa3-a1c3-4818-a06f-a2d85fde77e7 - Microsoft.eShopOnContainers.Services.Ordering.Application - .\obj - .\bin\ - v4.5.1 - - - 2.0 - - - \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.Application/Properties/AssemblyInfo.cs b/src/Services/Ordering/Ordering.Application/Properties/AssemblyInfo.cs deleted file mode 100644 index a38e1f35b..000000000 --- a/src/Services/Ordering/Ordering.Application/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Ordering.Application")] -[assembly: AssemblyTrademark("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("4193caa3-a1c3-4818-a06f-a2d85fde77e7")] diff --git a/src/Services/Ordering/Ordering.Application/Queries/IOrderQueries.cs b/src/Services/Ordering/Ordering.Application/Queries/IOrderQueries.cs deleted file mode 100644 index a011f2359..000000000 --- a/src/Services/Ordering/Ordering.Application/Queries/IOrderQueries.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Queries -{ - using System.Threading.Tasks; - - public interface IOrderQueries - { - Task GetOrder(int id); - - Task GetOrders(); - - Task GetCardTypes(); - } -} diff --git a/src/Services/Ordering/Ordering.Application/Queries/OrderQueries.cs b/src/Services/Ordering/Ordering.Application/Queries/OrderQueries.cs deleted file mode 100644 index 80cf57d68..000000000 --- a/src/Services/Ordering/Ordering.Application/Queries/OrderQueries.cs +++ /dev/null @@ -1,128 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Queries -{ - using Dapper; - using Microsoft.Extensions.Configuration; - using System.Data.SqlClient; - using System.Threading.Tasks; - using System; - using System.Dynamic; - using System.Collections.Generic; - using System.Linq; - - public class OrderQueries - : IOrderQueries - { - private string _connectionString = string.Empty; - - public OrderQueries(IConfiguration configuration) - { - _connectionString = configuration["ConnectionString"]; - } - - - public async Task GetOrder(int id) - { - using (var connection = new SqlConnection(_connectionString)) - { - connection.Open(); - - var result = await connection.QueryAsync( - @"select o.[Id] as ordernumber,o.OrderDate as date, os.Name as status, - oi.ProductName as productname, oi.Units as units, oi.UnitPrice as unitprice, oi.PictureUrl as pictureurl, - oa.Street as street, oa.City as city, oa.Country as country, oa.State as state, oa.ZipCode as zipcode - FROM ordering.Orders o - LEFT JOIN ordering.Orderitems oi ON o.Id = oi.orderid - LEFT JOIN ordering.orderstatus os on o.StatusId = os.Id - LEFT JOIN ordering.address oa on o.ShippingAddressId = oa.Id - WHERE o.Id=@id" - , new { id } - ); - - if (result.AsList().Count == 0) - throw new KeyNotFoundException(); - - return MapOrderItems(result); - } - } - - public async Task GetOrders() - { - using (var connection = new SqlConnection(_connectionString)) - { - connection.Open(); - - return await connection.QueryAsync(@"SELECT o.[Id] as ordernumber,o.[OrderDate] as [date],os.[Name] as [status],SUM(oi.units*oi.unitprice) as total - FROM [ordering].[Orders] o - LEFT JOIN[ordering].[orderitems] oi ON o.Id = oi.orderid - LEFT JOIN[ordering].[orderstatus] os on o.StatusId = os.Id - GROUP BY o.[Id], o.[OrderDate], os.[Name]"); - } - } - - public async Task GetCardTypes() - { - using (var connection = new SqlConnection(_connectionString)) - { - connection.Open(); - - return await connection.QueryAsync("SELECT * FROM ordering.cardtypes"); - } - } - - private dynamic MapOrderItems(dynamic result) - { - dynamic order = new ExpandoObject(); - - order.ordernumber = result[0].ordernumber; - order.date = result[0].date; - order.status = result[0].status; - order.street = result[0].street; - order.city = result[0].city; - order.zipcode = result[0].zipcode; - order.country = result[0].country; - - order.orderitems = new List(); - order.total = 0; - - foreach (dynamic item in result) - { - dynamic orderitem = new ExpandoObject(); - orderitem.productname = item.productname; - orderitem.units = item.units; - orderitem.unitprice = item.unitprice; - orderitem.pictureurl = item.pictureurl; - - order.total += item.units * item.unitprice; - order.orderitems.Add(orderitem); - } - - return order; - } - - //TODO/CCE: try to use this method instead actual. - //private object MapOrderItems(dynamic result) - //{ - // IEnumerable items = (result as System.Collections.IEnumerable).Cast(); - // var order = new - // { - // ordernumber = result[0].ordernumbe, - // date = result[0].date, - // status = result[0].status, - // street = result[0].street, - // city = result[0].city, - // zipcode = result[0].zipcode, - // country = result[0].country, - // total = items.Select(r => (int)r.units * (int)r.unitprice).Sum(), - // orderItems = items.Select(r => new - // { - // productname = r.productname, - // units = r.units, - // unitprice = r.unitprice, - // pictureurl = r.pictureurl - // }) - // }; - - // return order; - //} - } -} diff --git a/src/Services/Ordering/Ordering.Application/project.json b/src/Services/Ordering/Ordering.Application/project.json deleted file mode 100644 index 158ae13d8..000000000 --- a/src/Services/Ordering/Ordering.Application/project.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "version": "1.0.0-*", - - "dependencies": { - "NETStandard.Library": "1.6.0", - "MediatR": "2.1.0", - "Dapper": "1.50.2", - "System.Dynamic.Runtime": "4.0.11", - "Microsoft.CSharp": "4.0.1", - "Microsoft.Extensions.Configuration": "1.0.0", - "System.Data.SqlClient": "4.1.0", - "Microsoft.Extensions.Logging.Abstractions": "1.0.0", - "Ordering.Domain": "1.0.0-*" - }, - - "frameworks": { - "netstandard1.6": { - "imports": "dnxcore50" - } - } -} diff --git a/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs b/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs index 9be6d29d5..67929d4b0 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs @@ -39,13 +39,8 @@ modelBuilder.Entity(ConfigureCardTypes); modelBuilder.Entity(ConfigureOrderStatus); modelBuilder.Entity
(ConfigureAddress); - - modelBuilder.Entity
() - .ToTable("address", DEFAULT_SCHEMA); } - - void ConfigureBuyer(EntityTypeBuilder buyerConfiguration) { buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA); diff --git a/test/Services/UnitTest/Ordering/Application/NewOrderCommandHandlerTest.cs b/test/Services/UnitTest/Ordering/Application/NewOrderCommandHandlerTest.cs index c728ed70f..1e50e639d 100644 --- a/test/Services/UnitTest/Ordering/Application/NewOrderCommandHandlerTest.cs +++ b/test/Services/UnitTest/Ordering/Application/NewOrderCommandHandlerTest.cs @@ -1,18 +1,15 @@ -using MediatR; -using Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Commands; -using Microsoft.eShopOnContainers.Services.Ordering.Domain; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.Repositories; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork; -using Moq; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Xunit; - -namespace UnitTest.Ordering.Application +namespace UnitTest.Ordering.Application { + using Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Commands; + using Microsoft.eShopOnContainers.Services.Ordering.Domain; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.Repositories; + using Moq; + using System; + using System.Threading; + using System.Threading.Tasks; + using Xunit; + + public class NewOrderRequestHandlerTest { private readonly Mock _buyerRepositoryMock; @@ -20,25 +17,29 @@ namespace UnitTest.Ordering.Application public NewOrderRequestHandlerTest() { - //Mocks; + _buyerRepositoryMock = new Mock(); _orderRepositoryMock = new Mock(); } [Fact] - public async Task Handle_ReturnsTrue_WhenOrderIsPersistedSuccesfully() + public async Task Handle_returns_true_when_order_is_persisted_succesfully() { // Arrange _buyerRepositoryMock.Setup(buyerRepo => buyerRepo.FindAsync(FakeOrderRequestWithBuyer().Buyer)) .Returns(Task.FromResult(FakeBuyer())); + _buyerRepositoryMock.Setup(buyerRepo => buyerRepo.UnitOfWork.SaveChangesAsync(default(CancellationToken))) .Returns(Task.FromResult(1)); - _orderRepositoryMock.Setup(or => or.Add(FakeOrder())).Returns(FakeOrder()); - _orderRepositoryMock.Setup(or => or.UnitOfWork.SaveChangesAsync(default(CancellationToken))).Returns(Task.FromResult(1)); + _orderRepositoryMock.Setup(or => or.Add(FakeOrder())) + .Returns(FakeOrder()); + + _orderRepositoryMock.Setup(or => or.UnitOfWork.SaveChangesAsync(default(CancellationToken))) + .Returns(Task.FromResult(1)); //Act - var handler = new NewOrderRequestHandler(_buyerRepositoryMock.Object, _orderRepositoryMock.Object); + var handler = new NewOrderCommandHandler(_buyerRepositoryMock.Object, _orderRepositoryMock.Object); var result = await handler.Handle(FakeOrderRequestWithBuyer()); //Assert @@ -46,18 +47,20 @@ namespace UnitTest.Ordering.Application } [Fact] - public async Task Handle_ReturnsFalse_WhenOrderIsNotPersisted() + public async Task Handle_return_false_if_order_is_not_persisted() { _buyerRepositoryMock.Setup(buyerRepo => buyerRepo.FindAsync(FakeOrderRequestWithBuyer().Buyer)) .Returns(Task.FromResult(FakeBuyer())); + _buyerRepositoryMock.Setup(buyerRepo => buyerRepo.UnitOfWork.SaveChangesAsync(default(CancellationToken))) .Returns(Task.FromResult(1)); _orderRepositoryMock.Setup(or => or.Add(FakeOrder())).Returns(FakeOrder()); - _orderRepositoryMock.Setup(or => or.UnitOfWork.SaveChangesAsync(default(CancellationToken))).Returns(Task.FromResult(0)); + _orderRepositoryMock.Setup(or => or.UnitOfWork.SaveChangesAsync(default(CancellationToken))) + .Returns(Task.FromResult(0)); //Act - var handler = new NewOrderRequestHandler(_buyerRepositoryMock.Object, _orderRepositoryMock.Object); + var handler = new NewOrderCommandHandler(_buyerRepositoryMock.Object, _orderRepositoryMock.Object); var result = await handler.Handle(FakeOrderRequestWithBuyer()); //Assert @@ -77,9 +80,9 @@ namespace UnitTest.Ordering.Application }; } - private NewOrderRequest FakeOrderRequestWithBuyer() + private NewOrderCommand FakeOrderRequestWithBuyer() { - return new NewOrderRequest + return new NewOrderCommand { Buyer = "1234", CardNumber = "1234", diff --git a/test/Services/UnitTest/Ordering/Controllers/OrderControllerTest.cs b/test/Services/UnitTest/Ordering/Controllers/OrderControllerTest.cs deleted file mode 100644 index b7d9a170e..000000000 --- a/test/Services/UnitTest/Ordering/Controllers/OrderControllerTest.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using Xunit; -using System.Threading.Tasks; -using Moq; -using MediatR; -using Microsoft.eShopOnContainers.Services.Ordering.API.Controllers; -using Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Commands; -using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services; -using Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Queries; -using System.Collections.Generic; - -namespace UnitTest.Ordering.Controllers -{ - public class OrderControllerTest - { - private readonly Mock _mediatorMock; - private readonly Mock _identityMock; - private readonly Mock _queriesMock; - private readonly string _userIdentity; - - public OrderControllerTest() - { - //Mocks; - _mediatorMock = new Mock(); - _identityMock = new Mock(); - _queriesMock = new Mock(); - _userIdentity = Guid.NewGuid().ToString(); - } - - [Fact] - public async Task AddOrder_ReturnsBadRequestResult_WhenPersitenceOperationFails() - { - // Arrange - var orderRequest = new object() as IAsyncRequest; - - _mediatorMock.Setup(mediator => mediator.SendAsync(OrderFakeNotExpired())) - .Returns(Task.FromResult(false)); - - _identityMock.Setup(identity => identity.GetUserIdentity()) - .Returns(Guid.NewGuid().ToString()); - - var controller = new OrdersController(_mediatorMock.Object, _queriesMock.Object, _identityMock.Object); - - // Act - var badRequestResult = await controller.AddOrder(OrderFakeNotExpired()); - - // Assert - Assert.IsType(badRequestResult); - } - - [Fact] - public async Task GetOrder_ReturnsNotFound_WhenItemNotFound() - { - // Arrange - _queriesMock.Setup(queries => queries.GetOrder(1)) - .Throws(new KeyNotFoundException()); - - var controller = new OrdersController(_mediatorMock.Object, _queriesMock.Object, _identityMock.Object); - - // Act - var notFoundResult = await controller.GetOrder(1); - - // Assert - Assert.IsType(notFoundResult); - } - - [Fact] - public async Task GetOrder_ReturnsOkObjecResult_WheItemFound() - { - // Arrange - _queriesMock.Setup(queries => queries.GetOrder(1)) - .Returns(Task.FromResult(new object())); - - var controller = new OrdersController(_mediatorMock.Object, _queriesMock.Object, _identityMock.Object); - - // Act - var OkObjectResult = await controller.GetOrder(1); - - // Assert - Assert.IsType(OkObjectResult); - } - - [Fact] - public async Task GetOrders_ReturnsOkObjectResult() - { - // Arrange - _queriesMock.Setup(queries => queries.GetOrders()) - .Returns(Task.FromResult(new object())); - - var controller = new OrdersController(_mediatorMock.Object, _queriesMock.Object, _identityMock.Object); - - // Act - var OkObjectResult = await controller.GetOrders(); - - // Assert - Assert.IsType(OkObjectResult); - } - - [Fact] - public async Task GetCardTypes() - { - // Arrange - _queriesMock.Setup(queries => queries.GetCardTypes()) - .Returns(Task.FromResult(new object())); - - var controller = new OrdersController(_mediatorMock.Object, _queriesMock.Object, _identityMock.Object); - - // Act - var OkObjectResult = await controller.GetCardTypes(); - - // Assert - Assert.IsType(OkObjectResult); - } - - //Fakes - private NewOrderRequest OrderFakeNotExpired() - { - return new NewOrderRequest() - { - CardTypeId = 1, - CardExpiration = new DateTime(2020, 12, 12), - Buyer = _userIdentity - }; - } - - private NewOrderRequest OrderFakeExpired() - { - return new NewOrderRequest() - { - CardTypeId = 1, - CardExpiration = DateTime.Now.AddYears(-1) - }; - } - } -}