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 13, 2024
1 parent 3facfb0 commit 8e13592
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 48 deletions.
68 changes: 49 additions & 19 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/{date}", GetWeather);
endpoints.MapPost("/weather", CreateWeather);
endpoints.MapPut("/weather", UpdateWeather);
endpoints.MapDelete("/weather/{date}", 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);
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<IResult> 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<IResult> DeleteWeather(DateTime date, IWeatherService weatherService)
{
try {
await weatherService.DeleteWeather(date);
return Results.Ok();
});
} catch {
return Results.InternalServerError();
}
}
}
6 changes: 3 additions & 3 deletions template/src/Placeholder.Core/Weather/IWeatherRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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);
Task<Weather?> UpdateWeather(Weather weather);
Task DeleteWeather(DateTime date);
}
6 changes: 3 additions & 3 deletions 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 All @@ -41,7 +41,7 @@ public interface IWeatherService
/// A task that represents the asynchronous operation. The task result is a <see cref="Weather"/> objects with the informations of the updated weather.
/// If the weather is not found, the task result is <c>null</c>.
/// </returns>
Task<Weather?> UpdateWeather(long id, Weather weather);
Task<Weather?> UpdateWeather(Weather weather);

/// <summary>
/// Delete weather by id
Expand All @@ -50,5 +50,5 @@ public interface IWeatherService
/// <returns>
/// A task that represents the asynchronous operation. The task result is : Void.
/// </returns>
Task DeleteWeather(long id);
Task DeleteWeather(DateTime date);
}
4 changes: 1 addition & 3 deletions template/src/Placeholder.Core/Weather/Weather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
28 changes: 14 additions & 14 deletions template/src/Placeholder.Core/Weather/WeatherRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,58 @@ 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)
{
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<Weather>(query, new {weather.Date, weather.TemperatureC, weather.Summary}) ?? null;
return weatherCreated;
}

public async Task<Weather?> UpdateWeather(long id, Weather weather)
public async Task<Weather?> 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<Weather>(query, new { Id = id, weather.Date, weather.TemperatureC, weather.Summary }) ?? null;
Weather? weatherUpdated = await _dbConnection.QuerySingleOrDefaultAsync<Weather>(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 });
}
}
12 changes: 6 additions & 6 deletions template/src/Placeholder.Core/Weather/WeatherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ 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)
{
return await _weatherRepository.CreateWeather(weather);
}

public async Task<Weather?> UpdateWeather(long id, Weather weather)
public async Task<Weather?> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e13592

Please sign in to comment.