public async Task AssignCourierToDeliveryAsync(AssignCourierToDeliveryDtoSave assignCourierToDelivery) { var deliveryId = assignCourierToDelivery.DeliveryId; var courierId = assignCourierToDelivery.CourierId; var delivery = await _deliveryRepository.GetDeliveryByIdAsync(deliveryId); if (delivery == null) { throw new KeyNotFoundException($"Delivery not found with specified Id: {deliveryId}"); } var courier = await _courierRepository.GetCourierByIdAsync(courierId); if (courier == null) { throw new KeyNotFoundException($"Courier not found with specified Id: {courierId}"); } await _deliveryRepository.AssignCourierToDeliveryAsync(deliveryId, courierId); await _deliveryRepository.SetDeliveryStatusAsync(deliveryId, DeliveryStatus.CourierAssigned); var deliveryStatusChangedEvent = new CourierAssignedToDeliveryIntegrationEvent(deliveryId, courierId); _integrationEventPublisher.Publish(deliveryStatusChangedEvent); }
public async void DeliveryService_AssignCourierToDeliveryAsync_assigns_courier_and_publishes_integration_event() { // Arrange var delivery = A.New <Delivery>(); var courier = A.New <Courier>(); var dto = new AssignCourierToDeliveryDtoSave { CourierId = courier.Id, DeliveryId = delivery.Id }; _deliveryRepository.Setup(x => x.GetDeliveryByIdAsync(dto.DeliveryId)).ReturnsAsync(() => delivery); _courierRepository.Setup(x => x.GetCourierByIdAsync(dto.CourierId)).ReturnsAsync(() => courier); // Act Func <Task> action = async() => { await _service.AssignCourierToDeliveryAsync(dto); }; // Assert await action.Should().NotThrowAsync(); _deliveryRepository.Verify(x => x.GetDeliveryByIdAsync(dto.DeliveryId), Times.Once()); _courierRepository.Verify(x => x.GetCourierByIdAsync(dto.CourierId), Times.Once()); _deliveryRepository.Verify(x => x.AssignCourierToDeliveryAsync(dto.DeliveryId, dto.CourierId), Times.Once()); _deliveryRepository.Verify(x => x.SetDeliveryStatusAsync(dto.DeliveryId, DeliveryStatus.CourierAssigned), Times.Once()); _integrationEventPublisher.Verify(x => x.Publish(It.Is <CourierAssignedToDeliveryIntegrationEvent>( @event => @event.DeliveryId == dto.DeliveryId && @event.CourierId == dto.CourierId)), Times.Once()); }
public async Task <IActionResult> AssignCourierToDeliveryAsync( AssignCourierToDeliveryDtoSave assignCourierToDelivery) { await _deliveryService.AssignCourierToDeliveryAsync(assignCourierToDelivery); return(Accepted()); }