Skip to content

Commit

Permalink
feat(311699): Refacto weatherEndpoints.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey-Dulac committed Dec 12, 2024
1 parent 3facfb0 commit 6bc4b03
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
72 changes: 41 additions & 31 deletions template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,46 @@ public static class WeatherEndpoints
{
public static void MapWeatherEndpoints(this IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/weathers", async (IWeatherService weatherService) =>
{
IEnumerable<Weather> weathers = await weatherService.GetWeathers();
return Results.Ok(weathers);
});

endpoints.MapGet("/weather/{weatherId}", async (long weatherId, IWeatherService weatherService) =>
{
Weather? weather = await weatherService.GetWeather(weatherId);

if (weather is null) return Results.NotFound();
return Results.Ok(weather);
});

endpoints.MapPost("/weather", async ([FromBody] Weather weather, IWeatherService weatherService) =>
{
Weather? weatherCreated = await weatherService.CreateWeather(weather);
return Results.Created($"/{weatherCreated?.Id}", weatherCreated);
});

endpoints.MapPut("/weather/{weatherId}", async (long weatherId, [FromBody] Weather weather, IWeatherService weatherService) =>
{
Weather? weatherUpdated = await weatherService.UpdateWeather(weatherId, weather);
return Results.Ok(weatherUpdated);
});

endpoints.MapDelete("/weather/{weatherId}", async (long weatherId, IWeatherService weatherService) =>
{
await weatherService.DeleteWeather(weatherId);
return Results.Ok();
});
endpoints.MapGet("/weathers", GetAllWeathers);
endpoints.MapGet("/weather/{weatherId}", GetWeatherById);
endpoints.MapPost("/weather", CreateWeather);
endpoints.MapPut("/weather/{weatherId}", UpdateWeather);
endpoints.MapDelete("/weather/{weatherId}", DeleteWeather);
}

private static async Task<IResult> GetAllWeathers(IWeatherService weatherService)
{
IEnumerable<Weather> weathers = await weatherService.GetWeathers();
return Results.Ok(weathers);
}

private static async Task<IResult> GetWeatherById(long weatherId, IWeatherService weatherService)
{
Weather? weather = await weatherService.GetWeather(weatherId);

if (weather is null) return Results.NotFound();
return Results.Ok(weather);
}

private static async Task<IResult> CreateWeather([FromBody] Weather weather, IWeatherService weatherService)
{
Weather? weatherCreated = await weatherService.CreateWeather(weather);

if (weatherCreated is null) return Results.NotFound();
return Results.Created($"/{weatherCreated?.Id}", weatherCreated);
}

private static async Task<IResult> UpdateWeather(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);
}

private static async Task<IResult> DeleteWeather(long weatherId, IWeatherService weatherService)
{
await weatherService.DeleteWeather(weatherId);
return Results.Ok();
}
}
4 changes: 2 additions & 2 deletions template/src/Placeholder.Core/Weather/WeatherRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class WeatherRepository(DbConnection connection) : IWeatherRepository
public async Task<IEnumerable<Weather>> GetWeathers()
{
var query = @"
SELECT *
SELECT Date, TemperatureC, Summary
FROM Weather
";

Expand All @@ -22,7 +22,7 @@ FROM Weather
public async Task<Weather?> GetWeather(long id)
{
var query = @"
SELECT *
SELECT Date, TemperatureC, Summary
FROM Weather
WHERE Id = @Id
";
Expand Down

0 comments on commit 6bc4b03

Please sign in to comment.