Skip to content

Commit

Permalink
order create saga pattern implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Oğuzhan Aydın committed Apr 28, 2021
1 parent b853c48 commit f9c0d93
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Extensions.Logging;
using Order.Api.Application.IntegrationEvents.Events;
using Order.Api.Domain;
using System;
using System.Threading.Tasks;
using Zero.Core.Repositories;
using Zero.Core.UnitOfWork;
using Zero.EventBus.Abstractions;

namespace Order.Api.Application.IntegrationEvents.EventHandlers
{
public class RemoveProductStockErrorIntegrationEventHandler : IIntegrationEventHandler<RemoveProductStockErrorIntegrationEvent>
{
#region .ctor

private readonly IRepository<Domain.Order> _orderRepository;
private readonly IRepository<OrderItem> _orderItemRepository;
private readonly ILogger<RemoveProductStockErrorIntegrationEventHandler> _logger;
private readonly IUnitOfWork _uow;

public RemoveProductStockErrorIntegrationEventHandler(IRepository<Domain.Order> orderRepository, IRepository<OrderItem> orderItemRepository, ILogger<RemoveProductStockErrorIntegrationEventHandler> logger, IUnitOfWork uow)
{
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_orderItemRepository = orderItemRepository ?? throw new ArgumentNullException(nameof(orderItemRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_uow = uow ?? throw new ArgumentNullException(nameof(uow));
}

#endregion

public async Task Handle(RemoveProductStockErrorIntegrationEvent @event)
{
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);

await _orderItemRepository.DeleteAsync(x => x.Order.Id == @event.OrderId);
await _orderRepository.DeleteAsync(x => x.Id == @event.OrderId);
await _uow.SaveAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Zero.EventBus.Events;

namespace Order.Api.Application.IntegrationEvents.Events
{
public record RemoveProductStockErrorIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }

public RemoveProductStockErrorIntegrationEvent(int orderId)
{
OrderId = orderId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace Order.Api.Application.IntegrationEvents.Events
public record RemoveProductStockIntegrationEvent : IntegrationEvent
{
public int UserId { get; }
public int OrderId { get; }
public List<OrderItemIntegrationEventModel> OrderItems { get; }

public RemoveProductStockIntegrationEvent(List<OrderItemIntegrationEventModel> orderItems, int userId)
public RemoveProductStockIntegrationEvent(List<OrderItemIntegrationEventModel> orderItems, int userId, int orderId)
{
OrderItems = orderItems;
UserId = userId;
OrderId = orderId;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Order.Api.Application.IntegrationEvents.EventHandlers;
using Order.Api.Application.IntegrationEvents.Events;
using Zero.EventBus.Abstractions;

Expand All @@ -11,6 +12,7 @@ public static void UseSubscribes(this IApplicationBuilder builder)
{
var bus = builder.ApplicationServices.GetService<IEventBus>();

bus.Subscribe<RemoveProductStockErrorIntegrationEvent, RemoveProductStockErrorIntegrationEventHandler>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Product.Api.Application.Hubs;
using Product.Api.Application.IntegrationEvents.Events;
using Product.Api.Application.Queries;
using Product.Api.Domain.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Product.Api.Application.Queries;
using Zero.Core.Extensions;
using Zero.Core.Repositories;
using Zero.Core.UnitOfWork;
Expand All @@ -25,14 +25,16 @@ public class RemoveProductStockIntegrationEventHandler : IIntegrationEventHandle
private readonly IRepository<Domain.Product> _productRepository;
private readonly IUnitOfWork _uow;
private readonly IDistributedCache _cache;
private readonly IEventBus _bus;

public RemoveProductStockIntegrationEventHandler(ILogger<RemoveProductStockIntegrationEventHandler> logger, IHubContext<ProductHub, IProductHub> productHub, IRepository<Domain.Product> productRepository, IUnitOfWork uow, IDistributedCache cache)
public RemoveProductStockIntegrationEventHandler(ILogger<RemoveProductStockIntegrationEventHandler> logger, IHubContext<ProductHub, IProductHub> productHub, IRepository<Domain.Product> productRepository, IUnitOfWork uow, IDistributedCache cache, IEventBus bus)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_productHub = productHub ?? throw new ArgumentNullException(nameof(productHub));
_productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository));
_uow = uow ?? throw new ArgumentNullException(nameof(uow));
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_bus = bus ?? throw new ArgumentNullException(nameof(bus));
}

#endregion
Expand Down Expand Up @@ -61,9 +63,9 @@ public async Task Handle(RemoveProductStockIntegrationEvent @event)
catch (Exception ex)
{
_logger.LogError("----- Error occured integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_bus.Publish(new RemoveProductStockErrorIntegrationEvent(@event.OrderId));
await _productHub.Clients.Group(@event.UserId.ToString()).ProductStockChangedError(ex.Message);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Zero.EventBus.Events;

namespace Product.Api.Application.IntegrationEvents.Events
{
public record RemoveProductStockErrorIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }

public RemoveProductStockErrorIntegrationEvent(int orderId)
{
OrderId = orderId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace Product.Api.Application.IntegrationEvents.Events
public record RemoveProductStockIntegrationEvent : IntegrationEvent
{
public int UserId { get; }
public int OrderId { get; }
public List<OrderItemIntegrationEventModel> OrderItems { get; }

public RemoveProductStockIntegrationEvent(List<OrderItemIntegrationEventModel> orderItems, int userId)
public RemoveProductStockIntegrationEvent(List<OrderItemIntegrationEventModel> orderItems, int userId, int orderId)
{
OrderItems = orderItems;
UserId = userId;
OrderId = orderId;
}
}

Expand Down

0 comments on commit f9c0d93

Please sign in to comment.