TourIAm/TIAMWebApp/Server/Controllers/GoogleAPIController .cs

104 lines
3.5 KiB
C#

using AyCode.Core.Extensions;
using AyCode.Core.Server.Loggers;
using GoogleApi;
using GoogleApi.Entities.Common;
using GoogleApi.Entities.Common.Enums;
using GoogleApi.Entities.Maps.AddressValidation.Request;
using GoogleApi.Entities.Maps.Common;
using GoogleApi.Entities.Maps.Common.Enums;
using GoogleApi.Entities.Maps.DistanceMatrix.Request;
using GoogleApi.Entities.Maps.DistanceMatrix.Response;
using GoogleApi.Entities.Maps.Geocoding.Location.Request;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using TIAM.Services.Server;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.PageModels;
namespace TIAMWebApp.Server.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
public class GoogleAPIController : ControllerBase
{
private readonly ILogger<SupplierAPIController> _logger;
private readonly GooglePlacesService _googlePlacesService;
public GoogleAPIController(ILogger<SupplierAPIController> logger, GooglePlacesService googlePlacesService)
{
_logger = logger;
_googlePlacesService = googlePlacesService;
}
//[HttpPost]
//[Route("GetAddressForCoordinates")]
//public async Task<string?> GetAddressForCoordinates(TripInfo myTrip)
//{
// var latitude = myTrip.StartLatitude; // Example latitude
// var longitude = myTrip.StartLongitude; // Example longitude
// GlobalLogger.Info(latitude.ToString());
// GlobalLogger.Info(longitude.ToString());
// var booo = new Coordinate(latitude, longitude);
// var request = new LocationGeocodeRequest
// {
// Key = _apiKey,
// Location = booo
// };
// try
// {
// var response = await GoogleMaps.Geocode.LocationGeocode.QueryAsync(request);
// if (response.Status == Status.Ok)
// {
// return response.RawJson;
// }
// else
// {
// return "Something wrong";
// }
// }
// catch (Exception ex)
// {
// return ex + "Error occurred while calling GoogleMaps.Geocode.LocationGeocode.QueryAsync";
// }
//}
//google api call to get coordinates from address
[HttpPost]
[Route(APIUrls.GoogleValidateAddressRouteName)]
public async Task<string> ValidateAddress([FromBody] string address)
{
_logger.LogDebug($"ValidateAddress Called {address}");
var result = await _googlePlacesService.ValidateAddressAsync(address);
return result;
}
[HttpPost]
[Route(APIUrls.GoogleCalculateRouteRouteName)]
public async Task<string> CalculateRouteAsync(RouteCalculationModel routeCalculationModel)
{
var result = await _googlePlacesService.CalculateRouteAsync(routeCalculationModel.Origin, routeCalculationModel.Destination, routeCalculationModel.TravelTime);
return result;
}
[HttpPost]
[Route(APIUrls.GoogleAutoCompleteRouteName)]
public async Task<string> GoogleAutoComplete([FromBody] string address)
{
_logger.LogDebug($"ValidateAddress Called {address}");
var result = await _googlePlacesService.GetPlacePredictionsAsync(address);
return result.ToJson();
}
}
}