Modify AsyncRequestNames, include UnitTest project, remove unused application ordering layer
This commit is contained in:
parent
73e7fefde5
commit
3c277a49de
@ -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
|
||||
|
@ -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<bool>
|
||||
{
|
||||
|
||||
private readonly List<OrderItem> _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<OrderItem> OrderItems => _orderItems;
|
||||
|
||||
public void AddOrderItem(OrderItem item)
|
||||
{
|
||||
_orderItems.Add(item);
|
||||
}
|
||||
|
||||
public NewOrderRequest()
|
||||
{
|
||||
_orderItems = new List<OrderItem>();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<NewOrderRequest, bool>
|
||||
{
|
||||
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<bool> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -43,7 +43,7 @@
|
||||
|
||||
[Route("new")]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddOrder([FromBody]NewOrderRequest order)
|
||||
public async Task<IActionResult> AddOrder([FromBody]NewOrderCommand order)
|
||||
{
|
||||
if (order.CardTypeId == 0)
|
||||
order.CardTypeId = 1;
|
||||
|
@ -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)));
|
||||
|
@ -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<NewOrderRequest, bool>
|
||||
{
|
||||
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<bool> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<bool>
|
||||
{
|
||||
|
||||
private readonly List<OrderItem> _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<OrderItem> OrderItems => _orderItems;
|
||||
|
||||
public void AddOrderItem(OrderItem item)
|
||||
{
|
||||
_orderItems.Add(item);
|
||||
}
|
||||
|
||||
public NewOrderRequest()
|
||||
{
|
||||
_orderItems = new List<OrderItem>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Decorators
|
||||
{
|
||||
using Extensions.Logging;
|
||||
using MediatR;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class LogDecorator<TRequest, TResponse>
|
||||
: IAsyncRequestHandler<TRequest, TResponse>
|
||||
where TRequest : IAsyncRequest<TResponse>
|
||||
{
|
||||
private readonly IAsyncRequestHandler<TRequest, TResponse> _inner;
|
||||
private readonly ILogger<LogDecorator<TRequest, TResponse>> _logger;
|
||||
|
||||
|
||||
public LogDecorator(
|
||||
IAsyncRequestHandler<TRequest, TResponse> inner,
|
||||
ILogger<LogDecorator<TRequest, TResponse>> logger)
|
||||
{
|
||||
_inner = inner;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<TResponse> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>4193caa3-a1c3-4818-a06f-a2d85fde77e7</ProjectGuid>
|
||||
<RootNamespace>Microsoft.eShopOnContainers.Services.Ordering.Application</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
@ -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")]
|
@ -1,13 +0,0 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Application.Queries
|
||||
{
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface IOrderQueries
|
||||
{
|
||||
Task<dynamic> GetOrder(int id);
|
||||
|
||||
Task<dynamic> GetOrders();
|
||||
|
||||
Task<dynamic> GetCardTypes();
|
||||
}
|
||||
}
|
@ -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<dynamic> GetOrder(int id)
|
||||
{
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
var result = await connection.QueryAsync<dynamic>(
|
||||
@"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<dynamic> GetOrders()
|
||||
{
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
return await connection.QueryAsync<dynamic>(@"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<dynamic> GetCardTypes()
|
||||
{
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
return await connection.QueryAsync<dynamic>("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<dynamic>();
|
||||
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<dynamic> items = (result as System.Collections.IEnumerable).Cast<dynamic>();
|
||||
// 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;
|
||||
//}
|
||||
}
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
@ -39,13 +39,8 @@
|
||||
modelBuilder.Entity<CardType>(ConfigureCardTypes);
|
||||
modelBuilder.Entity<OrderStatus>(ConfigureOrderStatus);
|
||||
modelBuilder.Entity<Address>(ConfigureAddress);
|
||||
|
||||
modelBuilder.Entity<Address>()
|
||||
.ToTable("address", DEFAULT_SCHEMA);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ConfigureBuyer(EntityTypeBuilder<Buyer> buyerConfiguration)
|
||||
{
|
||||
buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA);
|
||||
|
@ -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<IBuyerRepository> _buyerRepositoryMock;
|
||||
@ -20,25 +17,29 @@ namespace UnitTest.Ordering.Application
|
||||
|
||||
public NewOrderRequestHandlerTest()
|
||||
{
|
||||
//Mocks;
|
||||
|
||||
_buyerRepositoryMock = new Mock<IBuyerRepository>();
|
||||
_orderRepositoryMock = new Mock<IOrderRepository>();
|
||||
}
|
||||
|
||||
[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<Buyer>(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<Buyer>(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",
|
||||
|
@ -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<IMediator> _mediatorMock;
|
||||
private readonly Mock<IIdentityService> _identityMock;
|
||||
private readonly Mock<IOrderQueries> _queriesMock;
|
||||
private readonly string _userIdentity;
|
||||
|
||||
public OrderControllerTest()
|
||||
{
|
||||
//Mocks;
|
||||
_mediatorMock = new Mock<IMediator>();
|
||||
_identityMock = new Mock<IIdentityService>();
|
||||
_queriesMock = new Mock<IOrderQueries>();
|
||||
_userIdentity = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOrder_ReturnsBadRequestResult_WhenPersitenceOperationFails()
|
||||
{
|
||||
// Arrange
|
||||
var orderRequest = new object() as IAsyncRequest<bool>;
|
||||
|
||||
_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>(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>(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>(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>(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>(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)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user