Newer
Older
CommandsEvents / src / Shipping / CreateShippingLabelHandler.cs
@Derek Comartin Derek Comartin on 11 Mar 2022 922 bytes Init
using System;
using System.Threading.Tasks;
using Billing.Contracts;
using NServiceBus;
using Sales.Contracts;
using Shipping.Contracts;

namespace Shipping
{
    public class CreateShippingLabelHandler :IHandleMessages<OrderBilled>
    {
        private readonly ShippingDbContext _dbContext;

        public CreateShippingLabelHandler(ShippingDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task Handle(OrderBilled message, IMessageHandlerContext context)
        {
            await _dbContext.ShippingLabels.AddAsync(new ShippingLabel
            {
                OrderId = message.OrderId,
                OrderDate = DateTime.UtcNow
            });
            await _dbContext.SaveChangesAsync();

            await context.Publish<ShippingLabelCreated>(created =>
            {
                created.OrderId = message.OrderId;
            });
        }
    }
}