public async Task <ActionResult> QueryCsv(QueryCredentials queryCredentials)
        {
            try //TODO Cache
            {
                if (queryCredentials.Aggregated.HasValue && queryCredentials.Aggregated.Value == true &&
                    queryCredentials.ClusterRadius.HasValue && queryCredentials.ClusterRadius.Value > 0)
                {
                    var aggregatedList = await _credentialService.ClusteredCredentials(queryCredentials.Lat.GetValueOrDefault(),
                                                                                       queryCredentials.Lon.GetValueOrDefault(), queryCredentials.Radius.GetValueOrDefault(), queryCredentials.ClusterRadius.GetValueOrDefault(),
                                                                                       queryCredentials.Filter?.AgeToTuple(), queryCredentials.Filter?.Sex);

                    string csv = ToCsv(aggregatedList);
                    return(File(System.Text.Encoding.UTF8.GetBytes(csv), "text/csv", "data.csv"));
                }

                if (queryCredentials.Aggregated.HasValue && queryCredentials.Aggregated.Value == true)
                {
                    var aggregatedList = await _credentialService.GetPointsInCircleAggregated(queryCredentials.Lat.Value,
                                                                                              queryCredentials.Lon.Value, queryCredentials.Radius.Value, queryCredentials.Filter?.Country,
                                                                                              queryCredentials.Filter?.State, queryCredentials.Filter?.AgeToTuple(), queryCredentials.Filter?.Sex);

                    string csv = ToCsv(aggregatedList);
                    return(File(System.Text.Encoding.UTF8.GetBytes(csv), "text/csv", "data.csv"));
                }

                if (queryCredentials.Lat.HasValue && queryCredentials.Lon.HasValue && queryCredentials.Radius.HasValue)
                {
                    if (queryCredentials.Radius.Value <= 0)
                    {
                        return(StatusCode(422, "Radius cannot be 0 or less"));
                    }

                    if (queryCredentials.Filter != null)
                    {
                        var list = await _credentialService.GetPointsInCircle(queryCredentials.Lat.Value,
                                                                              queryCredentials.Lon.Value, queryCredentials.Radius.Value, queryCredentials.Filter.Country,
                                                                              queryCredentials.Filter.State, queryCredentials.Filter.AgeToTuple(), queryCredentials.Filter.Sex);

                        string finalCsv = ToCsv(list);
                        return(File(System.Text.Encoding.UTF8.GetBytes(finalCsv), "text/csv", "data.csv"));
                    }
                    else
                    {
                        var list = await _credentialService.GetPointsInCircle(queryCredentials.Lat.Value,
                                                                              queryCredentials.Lon.Value, queryCredentials.Radius.Value);

                        string finalCsv = ToCsv(list);
                        return(File(System.Text.Encoding.UTF8.GetBytes(finalCsv), "text/csv", "data.csv"));
                    }
                }

                return(NotFound());
            }
            catch (Exception e)
            {
                var guid = Guid.NewGuid();
                _logger.LogError(e, $"Error on Post. Id: {guid}", null);
                return(StatusCode(500, $"Error Id {guid}"));
            }
        }
        private async Task <ActionResult> QueryCredentials(QueryCredentials queryCredentials)
        {
            try
            {
                if (queryCredentials.Aggregated.HasValue && queryCredentials.Aggregated.Value == true &&
                    queryCredentials.ClusterRadius.HasValue && queryCredentials.ClusterRadius.Value > 0)
                {
                    var aggregatedList = await _credentialService.ClusteredCredentials(queryCredentials.Lat.GetValueOrDefault(),
                                                                                       queryCredentials.Lon.GetValueOrDefault(), queryCredentials.Radius.GetValueOrDefault(), queryCredentials.ClusterRadius.GetValueOrDefault(),
                                                                                       queryCredentials.Filter?.AgeToTuple(), queryCredentials.Filter?.Sex);

                    return(Ok(aggregatedList));
                }

                if (queryCredentials.Aggregated.HasValue && queryCredentials.Aggregated.Value == true)
                {
                    var aggregatedList = await _credentialService.GetPointsInCircleAggregated(queryCredentials.Lat.GetValueOrDefault(),
                                                                                              queryCredentials.Lon.GetValueOrDefault(), queryCredentials.Radius.GetValueOrDefault(), queryCredentials.Filter?.Country,
                                                                                              queryCredentials.Filter?.State, queryCredentials.Filter?.AgeToTuple(), queryCredentials.Filter?.Sex);

                    return(Ok(aggregatedList));
                }

                if (queryCredentials.Lat.HasValue && queryCredentials.Lon.HasValue && queryCredentials.Radius.HasValue)
                {
                    if (queryCredentials.Radius.Value <= 0)
                    {
                        return(StatusCode(422, "Radius cannot be 0 or less"));
                    }
                    if (queryCredentials.Filter != null)
                    {
                        var list = await _credentialService.GetPointsInCircle(queryCredentials.Lat.Value,
                                                                              queryCredentials.Lon.Value, queryCredentials.Radius.Value, queryCredentials.Filter.Country,
                                                                              queryCredentials.Filter.State, queryCredentials.Filter.AgeToTuple(), queryCredentials.Filter.Sex);

                        return(Ok(list));
                    }
                    else
                    {
                        var list = await _credentialService.GetPointsInCircle(queryCredentials.Lat.Value,
                                                                              queryCredentials.Lon.Value, queryCredentials.Radius.Value);

                        return(Ok(list));
                    }
                }

                return(NotFound());
            }
            catch (Exception e)
            {
                var guid = Guid.NewGuid();
                _logger.LogError(e, $"Error on Post. Id: {guid}", null);
                return(StatusCode(500, $"Error Id {guid}"));
            }
        }
 public async Task <ActionResult> Query(QueryCredentials queryCredentials)
 {
     return(await QueryCredentials(queryCredentials)); //TODO cache
 }