public async Task <IEnumerable <OrderCompanyDTO> > GetListOfCompaniesAsync(ClientLocationDTO clientInfo) { List <OrderCompanyDTO> result = new List <OrderCompanyDTO>(); var sw = new Stopwatch(); sw.Start(); var companies = await db.Companies.GetAsync(c => c.Workers.Any(w => w.StatusId == (int)WorkerStatus.Working), include : c => c.Workers); sw.Stop(); Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine($"Elapsed time on query == {sw.ElapsedMilliseconds}ms"); Console.WriteLine("--------------------------------------------------------------"); foreach (var company in companies) { var companyInfo = await GetClosestWorkerAsync(company, clientInfo); if (companyInfo != null) { result.Add(companyInfo); } } return(result.Count == 0 ? null : result); }
private async Task <OrderCompanyDTO> GetClosestWorkerAsync(Company company, ClientLocationDTO clientInfo) { double minDistance = -1; string minDuration = null; int closestWorkerId = -1; foreach (var worker in company.Workers) { if (worker.StatusId != (int)WorkerStatus.Working || worker.CarTypeId != clientInfo.CarType) { continue; } var workerLocation = await db.WorkersLastLocation.FindByIdAsync(worker.Id); var distance = await mapService.GetDistanceAsync(clientInfo.Latitude, clientInfo.Longitude, workerLocation.Latitude, workerLocation.Longitude); var duration = await mapService.GetDurationAsync(clientInfo.Latitude, clientInfo.Longitude, workerLocation.Latitude, workerLocation.Longitude); if (minDistance == -1 || distance < minDistance) { minDistance = distance; minDuration = duration; closestWorkerId = worker.Id; } } if (minDistance != -1) { var result = Mapper.Map <Company, OrderCompanyDTO>(company); result.Rate = company.CountRate == 0 ? 0 : (double)company.SumRate / (double)company.CountRate; result.ClosestDistance = minDistance; result.ClosestDuration = minDuration; result.ClosestWorkerId = closestWorkerId; return(result); } return(null); }
public async Task <IActionResult> ListOfCompanies([FromHeader(Name = "api_key")] string apiKey, [FromBody] ClientLocationDTO clientInfo) { if (await clientService.GetByApiKeyAsync(apiKey) == null) { return(Unauthorized()); } if (clientInfo == null || !TryValidateModel(clientInfo)) { return(BadRequest(ModelState)); } var response = orderService.GetListOfCompaniesAsync(clientInfo); if (response == null) { return(NotFound()); } return(Json(response)); }