diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 0bf15fc42..c3f548641 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -47,7 +47,22 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers [ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)] public async Task> 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")] diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs new file mode 100644 index 000000000..892b0fef6 --- /dev/null +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs @@ -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; + } + } +} diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/UserAddedCartItemToBasketIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/UserAddedCartItemToBasketIntegrationEventHandler.cs new file mode 100644 index 000000000..cb6cf454b --- /dev/null +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/UserAddedCartItemToBasketIntegrationEventHandler.cs @@ -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 + { + private readonly IHubContext _hubContext; + private readonly ILogger _logger; + + public UserAddedCartItemToBasketIntegrationEventHandler( + IHubContext hubContext, + ILogger 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"); + } + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/BasketItem.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/BasketItem.cs new file mode 100644 index 000000000..fb3f58c13 --- /dev/null +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/BasketItem.cs @@ -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 Validate(ValidationContext validationContext) + { + var results = new List(); + + if (Quantity < 1) + { + results.Add(new ValidationResult("Invalid number of units", new []{ "Quantity" })); + } + + return results; + } + } +} diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs new file mode 100644 index 000000000..2428149b8 --- /dev/null +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs @@ -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; + } + } +} diff --git a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs index edcc80521..894db7358 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs @@ -167,6 +167,7 @@ namespace Ordering.SignalrHub eventBus.Subscribe(); eventBus.Subscribe(); eventBus.Subscribe(); + eventBus.Subscribe(); } private void ConfigureAuthService(IServiceCollection services) diff --git a/src/Web/WebMVC/Views/Shared/_Layout.cshtml b/src/Web/WebMVC/Views/Shared/_Layout.cshtml index c5447c980..a0c4b30a9 100644 --- a/src/Web/WebMVC/Views/Shared/_Layout.cshtml +++ b/src/Web/WebMVC/Views/Shared/_Layout.cshtml @@ -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();