Modify AsyncRequestNames, include UnitTest project, remove unused application ordering layer
This commit is contained in:
parent
8ba5c51296
commit
099cd1b350
@ -0,0 +1,48 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Api.Application.Commands
|
||||
{
|
||||
using System;
|
||||
using MediatR;
|
||||
using Domain;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class NewOrderCommand
|
||||
: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 NewOrderCommand()
|
||||
{
|
||||
_orderItems = new List<OrderItem>();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
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 NewOrderCommandHandler
|
||||
: IAsyncRequestHandler<NewOrderCommand, bool>
|
||||
{
|
||||
private readonly IBuyerRepository _buyerRepository;
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
|
||||
public NewOrderCommandHandler(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(NewOrderCommand 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, NewOrderCommand message)
|
||||
{
|
||||
Payment payment = PaymentAlreadyExist(buyer, message);
|
||||
|
||||
if (payment == null)
|
||||
{
|
||||
payment = CreatePayment(message);
|
||||
buyer.Payments.Add(payment);
|
||||
}
|
||||
|
||||
return payment;
|
||||
|
||||
}
|
||||
|
||||
Payment PaymentAlreadyExist(Domain.Buyer buyer, NewOrderCommand 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(NewOrderCommand message)
|
||||
{
|
||||
return _buyerRepository.Add(
|
||||
new Buyer(message.Buyer));
|
||||
}
|
||||
|
||||
Order CreateOrder(int buyerId, int paymentId, int addressId)
|
||||
{
|
||||
return new Order(buyerId, paymentId);
|
||||
}
|
||||
|
||||
Payment CreatePayment(NewOrderCommand message)
|
||||
{
|
||||
return new Payment(message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration, message.CardTypeId);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
|
||||
//namespace FunctionalTests.Services.Basket
|
||||
//{
|
||||
// using Microsoft.eShopOnContainers.Services.Basket.API;
|
||||
// using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||
// using Microsoft.Extensions.Logging;
|
||||
// using Microsoft.Extensions.Options;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Threading.Tasks;
|
||||
// using Xunit;
|
||||
|
||||
|
||||
// public class RedisBasketRepositoryTests
|
||||
// {
|
||||
// [Fact]
|
||||
// public async Task UpdateBasket_return_and_add_basket()
|
||||
// {
|
||||
// var redisBasketRepository = BuildBasketRepository();
|
||||
|
||||
// var basket = await redisBasketRepository.UpdateBasket(new CustomerBasket("customerId")
|
||||
// {
|
||||
// BuyerId = "buyerId",
|
||||
// Items = BuildBasketItems()
|
||||
// });
|
||||
|
||||
// Assert.NotNull(basket);
|
||||
// Assert.Equal(1, basket.Items.Count);
|
||||
// }
|
||||
|
||||
// [Fact]
|
||||
// public async Task GetBasket_return_existing_basket()
|
||||
// {
|
||||
// }
|
||||
|
||||
// RedisBasketRepository BuildBasketRepository()
|
||||
// {
|
||||
// var loggerFactory = new LoggerFactory();
|
||||
|
||||
// var options = Options.Create<BasketSettings>(new BasketSettings()
|
||||
// {
|
||||
// ConnectionString = "127.0.0.1"
|
||||
// });
|
||||
|
||||
// return new RedisBasketRepository(options, loggerFactory);
|
||||
// }
|
||||
|
||||
// List<BasketItem> BuildBasketItems()
|
||||
// {
|
||||
// return new List<BasketItem>()
|
||||
// {
|
||||
// new BasketItem()
|
||||
// {
|
||||
// Id = "basketId",
|
||||
// PictureUrl = "pictureurl",
|
||||
// ProductId = "productId",
|
||||
// ProductName = "productName",
|
||||
// Quantity = 1,
|
||||
// UnitPrice = 1
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
//}
|
@ -31,7 +31,7 @@ namespace FunctionalTests.Services.Catalog
|
||||
return $"api/v1/catalog/items?pageIndex={pageIndex}&pageSize={pageCount}";
|
||||
}
|
||||
|
||||
public static string Filtered(int catalogTypeId,int catalogBrandId)
|
||||
public static string Filtered(int catalogTypeId, int catalogBrandId)
|
||||
{
|
||||
return $"api/v1/catalog/items/type/{catalogTypeId}/brand/{catalogBrandId}";
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
using Xunit;
|
||||
|
||||
public class CatalogScenarios
|
||||
:CatalogScenarioBase
|
||||
: CatalogScenarioBase
|
||||
{
|
||||
[Fact]
|
||||
public async Task Get_get_all_catalogitems_and_response_ok_status_code()
|
||||
@ -24,7 +24,7 @@
|
||||
using (var server = CreateServer())
|
||||
{
|
||||
var response = await server.CreateClient()
|
||||
.GetAsync(Get.Paginated(0,4));
|
||||
.GetAsync(Get.Paginated(0, 4));
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
@ -56,7 +56,7 @@
|
||||
var content = new StringContent(BuildOrderWithInvalidExperationTime(), UTF8Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await server.CreateClient()
|
||||
.PostAsync(Post.AddNewOrder,content);
|
||||
.PostAsync(Post.AddNewOrder, content);
|
||||
|
||||
Assert.True(response.StatusCode == System.Net.HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
@ -2,14 +2,17 @@
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"Ordering.API": "1.0.0-*",
|
||||
"Microsoft.NETCore.App": "1.1.0",
|
||||
"Microsoft.AspNetCore.TestHost": "1.1.0",
|
||||
"dotnet-test-xunit": "2.2.0-preview2-build1029",
|
||||
"xunit": "2.2.0-beta4-build3444",
|
||||
"Catalog.API": "1.0.0-*",
|
||||
"xunit": "2.2.0-beta4-build3444"
|
||||
"Ordering.API": "1.0.0-*"
|
||||
},
|
||||
"testRunner": "xunit",
|
||||
"runtimes": {
|
||||
"win10-x64": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"netcoreapp1.0": {
|
||||
"dependencies": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user