From 8e135925d95dcaafbc4bb5dfed93251059ac206b 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 | 68 +++++++++++++------ .../Weather/IWeatherRepository.cs | 6 +- .../Weather/IWeatherService.cs | 6 +- .../src/Placeholder.Core/Weather/Weather.cs | 4 +- .../Weather/WeatherRepository.cs | 28 ++++---- .../Weather/WeatherService.cs | 12 ++-- .../11/20241108112106_InitialWeatherData.cs | 5 ++ 7 files changed, 81 insertions(+), 48 deletions(-) diff --git a/template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs b/template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs index c477469..2d76f7c 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/{date}", GetWeather); + endpoints.MapPost("/weather", CreateWeather); + endpoints.MapPut("/weather", UpdateWeather); + endpoints.MapDelete("/weather/{date}", 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); - return Results.Created($"/{weatherCreated?.Id}", weatherCreated); - }); + + if (weatherCreated is null) return Results.NotFound(); + return Results.Created($"weather/{weatherCreated?.Date}", weatherCreated); + } catch { + return Results.InternalServerError(); + } + } + + private static async Task UpdateWeather([FromBody] Weather weather, IWeatherService weatherService) + { + try { + Weather? weatherUpdated = await weatherService.UpdateWeather(weather); - endpoints.MapPut("/weather/{weatherId}", async (long weatherId, [FromBody] Weather weather, IWeatherService weatherService) => - { - 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) => - { - await weatherService.DeleteWeather(weatherId); + private static async Task DeleteWeather(DateTime date, IWeatherService weatherService) + { + try { + await weatherService.DeleteWeather(date); 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..0817b9f 100644 --- a/template/src/Placeholder.Core/Weather/IWeatherRepository.cs +++ b/template/src/Placeholder.Core/Weather/IWeatherRepository.cs @@ -5,8 +5,8 @@ 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); + Task UpdateWeather(Weather weather); + Task DeleteWeather(DateTime date); } \ No newline at end of file diff --git a/template/src/Placeholder.Core/Weather/IWeatherService.cs b/template/src/Placeholder.Core/Weather/IWeatherService.cs index 5fbdc12..ee094ee 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 @@ -41,7 +41,7 @@ public interface IWeatherService /// A task that represents the asynchronous operation. The task result is a objects with the informations of the updated weather. /// If the weather is not found, the task result is null. /// - Task UpdateWeather(long id, Weather weather); + Task UpdateWeather(Weather weather); /// /// Delete weather by id @@ -50,5 +50,5 @@ public interface IWeatherService /// /// A task that represents the asynchronous operation. The task result is : Void. /// - Task DeleteWeather(long id); + Task DeleteWeather(DateTime date); } \ No newline at end of file diff --git a/template/src/Placeholder.Core/Weather/Weather.cs b/template/src/Placeholder.Core/Weather/Weather.cs index a638741..b9f2737 100644 --- a/template/src/Placeholder.Core/Weather/Weather.cs +++ b/template/src/Placeholder.Core/Weather/Weather.cs @@ -4,15 +4,13 @@ public class Weather { public Weather() { } - public Weather(int id, DateTime date, int temperatureC, string? summary) + public Weather(DateTime date, int temperatureC, string? summary) { - Id = id; Date = date; TemperatureC = temperatureC; Summary = summary; } - public int Id { get; set; } public DateTime? Date { get; set; } public int TemperatureC { get; set; } diff --git a/template/src/Placeholder.Core/Weather/WeatherRepository.cs b/template/src/Placeholder.Core/Weather/WeatherRepository.cs index 89962c1..f109aad 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) @@ -35,34 +35,34 @@ FROM Weather var query = @" INSERT INTO Weather (Date, TemperatureC, Summary) VALUES (@Date, @TemperatureC, @Summary) - RETURNING Id, Date, TemperatureC, Summary + RETURNING Date, TemperatureC, Summary "; Weather? weatherCreated = await _dbConnection.QuerySingleOrDefaultAsync(query, new {weather.Date, weather.TemperatureC, weather.Summary}) ?? null; return weatherCreated; } - public async Task UpdateWeather(long id, Weather weather) + public async Task UpdateWeather(Weather weather) { var query = @" UPDATE Weather - SET Date = @Date, TemperatureC = @TemperatureC, Summary = @Summary - WHERE Id = @Id - RETURNING Id, Date, TemperatureC, Summary + SET TemperatureC = @TemperatureC, Summary = @Summary + WHERE Date = @Date + RETURNING Date, TemperatureC, Summary "; - Weather? weatherUpdated = await _dbConnection.QuerySingleOrDefaultAsync(query, new { Id = id, weather.Date, weather.TemperatureC, weather.Summary }) ?? null; + Weather? weatherUpdated = await _dbConnection.QuerySingleOrDefaultAsync(query, new { weather.Date, weather.TemperatureC, weather.Summary }) ?? null; return weatherUpdated; } - public async Task DeleteWeather(long id) + public async Task DeleteWeather(DateTime date) { var query = @" DELETE FROM Weather - WHERE Id = @Id + WHERE Date = @Date "; - await _dbConnection.ExecuteAsync(query, new { Id = id }); + await _dbConnection.ExecuteAsync(query, new { Date = date }); } } \ No newline at end of file diff --git a/template/src/Placeholder.Core/Weather/WeatherService.cs b/template/src/Placeholder.Core/Weather/WeatherService.cs index 87adeb6..98cc5ca 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) @@ -21,13 +21,13 @@ public async Task> GetWeathers() return await _weatherRepository.CreateWeather(weather); } - public async Task UpdateWeather(long id, Weather weather) + public async Task UpdateWeather(Weather weather) { - return await _weatherRepository.UpdateWeather(id, weather); + return await _weatherRepository.UpdateWeather(weather); } - public async Task DeleteWeather(long id) + public async Task DeleteWeather(DateTime date) { - await _weatherRepository.DeleteWeather(id); + await _weatherRepository.DeleteWeather(date); } } \ No newline at end of file diff --git a/template/src/Placeholder.Migration/Migrations/2024/11/20241108112106_InitialWeatherData.cs b/template/src/Placeholder.Migration/Migrations/2024/11/20241108112106_InitialWeatherData.cs index 1ab2ed9..ac252d5 100644 --- a/template/src/Placeholder.Migration/Migrations/2024/11/20241108112106_InitialWeatherData.cs +++ b/template/src/Placeholder.Migration/Migrations/2024/11/20241108112106_InitialWeatherData.cs @@ -18,6 +18,11 @@ Summary VARCHAR(255) ); "); + // Create a unique index on the Date column + Execute.Sql(@" + CREATE UNIQUE INDEX IX_Weather_Date ON Weather(Date); + "); + // Insert some test data Execute.Sql(@" INSERT INTO Weather (Date, TemperatureC, Summary) VALUES