Fixed bugs related to the Product Update and PriceChangedIntegrationEvent. It was updating only when the price was changing... additional refactoring performed, too.
This commit is contained in:
parent
92777dc538
commit
313976da80
@ -20,11 +20,11 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
|
||||
foreach (var id in userIds)
|
||||
{
|
||||
var basket = await _repository.GetBasket(id);
|
||||
await UpdateBasket(@event.ProductId, @event.NewPrice, basket);
|
||||
await UpdatePriceInBasketItems(@event.ProductId, @event.NewPrice, basket);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateBasket(int productId, decimal newPrice, CustomerBasket basket)
|
||||
private async Task UpdatePriceInBasketItems(int productId, decimal newPrice, CustomerBasket basket)
|
||||
{
|
||||
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == productId).ToList();
|
||||
if (itemsToUpdate != null)
|
||||
|
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
||||
@ -138,36 +139,45 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
||||
//POST api/v1/[controller]/edit
|
||||
[Route("edit")]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> EditProduct([FromBody]CatalogItem product)
|
||||
public async Task<IActionResult> EditProduct([FromBody]CatalogItem productToUpdate)
|
||||
{
|
||||
var item = await _catalogContext.CatalogItems.SingleOrDefaultAsync(i => i.Id == product.Id);
|
||||
var catalogItem = await _catalogContext.CatalogItems.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id);
|
||||
if (catalogItem == null) return NotFound();
|
||||
|
||||
if (item == null)
|
||||
bool raiseProductPriceChangedEvent = false;
|
||||
IntegrationEvent priceChangedEvent = null;
|
||||
|
||||
if (catalogItem.Price != productToUpdate.Price) raiseProductPriceChangedEvent = true;
|
||||
|
||||
if (raiseProductPriceChangedEvent) // Create event if price has changed
|
||||
{
|
||||
return NotFound();
|
||||
var oldPrice = catalogItem.Price;
|
||||
priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
|
||||
}
|
||||
|
||||
if (item.Price != product.Price)
|
||||
{
|
||||
var oldPrice = item.Price;
|
||||
item.Price = product.Price;
|
||||
var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice);
|
||||
//Update current product
|
||||
catalogItem = productToUpdate;
|
||||
|
||||
using (var transaction = _catalogContext.Database.BeginTransaction())
|
||||
{
|
||||
_catalogContext.CatalogItems.Update(item);
|
||||
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
|
||||
using (var transaction = _catalogContext.Database.BeginTransaction())
|
||||
{
|
||||
_catalogContext.CatalogItems.Update(catalogItem);
|
||||
await _catalogContext.SaveChangesAsync();
|
||||
|
||||
await _integrationEventLogService.SaveEventAsync(@event);
|
||||
//Save to EventLog only if product price changed
|
||||
if(raiseProductPriceChangedEvent)
|
||||
await _integrationEventLogService.SaveEventAsync(priceChangedEvent);
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
_eventBus.Publish(@event);
|
||||
|
||||
await _integrationEventLogService.MarkEventAsPublishedAsync(@event);
|
||||
}
|
||||
}
|
||||
|
||||
//Publish to Event Bus only if product price changed
|
||||
if (raiseProductPriceChangedEvent)
|
||||
{
|
||||
_eventBus.Publish(priceChangedEvent);
|
||||
await _integrationEventLogService.MarkEventAsPublishedAsync(priceChangedEvent);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
@ -84,14 +84,14 @@
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Sweatshirt", Name = ".NET Bot Black Sweatshirt", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation Sweatshirt", Name = ".NET Foundation Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Sweatshirt", Name = ".NET Blue Sweatshirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Sweatshirt", Name = "Kudu Purple Sweatshirt", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" },
|
||||
|
@ -45,6 +45,15 @@
|
||||
<p>
|
||||
<a asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" class="text">Register as a new user?</a>
|
||||
</p>
|
||||
<p>
|
||||
Note that for demo purposes you don't need to register and can login with these credentials:
|
||||
</p>
|
||||
<p>
|
||||
User: <b>demouser@microsoft.com</b>
|
||||
</p>
|
||||
<p>
|
||||
Password: <b>Pass@word1</b>
|
||||
</p>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
|
@ -44,7 +44,16 @@
|
||||
<p>
|
||||
<a asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" class="text">Register as a new user?</a>
|
||||
</p>
|
||||
</form>
|
||||
<p>
|
||||
Note that for demo purposes you don't need to register and can login with these credentials:
|
||||
</p>
|
||||
<p>
|
||||
User: <b>demouser@microsoft.com</b>
|
||||
</p>
|
||||
<p>
|
||||
Password: <b>Pass@word1</b>
|
||||
</p>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -38,7 +38,7 @@
|
||||
<div class="esh-basket-items--border row">
|
||||
@if (item.OldUnitPrice != 0)
|
||||
{
|
||||
<div class="alert alert-warning esh-basket-margin12" role="alert"> The price of the item has changed.Old price was @item.OldUnitPrice $</div>
|
||||
<div class="alert alert-warning esh-basket-margin12" role="alert"> Note that the price of this article changed in our Catalog. The old price when you originally added it to the basket was @item.OldUnitPrice $</div>
|
||||
}
|
||||
</div>
|
||||
<br/>
|
||||
|
@ -29,7 +29,7 @@
|
||||
</article>
|
||||
<br/>
|
||||
<div class="esh-basket-items-margin-left1 row">
|
||||
<div class="alert alert-warning" role="alert" *ngIf="item.oldUnitPrice > 0"> The price of the item has changed.Old price was {{item.oldUnitPrice}} $</div>
|
||||
<div class="alert alert-warning" role="alert" *ngIf="item.oldUnitPrice > 0"> Note that the price of this article changed in our Catalog. The old price when you originally added it to the basket was {{item.oldUnitPrice}} $</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user