public async Task Handle(Message message, CancellationToken cancellationToken) { if (message.UserProperties.TryGetValue(eventTypeMessagePropertyKey, out var eventType)) { if (eventType.ToString().Equals(typeof(RoomAdded).Name, System.StringComparison.InvariantCultureIgnoreCase)) { var roomAddedEvent = JsonConvert.DeserializeObject <RoomAdded>(Encoding.Default.GetString(message.Body)); //translate the inventory event to reservations related event for use by event handlers within reservations var roomTypeAvailabilityIncreased = new RoomTypeAvailabilityIncreased(roomAddedEvent.HotelId, roomAddedEvent.RoomType, 1); await _bus.Publish(new EventData { EventType = roomTypeAvailabilityIncreased.GetType().Name, Event = roomTypeAvailabilityIncreased, Metadata = new Metadata() }); } } }
private async Task OnRoomTypeAvailabilityIncreased(IDenormalizerContext <RoomTypeAvailabilityReadModel> ctx, RoomTypeAvailabilityIncreased @event) { var roomTypeAvailability = await ctx.Repository.GetByKeys(@event.Id.ToString(), @event.RoomType, new CancellationToken()); if (roomTypeAvailability == null) { roomTypeAvailability = new RoomTypeAvailabilityReadModel { HotelId = @event.Id, RoomType = @event.RoomType, Amount = @event.Amount }; } else { roomTypeAvailability.Amount += @event.Amount; } await ctx.Repository.Save(roomTypeAvailability, new CancellationToken()); }