From ec692b1c8a59dc0b24326a6af25d8f0ffe06b5c1 Mon Sep 17 00:00:00 2001 From: Geoffrey Dulac Date: Thu, 12 Dec 2024 10:53:38 -0500 Subject: [PATCH] feat(311699): Refacto weatherEndpoints.cs --- .../Endpoints/WeatherEndpoints.cs | 62 ++++++++++++++----- .../Weather/IWeatherRepository.cs | 2 +- .../Weather/IWeatherService.cs | 2 +- .../Weather/WeatherRepository.cs | 10 +-- .../Weather/WeatherService.cs | 4 +- 5 files changed, 55 insertions(+), 25 deletions(-) diff --git a/template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs b/template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs index c477469..fdbfed0 100644 --- a/template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs +++ b/template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs @@ -7,36 +7,66 @@ public static class WeatherEndpoints { public static void MapWeatherEndpoints(this IEndpointRouteBuilder endpoints) { - endpoints.MapGet("/weathers", async (IWeatherService weatherService) => - { + endpoints.MapGet("/weathers", GetAllWeathers); + endpoints.MapGet("/weather/{weatherId}", GetWeather); + endpoints.MapPost("/weather", CreateWeather); + endpoints.MapPut("/weather/{weatherId}", UpdateWeather); + endpoints.MapDelete("/weather/{weatherId}", DeleteWeather); + } + + private static async Task GetAllWeathers(IWeatherService weatherService) + { + try { IEnumerable weathers = await weatherService.GetWeathers(); return Results.Ok(weathers); - }); + } catch { + return Results.InternalServerError(); + } + } - endpoints.MapGet("/weather/{weatherId}", async (long weatherId, IWeatherService weatherService) => - { - Weather? weather = await weatherService.GetWeather(weatherId); + private static async Task GetWeather(DateTime date, IWeatherService weatherService) + { + try { + Weather? weather = await weatherService.GetWeather(date); if (weather is null) return Results.NotFound(); return Results.Ok(weather); - }); + } catch { + return Results.InternalServerError(); + } + } - endpoints.MapPost("/weather", async ([FromBody] Weather weather, IWeatherService weatherService) => - { + private static async Task CreateWeather([FromBody] Weather weather, IWeatherService weatherService) + { + try { Weather? weatherCreated = await weatherService.CreateWeather(weather); + + if (weatherCreated is null) return Results.NotFound(); return Results.Created($"/{weatherCreated?.Id}", weatherCreated); - }); + } catch { + return Results.InternalServerError(); + } + } - endpoints.MapPut("/weather/{weatherId}", async (long weatherId, [FromBody] Weather weather, IWeatherService weatherService) => - { + private static async Task UpdateWeather(long weatherId, [FromBody] Weather weather, IWeatherService weatherService) + { + try { Weather? weatherUpdated = await weatherService.UpdateWeather(weatherId, weather); + + if (weatherUpdated is null) return Results.NotFound(); return Results.Ok(weatherUpdated); - }); + } catch { + return Results.InternalServerError(); + } + } - endpoints.MapDelete("/weather/{weatherId}", async (long weatherId, IWeatherService weatherService) => - { + private static async Task DeleteWeather(long weatherId, IWeatherService weatherService) + { + try { await weatherService.DeleteWeather(weatherId); return Results.Ok(); - }); + } catch { + return Results.InternalServerError(); + } } } \ No newline at end of file diff --git a/template/src/Placeholder.Core/Weather/IWeatherRepository.cs b/template/src/Placeholder.Core/Weather/IWeatherRepository.cs index e3a0344..b405fa7 100644 --- a/template/src/Placeholder.Core/Weather/IWeatherRepository.cs +++ b/template/src/Placeholder.Core/Weather/IWeatherRepository.cs @@ -5,7 +5,7 @@ namespace Placeholder.Core; public interface IWeatherRepository { Task> GetWeathers(); - Task GetWeather(long id); + Task GetWeather(DateTime date); Task CreateWeather(Weather weather); Task UpdateWeather(long id, Weather weather); Task DeleteWeather(long id); diff --git a/template/src/Placeholder.Core/Weather/IWeatherService.cs b/template/src/Placeholder.Core/Weather/IWeatherService.cs index 5fbdc12..299346b 100644 --- a/template/src/Placeholder.Core/Weather/IWeatherService.cs +++ b/template/src/Placeholder.Core/Weather/IWeatherService.cs @@ -20,7 +20,7 @@ public interface IWeatherService /// A task that represents the asynchronous operation. The task result is a objects. /// If the weather is not found, the task result is null. /// - Task GetWeather(long id); + Task GetWeather(DateTime date); /// /// Create a new weather diff --git a/template/src/Placeholder.Core/Weather/WeatherRepository.cs b/template/src/Placeholder.Core/Weather/WeatherRepository.cs index 89962c1..90a52dd 100644 --- a/template/src/Placeholder.Core/Weather/WeatherRepository.cs +++ b/template/src/Placeholder.Core/Weather/WeatherRepository.cs @@ -11,7 +11,7 @@ internal class WeatherRepository(DbConnection connection) : IWeatherRepository public async Task> GetWeathers() { var query = @" - SELECT * + SELECT Date, TemperatureC, Summary FROM Weather "; @@ -19,15 +19,15 @@ FROM Weather return result.ToArray(); } - public async Task GetWeather(long id) + public async Task GetWeather(DateTime date) { var query = @" - SELECT * + SELECT Date, TemperatureC, Summary FROM Weather - WHERE Id = @Id + WHERE Date = @Date "; - return await _dbConnection.QuerySingleOrDefaultAsync(query, new { Id = id }); + return await _dbConnection.QuerySingleOrDefaultAsync(query, new { Date = date }); } public async Task CreateWeather(Weather weather) diff --git a/template/src/Placeholder.Core/Weather/WeatherService.cs b/template/src/Placeholder.Core/Weather/WeatherService.cs index 87adeb6..9582c96 100644 --- a/template/src/Placeholder.Core/Weather/WeatherService.cs +++ b/template/src/Placeholder.Core/Weather/WeatherService.cs @@ -11,9 +11,9 @@ public async Task> GetWeathers() return await _weatherRepository.GetWeathers(); } - public async Task GetWeather(long id) + public async Task GetWeather(DateTime date) { - return await _weatherRepository.GetWeather(id); + return await _weatherRepository.GetWeather(date); } public async Task CreateWeather(Weather weather)