adding some views

This commit is contained in:
2026-03-22 16:28:01 +01:00
parent 498d46c10f
commit 7594d179a8
16 changed files with 327 additions and 92 deletions
+85 -23
View File
@@ -6,41 +6,103 @@ namespace CityInfo.WEB.Services
public class CityDataService : ICityDataService
{
private readonly HttpClient _httpClient;
private readonly ILogger<CityDataService> _logger;
public CityDataService(
HttpClient httpClient)
public CityDataService(HttpClient httpClient, ILogger<CityDataService> logger)
{
_httpClient = httpClient;
}
public async Task<City> CreateCity(City city)
{
var response = await _httpClient.PostAsJsonAsync<City>("/api/cities", city);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<City>();
}
return null;
}
public async Task DeleteCity(int cityId)
{
await _httpClient.DeleteAsync($"/api/cities/{cityId}");
_logger = logger;
}
public async Task<IEnumerable<City>> GetCities()
{
return await _httpClient.GetFromJsonAsync<IEnumerable<City>>("/api/cities");
try
{
return await _httpClient.GetFromJsonAsync<IEnumerable<City>>("/api/cities")
?? Enumerable.Empty<City>();
}
catch (Exception ex)
{
_logger.LogError(ex, "Erreur lors de la récupération des villes");
return Enumerable.Empty<City>();
}
}
public async Task<City> GetCity(int cityId)
public async Task<City?> GetCity(int cityId)
{
return await _httpClient.GetFromJsonAsync<City>($"/api/cities/{cityId}");
try
{
var response = await _httpClient.GetAsync($"/api/cities/{cityId}");
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<City>();
}
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
_logger.LogWarning("Erreur HTTP {StatusCode} lors du chargement de la ville {CityId}",
response.StatusCode, cityId);
return null;
}
catch (Exception ex)
{
_logger.LogError(ex, "Erreur lors de la récupération de la ville {CityId}", cityId);
return null;
}
}
public async Task UpdateCity(City city)
public async Task<City?> CreateCity(City city)
{
await _httpClient.PutAsJsonAsync<City>($"/api/cities/{city.Id}", city);
try
{
var response = await _httpClient.PostAsJsonAsync("/api/cities", city);
if (!response.IsSuccessStatusCode)
{
_logger.LogWarning("Erreur HTTP {StatusCode} lors de la création d'une ville", response.StatusCode);
return null;
}
return await response.Content.ReadFromJsonAsync<City>();
}
catch (Exception ex)
{
_logger.LogError(ex, "Erreur lors de la création d'une ville");
return null;
}
}
public async Task<bool> UpdateCity(City city)
{
try
{
var response = await _httpClient.PutAsJsonAsync($"/api/cities/{city.Id}", city);
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
_logger.LogError(ex, "Erreur lors de la mise à jour de la ville {CityId}", city.Id);
return false;
}
}
public async Task<bool> DeleteCity(int cityId)
{
try
{
var response = await _httpClient.DeleteAsync($"/api/cities/{cityId}");
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
_logger.LogError(ex, "Erreur lors de la suppression de la ville {CityId}", cityId);
return false;
}
}
}
}
}
+4 -4
View File
@@ -5,9 +5,9 @@ namespace CityInfo.WEB.Services
public interface ICityDataService
{
public Task<IEnumerable<City>> GetCities();
public Task<City> GetCity(int cityId);
public Task<City> CreateCity(City city);
public Task UpdateCity(City city);
public Task DeleteCity(int cityId);
public Task<City?> GetCity(int cityId);
public Task<City?> CreateCity(City city);
public Task<bool> UpdateCity(City city);
public Task<bool> DeleteCity(int cityId);
}
}
@@ -5,9 +5,9 @@ namespace CityInfo.WEB.Services
public interface IPointOfInterestDataService
{
public Task<IEnumerable<PointOfInterest>> GetPointsOfInterest(int cityId);
public Task<PointOfInterest> GetPointOfInterest(int cityId, int pointOfInterestId);
public Task<PointOfInterest> CreatePointOfInterest(int cityId, PointOfInterest pointOfInterest);
public Task UpdatePointOfInterest(int cityId, PointOfInterest pointOfInterest);
public Task DeletePointOfInterest(int cityId, int pointOfInterestId);
public Task<PointOfInterest?> GetPointOfInterest(int cityId, int pointOfInterestId);
public Task<PointOfInterest?> CreatePointOfInterest(int cityId, PointOfInterest pointOfInterest);
public Task<bool> UpdatePointOfInterest(int cityId, PointOfInterest pointOfInterest);
public Task<bool> DeletePointOfInterest(int cityId, int pointOfInterestId);
}
}
@@ -1,37 +1,49 @@
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)
HttpClient httpClient, ILogger<PointOfInterestDataService> logger)
{
_httpClient = httpClient;
_logger = logger;
}
public Task<PointOfInterest> CreatePointOfInterest(int cityId, PointOfInterest pointOfInterest)
public Task<PointOfInterest?> CreatePointOfInterest(int cityId, PointOfInterest pointOfInterest)
{
throw new NotImplementedException();
}
public Task DeletePointOfInterest(int cityId, int pointOfInterestId)
public Task<bool> DeletePointOfInterest(int cityId, int pointOfInterestId)
{
throw new NotImplementedException();
}
public Task<PointOfInterest> GetPointOfInterest(int cityId, int pointOfInterestId)
public Task<PointOfInterest?> GetPointOfInterest(int cityId, int pointOfInterestId)
{
throw new NotImplementedException();
}
public Task<IEnumerable<PointOfInterest>> GetPointsOfInterest(int cityId)
public async Task<IEnumerable<PointOfInterest>> GetPointsOfInterest(int cityId)
{
throw new NotImplementedException();
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 Task UpdatePointOfInterest(int cityId, PointOfInterest pointOfInterest)
public Task<bool> UpdatePointOfInterest(int cityId, PointOfInterest pointOfInterest)
{
throw new NotImplementedException();
}