-
Notifications
You must be signed in to change notification settings - Fork 0
/
CartCreateEventHandler.cs
47 lines (41 loc) · 1.6 KB
/
CartCreateEventHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using TimedHostedService.Worker.Domain.CartEvents.Events;
using TimedHostedService.Worker.Domain.CartEvents.Mappers;
using TimedHostedService.Worker.Domain.Interfaces;
namespace TimedHostedService.Worker.Domain.CartEvents.Handlers
{
public class CartCreateEventHandler : IEventHandler
{
private readonly ILogger<CartCreateEventHandler> _logger;
private readonly ICartRepository _cartRepository;
private readonly ICartItemRepository _cartItemRepository;
private readonly ICartMapper _cartMapper;
public CartCreateEventHandler(ILogger<CartCreateEventHandler> logger, ICartRepository cartRepository,
ICartItemRepository cartItemRepository, ICartMapper cartMapper)
{
_logger = logger;
_cartRepository = cartRepository;
_cartItemRepository = cartItemRepository;
_cartMapper = cartMapper;
}
///<inheritdoc/>
public Task HandleAsync(CartEvent cartEvent)
{
var cart = _cartMapper.MapCart(cartEvent);
var newCartDbId = _cartRepository.Add(cart);
foreach (var cartItem in cart.CartItems)
{
cartItem.CartId = newCartDbId;
_cartItemRepository.Add(cartItem);
}
_logger.LogInformation($"{cartEvent.EventType} OK");
return Task.CompletedTask;
}
///<inheritdoc/>
public bool IsMatch(EventType eventType)
{
return eventType.Equals(EventType.CART_CREATE);
}
}
}