UpdateBasketCount draft changes

This commit is contained in:
Vijay Bikka 2019-11-17 16:19:50 -08:00
parent 897c924b5b
commit 152b15069b
7 changed files with 128 additions and 1 deletions

View File

@ -47,7 +47,22 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
public async Task<ActionResult<CustomerBasket>> UpdateBasketAsync([FromBody]CustomerBasket value)
{
return Ok(await _repository.UpdateBasketAsync(value));
var basket = await _repository.UpdateBasketAsync(value);
var integrationEvent = new UserAddedCartItemToBasketIntegrationEvent("demouser@microsoft.com", value.Items[value.Items.Count - 1]);
try
{
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", integrationEvent.Id, Program.AppName, integrationEvent);
_eventBus.Publish(integrationEvent);
}
catch (Exception ex)
{
_logger.LogError(ex, "ERROR Publishing integration event: {IntegrationEventId} from {AppName}", integrationEvent.Id, Program.AppName);
throw;
}
return Ok(basket);
}
[Route("checkout")]

View File

@ -0,0 +1,18 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using System.Collections.Generic;
namespace Basket.API.IntegrationEvents.Events
{
public class UserAddedCartItemToBasketIntegrationEvent : IntegrationEvent
{
public string BuyerName { get; set; }
public BasketItem Item { get; set; }
public UserAddedCartItemToBasketIntegrationEvent(string buyerName, BasketItem basketItem)
{
BuyerName = buyerName;
Item = basketItem;
}
}
}

View File

@ -0,0 +1,40 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.IntegrationEvents.Events;
using Serilog.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
{
public class UserAddedCartItemToBasketIntegrationEventHandler :
IIntegrationEventHandler<UserAddedCartItemToBasketIntegrationEvent>
{
private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<UserAddedCartItemToBasketIntegrationEventHandler> _logger;
public UserAddedCartItemToBasketIntegrationEventHandler(
IHubContext<NotificationsHub> hubContext,
ILogger<UserAddedCartItemToBasketIntegrationEventHandler> logger)
{
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task Handle(UserAddedCartItemToBasketIntegrationEvent @event)
{
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
{
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
await _hubContext.Clients
.Group(@event.BuyerName)
.SendAsync("UpdateBasketCount", "test message");
}
}
}
}

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Ordering.SignalrHub.IntegrationEvents
{
public class BasketItem : IValidatableObject
{
public string Id { get; set; }
public string ProductId { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public decimal OldUnitPrice { get; set; }
public int Quantity { get; set; }
public string PictureUrl { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
if (Quantity < 1)
{
results.Add(new ValidationResult("Invalid number of units", new []{ "Quantity" }));
}
return results;
}
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System.Collections.Generic;
namespace Ordering.SignalrHub.IntegrationEvents
{
public class UserAddedCartItemToBasketIntegrationEvent : IntegrationEvent
{
public string BuyerName { get; set; }
public BasketItem Item { get; set; }
public UserAddedCartItemToBasketIntegrationEvent(string buyerName, BasketItem basketItem)
{
BuyerName = buyerName;
Item = basketItem;
}
}
}

View File

@ -167,6 +167,7 @@ namespace Ordering.SignalrHub
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToCancelledIntegrationEvent, OrderStatusChangedToCancelledIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToSubmittedIntegrationEvent, OrderStatusChangedToSubmittedIntegrationEventHandler>();
eventBus.Subscribe<UserAddedCartItemToBasketIntegrationEvent, UserAddedCartItemToBasketIntegrationEventHandler>();
}
private void ConfigureAuthService(IServiceCollection services)

View File

@ -122,6 +122,14 @@
refreshOrderList();
}
});
connection.on("UpdateBasketCount", (message) => {
// toastr.success('Updated to status: ' + message.status, 'Order Id: ' + message.orderId);
//if (window.location.pathname.split("/").pop() === 'Order') {
console.log('Inside update basketcount method');
refreshOrderList();
//}
});
}
function getToken() {
@ -129,6 +137,7 @@
}
function refreshOrderList() {
console.log('In refreshOrderList method');
clearTimeout(timerId);
timerId = setTimeout(function () {
window.location.reload();