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 ec692b1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
62 changes: 46 additions & 16 deletions template/src/Placeholder.ApiService/Endpoints/WeatherEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IResult> GetAllWeathers(IWeatherService weatherService)
{
try {
IEnumerable<Weather> 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<IResult> 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<IResult> 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<IResult> 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<IResult> DeleteWeather(long weatherId, IWeatherService weatherService)
{
try {
await weatherService.DeleteWeather(weatherId);
return Results.Ok();
});
} catch {
return Results.InternalServerError();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Placeholder.Core;
public interface IWeatherRepository
{
Task<IEnumerable<Weather>> GetWeathers();
Task<Weather?> GetWeather(long id);
Task<Weather?> GetWeather(DateTime date);
Task<Weather?> CreateWeather(Weather weather);
Task<Weather?> UpdateWeather(long id, Weather weather);
Task DeleteWeather(long id);
Expand Down
2 changes: 1 addition & 1 deletion template/src/Placeholder.Core/Weather/IWeatherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IWeatherService
/// A task that represents the asynchronous operation. The task result is a <see cref="Weather"/> objects.
/// If the weather is not found, the task result is <c>null</c>.
/// </returns>
Task<Weather?> GetWeather(long id);
Task<Weather?> GetWeather(DateTime date);

/// <summary>
/// Create a new weather
Expand Down
10 changes: 5 additions & 5 deletions template/src/Placeholder.Core/Weather/WeatherRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ internal class WeatherRepository(DbConnection connection) : IWeatherRepository
public async Task<IEnumerable<Weather>> GetWeathers()
{
var query = @"
SELECT *
SELECT Date, TemperatureC, Summary
FROM Weather
";

var result = await _dbConnection.QueryAsync<Weather>(query);
return result.ToArray();
}

public async Task<Weather?> GetWeather(long id)
public async Task<Weather?> GetWeather(DateTime date)
{
var query = @"
SELECT *
SELECT Date, TemperatureC, Summary
FROM Weather
WHERE Id = @Id
WHERE Date = @Date
";

return await _dbConnection.QuerySingleOrDefaultAsync<Weather>(query, new { Id = id });
return await _dbConnection.QuerySingleOrDefaultAsync<Weather>(query, new { Date = date });
}

public async Task<Weather?> CreateWeather(Weather weather)
Expand Down
4 changes: 2 additions & 2 deletions template/src/Placeholder.Core/Weather/WeatherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public async Task<IEnumerable<Weather>> GetWeathers()
return await _weatherRepository.GetWeathers();
}

public async Task<Weather?> GetWeather(long id)
public async Task<Weather?> GetWeather(DateTime date)
{
return await _weatherRepository.GetWeather(id);
return await _weatherRepository.GetWeather(date);
}

public async Task<Weather?> CreateWeather(Weather weather)
Expand Down

0 comments on commit ec692b1

Please sign in to comment.