Files
aviq-boilerplate/CityInfo.WEB/Services/PointOfInterestDataService.cs
2026-03-22 23:27:16 +01:00

84 lines
3.0 KiB
C#

using CityInfo.WEB.Models;
using System.Net.Http.Json;
namespace CityInfo.WEB.Services
{
public class PointOfInterestDataService : IPointOfInterestDataService
{
private readonly HttpClient _httpClient;
private readonly ILogger<PointOfInterestDataService> _logger;
public PointOfInterestDataService(
HttpClient httpClient, ILogger<PointOfInterestDataService> logger)
{
_httpClient = httpClient;
_logger = logger;
}
public Task<PointOfInterest?> CreatePointOfInterest(int cityId, PointOfInterest pointOfInterest)
{
throw new NotImplementedException();
}
public Task<bool> DeletePointOfInterest(int cityId, int pointOfInterestId)
{
throw new NotImplementedException();
}
public async Task<PointOfInterest?> GetPointOfInterest(int cityId, int pointOfInterestId)
{
try
{
var response = await _httpClient.GetAsync($"/api/cities/{cityId}/pointsofinterest/{pointOfInterestId}");
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<PointOfInterest>();
}
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
_logger.LogWarning($"Erreur HTTP {response.StatusCode} lors du chargement du point d'intéret {pointOfInterestId}",
response.StatusCode, cityId);
return null;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Erreur lors de la récupération du point d'intéret {pointOfInterestId}", cityId);
return null;
}
}
public async Task<IEnumerable<PointOfInterest>> GetPointsOfInterest(int cityId)
{
try
{
return await _httpClient.GetFromJsonAsync<IEnumerable<PointOfInterest>>($"api/cities/{cityId}/pointsofinterest/")
?? Enumerable.Empty<PointOfInterest>();
}
catch (Exception ex)
{
_logger.LogError(ex, "Erreur lors de la récupération des Points d'intérets.");
return Enumerable.Empty<PointOfInterest>();
}
}
public async Task<bool> UpdatePointOfInterest(int cityId, PointOfInterest pointOfInterest)
{
try
{
var response = await _httpClient.PutAsJsonAsync($"/api/cities/{cityId}/pointsofinterest/{pointOfInterest.Id}", pointOfInterest);
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Erreur lors de la mise à jour du point d'intéret {pointOfInterest.Id}", pointOfInterest.Id);
return false;
}
}
}
}