public async Task <double> GetDistance(double fromLat, double fromLong, double toLat, double tolong) { if (!int.TryParse(_context.User.Claims.FirstOrDefault(cl => cl.Type == "id")?.Value, out var userId)) { RaiseError("unauthorized", HttpStatusCode.Unauthorized); } var distance = await _distanceRepo.GetQueryableItems(predicate : en => en.FromLat == fromLat && en.FromLong == fromLong && en.ToLat == toLat && en.ToLong == tolong).SingleOrDefaultAsync(); var request = new Request() { UserId = userId }; if (distance == null) { request.Distance = new Distance() { FromLat = fromLat, FromLong = fromLong, ToLat = toLat, ToLong = tolong, Value = GeoUtil.GetDistance(fromLat, fromLong, toLat, tolong) }; } if (distance != null) { request.DistanceId = distance.Id; } await _RequestRepo.InsertAsync(request); await _unitofWork.SaveChangesAsync(); return(distance != null ? distance.Value : request.Distance.Value); }