Refactoring of idempotent elements and logic.
This commit is contained in:
parent
53bdf6de04
commit
cea9600227
@ -9,8 +9,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events
|
|||||||
public IntegrationEvent()
|
public IntegrationEvent()
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid();
|
Id = Guid.NewGuid();
|
||||||
|
CreationDate = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Guid Id { get; }
|
public Guid Id { get; }
|
||||||
|
public DateTime CreationDate { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF
|
|||||||
public IntegrationEventLogEntry(IntegrationEvent @event)
|
public IntegrationEventLogEntry(IntegrationEvent @event)
|
||||||
{
|
{
|
||||||
EventId = @event.Id;
|
EventId = @event.Id;
|
||||||
CreationTime = DateTime.UtcNow;
|
CreationTime = @event.CreationDate;
|
||||||
EventTypeName = @event.GetType().FullName;
|
EventTypeName = @event.GetType().FullName;
|
||||||
Content = JsonConvert.SerializeObject(@event);
|
Content = JsonConvert.SerializeObject(@event);
|
||||||
State = EventStateEnum.NotPublished;
|
State = EventStateEnum.NotPublished;
|
||||||
|
@ -20,18 +20,18 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
|
|||||||
foreach (var id in userIds)
|
foreach (var id in userIds)
|
||||||
{
|
{
|
||||||
var basket = await _repository.GetBasket(id);
|
var basket = await _repository.GetBasket(id);
|
||||||
await UpdatePriceInBasketItems(@event.ProductId, @event.NewPrice, basket);
|
await UpdatePriceInBasketItems(@event.ProductId, @event.NewPrice, @event.OldPrice, basket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UpdatePriceInBasketItems(int productId, decimal newPrice, CustomerBasket basket)
|
private async Task UpdatePriceInBasketItems(int productId, decimal newPrice, decimal oldPrice, CustomerBasket basket)
|
||||||
{
|
{
|
||||||
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == productId).ToList();
|
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == productId).ToList();
|
||||||
if (itemsToUpdate != null)
|
if (itemsToUpdate != null)
|
||||||
{
|
{
|
||||||
foreach (var item in itemsToUpdate)
|
foreach (var item in itemsToUpdate)
|
||||||
{
|
{
|
||||||
if(item.UnitPrice != newPrice)
|
if(item.UnitPrice == oldPrice)
|
||||||
{
|
{
|
||||||
var originalPrice = item.UnitPrice;
|
var originalPrice = item.UnitPrice;
|
||||||
item.UnitPrice = newPrice;
|
item.UnitPrice = newPrice;
|
||||||
@ -39,7 +39,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
await _repository.UpdateBasket(basket);
|
await _repository.UpdateBasket(basket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
using Domain.AggregatesModel.OrderAggregate;
|
using Domain.AggregatesModel.OrderAggregate;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries;
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules
|
||||||
|
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency
|
||||||
{
|
{
|
||||||
public interface IRequestManager
|
public interface IRequestManager
|
||||||
{
|
{
|
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency
|
||||||
{
|
{
|
||||||
public class RequestManager : IRequestManager
|
public class RequestManager : IRequestManager
|
||||||
{
|
{
|
@ -6,7 +6,7 @@ namespace UnitTest.Ordering.Application
|
|||||||
{
|
{
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||||
|
using Ordering.Domain.Exceptions;
|
||||||
using System;
|
using System;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -82,7 +83,7 @@ public class BuyerAggregateTest
|
|||||||
var expiration = DateTime.Now.AddYears(-1);
|
var expiration = DateTime.Now.AddYears(-1);
|
||||||
|
|
||||||
//Act - Assert
|
//Act - Assert
|
||||||
Assert.Throws<ArgumentException>(() => new PaymentMethod(cardTypeId, alias, cardNumber, securityNumber, cardHolderName, expiration));
|
Assert.Throws<OrderingDomainException>(() => new PaymentMethod(cardTypeId, alias, cardNumber, securityNumber, cardHolderName, expiration));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
using Ordering.Domain.Events;
|
using Ordering.Domain.Events;
|
||||||
|
using Ordering.Domain.Exceptions;
|
||||||
using System;
|
using System;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ public class OrderAggregateTest
|
|||||||
var units = -1;
|
var units = -1;
|
||||||
|
|
||||||
//Act - Assert
|
//Act - Assert
|
||||||
Assert.Throws<ArgumentNullException>(() => new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units));
|
Assert.Throws<OrderingDomainException>(() => new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -53,7 +54,7 @@ public class OrderAggregateTest
|
|||||||
var units = 1;
|
var units = 1;
|
||||||
|
|
||||||
//Act - Assert
|
//Act - Assert
|
||||||
Assert.Throws<ArgumentException>(() => new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units));
|
Assert.Throws<OrderingDomainException>(() => new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -71,7 +72,7 @@ public class OrderAggregateTest
|
|||||||
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
|
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Throws<ArgumentException>(() => fakeOrderItem.SetNewDiscount(-1));
|
Assert.Throws<OrderingDomainException>(() => fakeOrderItem.SetNewDiscount(-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -89,7 +90,7 @@ public class OrderAggregateTest
|
|||||||
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
|
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Throws<ArgumentException>(() => fakeOrderItem.AddUnits(-1));
|
Assert.Throws<OrderingDomainException>(() => fakeOrderItem.AddUnits(-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user