public async Task <StadiumDetail> GetStadiumByNameAsync(string name) { var connStr = DbConnection.connectionString; var cmdStr = "Select * from stadium_team where stadium_name=@name"; StadiumDetail stadium = null; using (var conn = new NpgsqlConnection(connStr)) { using (var cmd = new NpgsqlCommand(cmdStr, conn)) { cmd.Parameters.AddWithValue("@name", name); await conn.OpenAsync(); using (NpgsqlDataReader rd = await cmd.ExecuteReaderAsync()) { stadium = new StadiumDetail(); while (rd.Read()) { stadium.StadiumName = rd["stadium_name"].ToString(); stadium.HomeTeam = rd["team_name"].ToString(); stadium.Capacity = Convert.ToInt32(rd["capacity"]); stadium.City = rd["city"].ToString(); stadium.TeamImage = rd["team_image"].ToString(); } } } } return(stadium); }
public async Task <IActionResult> OnGetStadiumDetail(string name, double latitude, double longitude) { var stadiumDetail = new StadiumDetail(); // Execute the search request var searchResponse = await GooglePlaces.NearBySearch.QueryAsync(new PlacesNearBySearchRequest { Key = GoogleApiKey, Name = name, Location = new Location(latitude, longitude), Radius = 1000 }); // If we did not get a good response, or the list of results are empty then get out of here if (!searchResponse.Status.HasValue || searchResponse.Status.Value != Status.Ok || !searchResponse.Results.Any()) { return(new BadRequestResult()); } // Get the first result var nearbyResult = searchResponse.Results.FirstOrDefault(); string placeId = nearbyResult.PlaceId; string photoReference = nearbyResult.Photos?.FirstOrDefault()?.PhotoReference; string photoCredit = nearbyResult.Photos?.FirstOrDefault()?.HtmlAttributions.FirstOrDefault(); //Execute the details request var detailsResponse = await GooglePlaces.Details.QueryAsync(new PlacesDetailsRequest { Key = GoogleApiKey, PlaceId = placeId }); // If we did not get a good response then get out of here if (!detailsResponse.Status.HasValue || detailsResponse.Status.Value != Status.Ok) { return(new BadRequestResult()); } // Set the details var detailsResult = detailsResponse.Result; stadiumDetail.FormattedAddress = detailsResult.FormattedAddress; stadiumDetail.PhoneNumber = detailsResult.InternationalPhoneNumber; stadiumDetail.Website = detailsResult.Website; if (photoReference != null) { // Execute the photo request var photosResponse = await GooglePlaces.Photos.QueryAsync(new PlacesPhotosRequest { Key = GoogleApiKey, PhotoReference = photoReference, MaxWidth = 400 }); if (photosResponse.PhotoBuffer != null) { stadiumDetail.Photo = Convert.ToBase64String(photosResponse.PhotoBuffer); stadiumDetail.PhotoCredit = photoCredit; } } return(new JsonResult(stadiumDetail)); }