diff --git a/src/Services/Basket/Basket.SignalrHub/AutofacModules/ApplicationModule.cs b/src/Services/Basket/Basket.SignalrHub/AutofacModules/ApplicationModule.cs index d344abcaa..9cdf7cf45 100644 --- a/src/Services/Basket/Basket.SignalrHub/AutofacModules/ApplicationModule.cs +++ b/src/Services/Basket/Basket.SignalrHub/AutofacModules/ApplicationModule.cs @@ -1,4 +1,5 @@ using Autofac; +using Basket.SignalrHub.IntegrationEvents; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using System; using System.Collections.Generic; @@ -24,6 +25,8 @@ namespace Basket.SignalrHub.AutofacModules // builder.RegisterAssemblyTypes(typeof(NewIntegrationEvent).GetTypeInfo().Assembly) // .AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); + builder.RegisterAssemblyTypes(typeof(UserAddedCartItemToBasketIntegrationEvent).GetTypeInfo().Assembly) + .AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); } } } diff --git a/src/Services/Basket/Basket.SignalrHub/Basket.SignalrHub.csproj b/src/Services/Basket/Basket.SignalrHub/Basket.SignalrHub.csproj index 485dd9c85..df9ff1c11 100644 --- a/src/Services/Basket/Basket.SignalrHub/Basket.SignalrHub.csproj +++ b/src/Services/Basket/Basket.SignalrHub/Basket.SignalrHub.csproj @@ -6,8 +6,6 @@ - - diff --git a/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/EventHandling/UserAddedCartItemToBasketIntegrationEventHandler.cs b/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/EventHandling/UserAddedCartItemToBasketIntegrationEventHandler.cs new file mode 100644 index 000000000..c65192ed5 --- /dev/null +++ b/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/EventHandling/UserAddedCartItemToBasketIntegrationEventHandler.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNetCore.SignalR; +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; +using Basket.SignalrHub.IntegrationEvents; +using Serilog.Context; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Basket.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/Basket/Basket.SignalrHub/IntegrationEvents/Events/BasketItem.cs b/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/Events/BasketItem.cs new file mode 100644 index 000000000..ddf5d13b7 --- /dev/null +++ b/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/Events/BasketItem.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Basket.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/Basket/Basket.SignalrHub/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs b/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs new file mode 100644 index 000000000..fde8da774 --- /dev/null +++ b/src/Services/Basket/Basket.SignalrHub/IntegrationEvents/Events/UserAddedCartItemToBasketIntegrationEvent.cs @@ -0,0 +1,17 @@ +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +using System.Collections.Generic; + +namespace Basket.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/Basket/Basket.SignalrHub/Startup.cs b/src/Services/Basket/Basket.SignalrHub/Startup.cs index 877c30722..87e80caff 100644 --- a/src/Services/Basket/Basket.SignalrHub/Startup.cs +++ b/src/Services/Basket/Basket.SignalrHub/Startup.cs @@ -17,6 +17,8 @@ using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; using Basket.SignalrHub.AutofacModules; +using Basket.SignalrHub.IntegrationEvents; +using Basket.SignalrHub.IntegrationEvents.EventHandling; namespace Basket.SignalrHub { @@ -160,6 +162,7 @@ namespace Basket.SignalrHub // Event bus subscribe events goes here // eventBus.Subscribe(); + eventBus.Subscribe(); } private void ConfigureAuthService(IServiceCollection services) diff --git a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs index 894db7358..c7089ec3c 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs @@ -167,7 +167,7 @@ namespace Ordering.SignalrHub eventBus.Subscribe(); eventBus.Subscribe(); eventBus.Subscribe(); - eventBus.Subscribe(); + //eventBus.Subscribe(); } private void ConfigureAuthService(IServiceCollection services) diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index 52a311369..3192d9b45 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -266,7 +266,8 @@ namespace Microsoft.eShopOnContainers.WebMVC options.Scope.Add("marketing"); options.Scope.Add("locations"); options.Scope.Add("webshoppingagg"); - options.Scope.Add("orders.signalrhub"); + options.Scope.Add("orders.signalrhub"); + options.Scope.Add("basket.signalrhub"); }); return services; diff --git a/src/Web/WebSPA/Client/modules/shared/services/security.service.ts b/src/Web/WebSPA/Client/modules/shared/services/security.service.ts index 267b3111e..c6170f522 100644 --- a/src/Web/WebSPA/Client/modules/shared/services/security.service.ts +++ b/src/Web/WebSPA/Client/modules/shared/services/security.service.ts @@ -81,7 +81,7 @@ export class SecurityService { let client_id = 'js'; let redirect_uri = location.origin + '/'; let response_type = 'id_token token'; - let scope = 'openid profile orders basket marketing locations webshoppingagg orders.signalrhub'; + let scope = 'openid profile orders basket marketing locations webshoppingagg orders.signalrhub basket.signalrhub'; let nonce = 'N' + Math.random() + '' + Date.now(); let state = Date.now() + '' + Math.random();