UpdateBasketCount draft changes
This commit is contained in:
parent
897c924b5b
commit
152b15069b
@ -47,7 +47,22 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
|||||||
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
|
||||||
public async Task<ActionResult<CustomerBasket>> UpdateBasketAsync([FromBody]CustomerBasket value)
|
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")]
|
[Route("checkout")]
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -167,6 +167,7 @@ namespace Ordering.SignalrHub
|
|||||||
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>();
|
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>();
|
||||||
eventBus.Subscribe<OrderStatusChangedToCancelledIntegrationEvent, OrderStatusChangedToCancelledIntegrationEventHandler>();
|
eventBus.Subscribe<OrderStatusChangedToCancelledIntegrationEvent, OrderStatusChangedToCancelledIntegrationEventHandler>();
|
||||||
eventBus.Subscribe<OrderStatusChangedToSubmittedIntegrationEvent, OrderStatusChangedToSubmittedIntegrationEventHandler>();
|
eventBus.Subscribe<OrderStatusChangedToSubmittedIntegrationEvent, OrderStatusChangedToSubmittedIntegrationEventHandler>();
|
||||||
|
eventBus.Subscribe<UserAddedCartItemToBasketIntegrationEvent, UserAddedCartItemToBasketIntegrationEventHandler>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ConfigureAuthService(IServiceCollection services)
|
private void ConfigureAuthService(IServiceCollection services)
|
||||||
|
@ -122,6 +122,14 @@
|
|||||||
refreshOrderList();
|
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() {
|
function getToken() {
|
||||||
@ -129,6 +137,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function refreshOrderList() {
|
function refreshOrderList() {
|
||||||
|
console.log('In refreshOrderList method');
|
||||||
clearTimeout(timerId);
|
clearTimeout(timerId);
|
||||||
timerId = setTimeout(function () {
|
timerId = setTimeout(function () {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user