public async Task <IActionResult> Customer([Bind("StartAddress,Locality,EndAddress")][FromBody] CustomerDrivePost model) { if (!ModelState.IsValid) { return(BadRequest(ErrorEnum.InvalidModelState)); } var userName = User.Identity.Name; if (userName == null) { return(Unauthorized(ErrorEnum.NoUserLogIn)); } var user = await userManager.FindByNameAsync(userName); Debug.Assert(user != null); var userId = user.Id; var lastRequest = await GetLastRequestAsync(userId); if (lastRequest != null) { var lastDrive = await drivesRepository.GetFirstByConditionAsync(d => d.CustomerRequestId == lastRequest.Id); if (lastDrive != null && lastDrive.Status == Drive.DriveStatus.Open) { return(BadRequest(ErrorEnum.DriveAlreadyOpen)); } } var driveRequest = await FormatRequestAsync(model.StartAddress, model.Locality, userId); if (driveRequest == null) { return(BadRequest(ErrorEnum.InvalidStartAddress)); } var endLocation = await FormatSimpleRequestAsync(model.EndAddress, model.Locality); if (endLocation == null) { return(BadRequest(ErrorEnum.InvalidEndAddress)); } var startCordinates = driveRequest.Cordinates; var endCordinates = endLocation.Cordinates; var directionsUrl = @"https://maps.googleapis.com/maps/api/directions/json?" + $"origin={startCordinates}" + $"&destination={endCordinates}&avoid=ferries&units=metric&key={configuration["Maps:Key"]}"; var directionsResponse = await urlService.SendRequestAsync(directionsUrl); var directionsRoot = JsonConvert.DeserializeObject <DirectionsRoot.RootObject>(directionsResponse); if (directionsRoot.status != "OK") { return(BadRequest(ErrorEnum.InvalidRoute)); } var directions = mapper.Map <Directions>(directionsRoot); requestsRepository.Insert(driveRequest); await requestsRepository.ApplyChangesAsync(); var drive = await driveService.WaitForDriverAsync(driveRequest, directions); driveRequest.Status = drive == null ? DriveRequest.RequestStatus.Cancelled : DriveRequest.RequestStatus.Answered; requestsRepository.Update(driveRequest); await requestsRepository.ApplyChangesAsync(); if (drive == null) { return(NotFound()); } var driverRequest = await requestsRepository.GetByIdAsync(drive.DriverRequestId); Debug.Assert(driverRequest != null); var driver = await driversRepository.GetByIdAsync(driverRequest.UserId); Debug.Assert(driver != null); var applicationDriver = await userManager.FindByIdAsync(driver.Id); Debug.Assert(applicationDriver != null); var result = mapper.Map <CustomerDriveResult>(drive); mapper.Map(driver, result); mapper.Map(applicationDriver, result); return(Ok(result)); }