private async Task AssignDeliveryToRider() { var deliveryToProcess = deliveryService.GetDeliveriesToAssign().FirstOrDefault(); if (deliveryToProcess != null) { logger.Log($"Processing delivery {deliveryToProcess.Id}", Microsoft.Extensions.Logging.LogLevel.Information, System.Net.HttpStatusCode.OK, nameof(RiderHandlerHostedService)); var availableRiders = await riderService.GetRiders(new DTO.RiderFilter() { Active = true }); if (availableRiders.Any()) { // TODO: Calcolare in base a distanza da punti di consegna, quando saranno geography anche loro :) var rider = availableRiders.OrderByDescending(r => r.AverageRating).First(); await deliveryService.UpdateDelivery(new DTO.Delivery() { Id = deliveryToProcess.Id, IdRider = rider.IdRider, TakeChargeDate = DateTime.UtcNow }); logger.Log($"Assigned rider {availableRiders} to delivery {deliveryToProcess.Id}", Microsoft.Extensions.Logging.LogLevel.Information, System.Net.HttpStatusCode.OK, nameof(RiderHandlerHostedService)); } else { logger.Log($"No available riders for delivery {deliveryToProcess.Id}", Microsoft.Extensions.Logging.LogLevel.Warning, System.Net.HttpStatusCode.OK, nameof(RiderHandlerHostedService)); } } }
private async Task AssignDeliveryToRider() { using (var scope = serviceScopeFactory.CreateScope()) { IDeliveryService deliveryService = scope.ServiceProvider.GetRequiredService <IDeliveryService>(); IRiderService riderService = scope.ServiceProvider.GetRequiredService <IRiderService>(); var deliveryToProcess = deliveryService.GetDeliveriesToAssign().FirstOrDefault(); if (deliveryToProcess != null) { logger.Log($"Processing delivery {deliveryToProcess.Id}", Microsoft.Extensions.Logging.LogLevel.Information, System.Net.HttpStatusCode.OK, nameof(RiderHandlerHostedService)); var availableRiders = await riderService.GetRiders(new DTO.RiderFilter() { Active = true }); if (availableRiders.Any()) { var rider = availableRiders .Where(r => r.StartingPoint != null) // TODO: stiamo usando solo il punto di pick up e non stiamo usando il range...migliorare l'algoritmo :) .OrderBy(r => r.StartingPoint.Distance(deliveryToProcess.PickUpPosition)) .First(); await deliveryService.UpdateDelivery(new DTO.Delivery() { Id = deliveryToProcess.Id, IdRider = rider.IdRider, TakeChargeDate = DateTime.UtcNow }); logger.Log($"Assigned rider {rider.IdRider} - \"{rider.RiderName}\" to delivery {deliveryToProcess.Id}", Microsoft.Extensions.Logging.LogLevel.Information, System.Net.HttpStatusCode.OK, nameof(RiderHandlerHostedService)); } else { logger.Log($"No available riders for delivery {deliveryToProcess.Id}", Microsoft.Extensions.Logging.LogLevel.Warning, System.Net.HttpStatusCode.OK, nameof(RiderHandlerHostedService)); } } } }
public async Task <IActionResult> GetRiders([FromQuery] DTO.RiderFilter filter) { var result = await riderService.GetRiders(filter); return(Ok(result)); }