Skip to content

Commit

Permalink
add request validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Gramli committed Jun 26, 2024
1 parent 02f8429 commit 0d0b15b
Show file tree
Hide file tree
Showing 34 changed files with 228 additions and 347 deletions.
12 changes: 12 additions & 0 deletions src/Weather.API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HttpDebug", "HttpDebug", "{
Tests\HttpDebug\debug-tests.http = Tests\HttpDebug\debug-tests.http
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallApiToolkit", "..\..\SmallApiToolkit\src\SmallApiToolkit\SmallApiToolkit.csproj", "{EC8B1CA5-AC84-42C3-8929-091D2EECA74F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallApiToolkit.Core", "..\..\SmallApiToolkit\src\SmallApiToolkit.Core\SmallApiToolkit.Core.csproj", "{E31BFF9D-9C2A-4D4B-96B0-9261D67083EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -104,6 +108,14 @@ Global
{501B5489-717D-49E3-940B-99DDA39F278F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{501B5489-717D-49E3-940B-99DDA39F278F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{501B5489-717D-49E3-940B-99DDA39F278F}.Release|Any CPU.Build.0 = Release|Any CPU
{EC8B1CA5-AC84-42C3-8929-091D2EECA74F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC8B1CA5-AC84-42C3-8929-091D2EECA74F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC8B1CA5-AC84-42C3-8929-091D2EECA74F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC8B1CA5-AC84-42C3-8929-091D2EECA74F}.Release|Any CPU.Build.0 = Release|Any CPU
{E31BFF9D-9C2A-4D4B-96B0-9261D67083EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E31BFF9D-9C2A-4D4B-96B0-9261D67083EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E31BFF9D-9C2A-4D4B-96B0-9261D67083EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E31BFF9D-9C2A-4D4B-96B0-9261D67083EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 13 additions & 11 deletions src/Weather.API/EndpointBuilders/WeatherBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using SmallApiToolkit.Core.Response;
using SmallApiToolkit.Extensions;
using Weather.API.Extensions;
using Weather.Core.Abstractions;
using Weather.Domain.Commands;
using Weather.Domain.Dtos;
using Weather.Domain.Http;
using Weather.Domain.Queries;

namespace Weather.API.EndpointBuilders
Expand All @@ -15,6 +16,7 @@ public static IEndpointRouteBuilder BuildWeatherEndpoints(this IEndpointRouteBui

endpointRouteBuilder
.MapGroup("weather")
.MapVersionGroup(1)
.BuildActualWeatherEndpoints()
.BuildForecastWeatherEndpoints()
.BuildFavoriteWeatherEndpoints();
Expand All @@ -24,21 +26,21 @@ public static IEndpointRouteBuilder BuildWeatherEndpoints(this IEndpointRouteBui

private static IEndpointRouteBuilder BuildActualWeatherEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapGet("v1/current",
endpointRouteBuilder.MapGet("current",
async (double latitude, double longitude, [FromServices] IGetCurrentWeatherHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(new GetCurrentWeatherQuery(latitude, longitude), cancellationToken))
.Produces<DataResponse<CurrentWeatherDto>>()
.ProducesDataResponse<CurrentWeatherDto>()
.WithName("GetCurrentWeather")
.WithTags("Getters");
return endpointRouteBuilder;
}

private static IEndpointRouteBuilder BuildForecastWeatherEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapGet("v1/forecast",
endpointRouteBuilder.MapGet("forecast",
async (double latitude, double longitude, [FromServices] IGetForecastWeatherHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(new GetForecastWeatherQuery(latitude, longitude), cancellationToken))
.Produces<DataResponse<ForecastWeatherDto>>()
.ProducesDataResponse<ForecastWeatherDto>()
.WithName("GetForecastWeather")
.WithTags("Getters");

Expand All @@ -47,24 +49,24 @@ await handler.SendAsync(new GetForecastWeatherQuery(latitude, longitude), cancel

private static IEndpointRouteBuilder BuildFavoriteWeatherEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapGet("v1/favorites",
endpointRouteBuilder.MapGet("favorites",
async ([FromServices] IGetFavoritesHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(EmptyRequest.Instance, cancellationToken))
.Produces<DataResponse<FavoritesWeatherDto>>()
.ProducesDataResponse<FavoritesWeatherDto>()
.WithName("GetFavorites")
.WithTags("Getters");

endpointRouteBuilder.MapPost("v1/favorite",
endpointRouteBuilder.MapPost("favorite",
async ([FromBody] AddFavoriteCommand addFavoriteCommand, [FromServices] IAddFavoriteHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(addFavoriteCommand, cancellationToken))
.Produces<DataResponse<int>>()
.ProducesDataResponse<int>()
.WithName("AddFavorite")
.WithTags("Setters");

endpointRouteBuilder.MapDelete("v1/favorite/{id}",
endpointRouteBuilder.MapDelete("favorite/{id}",
async (int id, [FromServices] IDeleteFavoriteHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(new DeleteFavoriteCommand { Id = id }, cancellationToken))
.Produces<DataResponse<bool>>()
.ProducesDataResponse<bool>()
.WithName("DeleteFavorite")
.WithTags("Delete");

Expand Down
14 changes: 0 additions & 14 deletions src/Weather.API/Extensions/IHandlerExtension.cs

This file was deleted.

56 changes: 0 additions & 56 deletions src/Weather.API/Middlewares/ExceptionMiddleware.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/Weather.API/Weather.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\SmallApiToolkit\src\SmallApiToolkit\SmallApiToolkit.csproj" />
<ProjectReference Include="..\Weather.Core\Weather.Core.csproj" />
<ProjectReference Include="..\Weather.Domain\Weather.Domain.csproj" />
<ProjectReference Include="..\Weather.Infrastructure\Weather.Infrastructure.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/Weather.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"AllowedHosts": "*",
"Weatherbit": {
"BaseUrl": "https://weatherbit-v1-mashape.p.rapidapi.com",
"XRapidAPIKey": "deacce6499mshd341d6b6a2df09bp198bc6jsn760c92c08824",
"XRapidAPIKey": "eb5356699fmshb18e9ea3414122dp1ae825jsnf0e1b81e88d8",
"XRapidAPIHost": "weatherbit-v1-mashape.p.rapidapi.com"
}
}
5 changes: 3 additions & 2 deletions src/Weather.Core/Abstractions/IAddFavoriteHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Weather.Domain.Commands;
using SmallApiToolkit.Core.RequestHandlers;
using Weather.Domain.Commands;

namespace Weather.Core.Abstractions
{
public interface IAddFavoriteHandler : IRequestHandler<int, AddFavoriteCommand>
public interface IAddFavoriteHandler : IHttpRequestHandler<int, AddFavoriteCommand>
{

}
Expand Down
5 changes: 3 additions & 2 deletions src/Weather.Core/Abstractions/IDeleteFavoriteHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Weather.Domain.Commands;
using SmallApiToolkit.Core.RequestHandlers;
using Weather.Domain.Commands;

namespace Weather.Core.Abstractions
{
public interface IDeleteFavoriteHandler : IRequestHandler<bool, DeleteFavoriteCommand>
public interface IDeleteFavoriteHandler : IHttpRequestHandler<bool, DeleteFavoriteCommand>
{
}
}
4 changes: 2 additions & 2 deletions src/Weather.Core/Abstractions/IGetCurrentWeatherHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Weather.Core.Queries;
using SmallApiToolkit.Core.RequestHandlers;
using Weather.Domain.Dtos;
using Weather.Domain.Queries;

namespace Weather.Core.Abstractions
{
public interface IGetCurrentWeatherHandler : IRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery>
public interface IGetCurrentWeatherHandler : IHttpRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery>
{
}
}
7 changes: 4 additions & 3 deletions src/Weather.Core/Abstractions/IGetFavoritesHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Weather.Domain.Dtos;
using Weather.Domain.Http;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Response;
using Weather.Domain.Dtos;

namespace Weather.Core.Abstractions
{
public interface IGetFavoritesHandler : IRequestHandler<FavoritesWeatherDto, EmptyRequest>
public interface IGetFavoritesHandler : IHttpRequestHandler<FavoritesWeatherDto, EmptyRequest>
{
}
}
5 changes: 3 additions & 2 deletions src/Weather.Core/Abstractions/IGetForecastWeatherHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Weather.Domain.Dtos;
using SmallApiToolkit.Core.RequestHandlers;
using Weather.Domain.Dtos;
using Weather.Domain.Queries;

namespace Weather.Core.Abstractions
{
public interface IGetForecastWeatherHandler : IRequestHandler<ForecastWeatherDto, GetForecastWeatherQuery>
public interface IGetForecastWeatherHandler : IHttpRequestHandler<ForecastWeatherDto, GetForecastWeatherQuery>
{
}
}
12 changes: 8 additions & 4 deletions src/Weather.Core/Commands/AddFavoriteHandler.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using Ardalis.GuardClauses;
using Microsoft.Extensions.Logging;
using Validot;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.Response;
using SmallApiToolkit.Core.Validation;
using Weather.Core.Abstractions;
using Weather.Core.Resources;
using Weather.Domain.Commands;
using Weather.Domain.Extensions;
using Weather.Domain.Http;
using Weather.Domain.Logging;

namespace Weather.Core.Commands
{
internal sealed class AddFavoriteHandler : IAddFavoriteHandler
{
private readonly IValidator<AddFavoriteCommand> _addFavoriteCommandValidator;
private readonly IRequestValidator<AddFavoriteCommand> _addFavoriteCommandValidator;
private readonly ILogger<IAddFavoriteHandler> _logger;
private readonly IWeatherCommandsRepository _weatherCommandsRepository;
public AddFavoriteHandler(IWeatherCommandsRepository weatherCommandsRepository, IValidator<AddFavoriteCommand> addFavoriteCommandValidator, ILogger<IAddFavoriteHandler> logger)
public AddFavoriteHandler(
IWeatherCommandsRepository weatherCommandsRepository,
IRequestValidator<AddFavoriteCommand> addFavoriteCommandValidator,
ILogger<IAddFavoriteHandler> logger)
{
_weatherCommandsRepository = Guard.Against.Null(weatherCommandsRepository);
_addFavoriteCommandValidator = Guard.Against.Null(addFavoriteCommandValidator);
Expand Down
11 changes: 6 additions & 5 deletions src/Weather.Core/Commands/DeleteFavoriteHandler.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using Ardalis.GuardClauses;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.Response;
using SmallApiToolkit.Core.Validation;
using Validot;
using Weather.Core.Abstractions;
using Weather.Core.Resources;
using Weather.Domain.Commands;
using Weather.Domain.Extensions;
using Weather.Domain.Http;

namespace Weather.Core.Commands
{
internal sealed class DeleteFavoriteHandler : IDeleteFavoriteHandler
{
private readonly IWeatherCommandsRepository _weatherCommandsRepository;
private readonly IValidator<DeleteFavoriteCommand> _validator;
private readonly IRequestValidator<DeleteFavoriteCommand> _validator;

public DeleteFavoriteHandler(
IWeatherCommandsRepository weatherCommandsRepository,
IValidator<DeleteFavoriteCommand> validator)
IWeatherCommandsRepository weatherCommandsRepository,
IRequestValidator<DeleteFavoriteCommand> validator)
{
_weatherCommandsRepository = Guard.Against.Null(weatherCommandsRepository);
_validator = Guard.Against.Null(validator);
Expand Down
19 changes: 10 additions & 9 deletions src/Weather.Core/Configuration/ContainerConfigurationExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Validation;
using Validot;
using Weather.Core.Abstractions;
using Weather.Core.Commands;
Expand All @@ -8,7 +10,6 @@
using Weather.Domain.Commands;
using Weather.Domain.Dtos;
using Weather.Domain.Queries;
using Wheaterbit.Client.Validation;

namespace Weather.Core.Configuration
{
Expand All @@ -21,20 +22,20 @@ public static IServiceCollection AddCore(this IServiceCollection serviceCollecti

private static IServiceCollection AddHandlers(this IServiceCollection serviceCollection)
=> serviceCollection
.AddScoped<IGetCurrentWeatherHandler, GetCurrentWeatherHandler>()
.AddScoped<IHttpRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery>, GetCurrentWeatherHandler>()
.AddScoped<IGetFavoritesHandler, GetFavoritesHandler>()
.AddScoped<IGetForecastWeatherHandler, GetForecastWeatherHandler>()
.AddScoped<IAddFavoriteHandler, AddFavoriteHandler>()
.AddScoped<IDeleteFavoriteHandler, DeleteFavoriteHandler>();

private static IServiceCollection AddValidation(this IServiceCollection serviceCollection)
=> serviceCollection
.AddValidotSingleton<IValidator<CurrentWeatherDto>, CurrentWeatherDtoSpecificationHolder, CurrentWeatherDto>()
.AddValidotSingleton<IValidator<ForecastWeatherDto>, ForecastWeatherDtoSpecificationHolder, ForecastWeatherDto>()
.AddValidotSingleton<IValidator<LocationDto>, LocationDtoSpecificationHolder, LocationDto>()
.AddValidotSingleton<IValidator<AddFavoriteCommand>, AddFavoriteCommandSpecificationHolder, AddFavoriteCommand>()
.AddValidotSingleton<IValidator<GetCurrentWeatherQuery>, GetCurrentWeatherQuerySpecificationHolder, GetCurrentWeatherQuery>()
.AddValidotSingleton<IValidator<GetForecastWeatherQuery>, GetForecastWeatherSpecificationHolder, GetForecastWeatherQuery>()
.AddValidotSingleton<IValidator<DeleteFavoriteCommand>, DeleteFavoriteCommandSpecificationHolder, DeleteFavoriteCommand>();
.AddValidotSingleton<IValidator<CurrentWeatherDto>, CurrentWeatherDtoValidator, CurrentWeatherDto>()
.AddValidotSingleton<IValidator<ForecastWeatherDto>, ForecastWeatherDtoValidator, ForecastWeatherDto>()
.AddValidotSingleton<IValidator<LocationDto>, LocationDtoValidator, LocationDto>()
.AddSingleton<IRequestValidator<AddFavoriteCommand>, AddFavoriteCommandValidator>()
.AddValidotSingleton<IValidator<GetCurrentWeatherQuery>, GetCurrentWeatherQueryValidator, GetCurrentWeatherQuery>()
.AddValidotSingleton<IValidator<GetForecastWeatherQuery>, GetForecastWeatherValidator, GetForecastWeatherQuery>()
.AddValidotSingleton<IValidator<DeleteFavoriteCommand>, DeleteFavoriteCommandValidator, DeleteFavoriteCommand>();
}
}
Loading

0 comments on commit 0d0b15b

Please sign in to comment.