Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/small api toolkit #2

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ And lastly, the most important sentence at the end: **the architecture of a proj
## Technologies
* [ASP.NET Core 8](https://learn.microsoft.com/en-us/aspnet/core/introduction-to-aspnet-core?view=aspnetcore-8.0)
* [Entity Framework Core InMemory](https://learn.microsoft.com/en-us/ef/core/providers/in-memory/?tabs=dotnet-core-cli)
* [SmallApiToolkit](https://github.com/Gramli/SmallApiToolkit)
* [AutoMapper](https://github.com/AutoMapper/AutoMapper)
* [FluentResuls](https://github.com/altmann/FluentResults)
* [Validot](https://github.com/bartoszlenar/Validot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
using SmallApiToolkit.Core.RequestHandlers;
using System.Net;
using Validot;
using Weather.API.Domain.Abstractions;
Expand All @@ -22,7 +23,7 @@ public class AddFavoriteHandlerTests
private readonly Mock<IMapper> _mapperMock;
private readonly Mock<DbSet<FavoriteLocationEntity>> _favoriteLocationEntityDbSetMock;

private readonly IRequestHandler<int, AddFavoriteCommand> _uut;
private readonly IHttpRequestHandler<int, AddFavoriteCommand> _uut;
public AddFavoriteHandlerTests()
{
_favoriteLocationEntityDbSetMock = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
using SmallApiToolkit.Core.RequestHandlers;
using System.Net;
using Validot;
using Weather.API.Domain.Abstractions;
Expand All @@ -17,7 +18,7 @@ public class DeleteFavoriteHandlerTests
private readonly Mock<TestWeatherContext> _weatherContextMock;
private readonly Mock<DbSet<FavoriteLocationEntity>> _favoriteLocationEntityDbSetMock;

private readonly IRequestHandler<bool, DeleteFavoriteCommand> _uut;
private readonly IHttpRequestHandler<bool, DeleteFavoriteCommand> _uut;
public DeleteFavoriteHandlerTests()
{
_deleteFavoriteCommandValidatorMock = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentResults;
using Microsoft.Extensions.Logging;
using Moq;
using SmallApiToolkit.Core.RequestHandlers;
using System.Net;
using Validot;
using Validot.Results;
Expand All @@ -20,7 +21,7 @@ public class GetCurrentWeatherHandlerTests
private readonly Mock<IWeatherService> _weatherServiceMock;
private readonly Mock<ILogger<GetCurrentWeatherHandler>> _loggerMock;

private readonly IRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery> _uut;
private readonly IHttpRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery> _uut;
public GetCurrentWeatherHandlerTests()
{
_getCurrentWeatherQueryValidatorMock = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Response;
using System.Net;
using Validot;
using Weather.API.Domain.Abstractions;
Expand All @@ -12,7 +14,6 @@
using Weather.API.Features.Favorites.GetFavorites;
using Weather.API.UnitTests.Domain.Database;
using Weather.API.UnitTests.TestExtensions;
using WeatherApi.Domain.Http;

namespace Weather.API.UnitTests.Features.GetFavorites
{
Expand All @@ -26,7 +27,7 @@ public class GetFavoritesHandlerTests
private readonly Mock<IMapper> _mapperMock;
private readonly Mock<DbSet<FavoriteLocationEntity>> _favoriteLocationEntityDbSetMock;

private readonly IRequestHandler<FavoritesWeatherDto, EmptyRequest> _uut;
private readonly IHttpRequestHandler<FavoritesWeatherDto, EmptyRequest> _uut;
public GetFavoritesHandlerTests()
{
_weatherServiceMock = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentResults;
using Microsoft.Extensions.Logging;
using Moq;
using SmallApiToolkit.Core.RequestHandlers;
using System.Net;
using Validot;
using Validot.Results;
Expand All @@ -20,7 +21,7 @@ public class GetForecastWeatherHandlerTests
private readonly Mock<IWeatherService> _weatherServiceMock;
private readonly Mock<ILogger<GetForecastWeatherHandler>> _loggerMock;

private readonly IRequestHandler<ForecastWeatherDto, GetForecastWeatherQuery> _uut;
private readonly IHttpRequestHandler<ForecastWeatherDto, GetForecastWeatherQuery> _uut;
public GetForecastWeatherHandlerTests()
{
_getForecastWeatherQueryValidatorMock = new Mock<IValidator<GetForecastWeatherQuery>>();
Expand Down
9 changes: 0 additions & 9 deletions src/Weather.API/Domain/Abstractions/IRequestHandler.cs

This file was deleted.

70 changes: 0 additions & 70 deletions src/Weather.API/Domain/Extensions/HttpDataResponses.cs

This file was deleted.

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

This file was deleted.

9 changes: 0 additions & 9 deletions src/Weather.API/Domain/Http/DataResponse.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/Weather.API/Domain/Http/EmptyRequest.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/Weather.API/Domain/Http/HttpDataResponse.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/Weather.API/Features/AddFavorites/AddFavoriteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
using AutoMapper;
using FluentResults;
using Microsoft.EntityFrameworkCore;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Response;
using Validot;
using Weather.API.Domain.Abstractions;
using Weather.API.Domain.Database.EFContext;
using Weather.API.Domain.Database.Entities;
using Weather.API.Domain.Extensions;
using Weather.API.Domain.Logging;
using Weather.API.Domain.Resources;
using WeatherApi.Domain.Http;

namespace Weather.API.Features.Favorites.AddFavorites
{
internal sealed class AddFavoriteHandler : IRequestHandler<int, AddFavoriteCommand>
internal sealed class AddFavoriteHandler : IHttpRequestHandler<int, AddFavoriteCommand>
{
private readonly IMapper _mapper;
private readonly WeatherContext _weatherContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Extensions;
using Validot;
using Weather.API.Domain.Abstractions;
using Weather.API.Domain.Extensions;
using Weather.API.Features.Favorites.AddFavorites;
using WeatherApi.Domain.Http;

namespace Weather.API.Features.AddFavorites
{
public static class ContainerConfigurationExtension
{
public static IEndpointRouteBuilder BuildAddFavoriteWeatherEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapPost("v1/favorite",
async ([FromBody] AddFavoriteCommand addFavoriteCommand, [FromServices] IRequestHandler<int, AddFavoriteCommand> handler, CancellationToken cancellationToken) =>
endpointRouteBuilder.MapPost("favorite",
async ([FromBody] AddFavoriteCommand addFavoriteCommand, [FromServices] IHttpRequestHandler<int, AddFavoriteCommand> handler, CancellationToken cancellationToken) =>
await handler.SendAsync(addFavoriteCommand, cancellationToken))
.Produces<DataResponse<int>>()
.ProducesDataResponse<int>()
.WithName("AddFavorite")
.WithTags("Setters");

Expand All @@ -23,7 +24,7 @@ await handler.SendAsync(addFavoriteCommand, cancellationToken))

public static IServiceCollection AddAddFavorites(this IServiceCollection serviceCollection)
=> serviceCollection
.AddScoped<IRequestHandler<int, AddFavoriteCommand>, AddFavoriteHandler>()
.AddScoped<IHttpRequestHandler<int, AddFavoriteCommand>, AddFavoriteHandler>()
.AddValidotSingleton<IValidator<AddFavoriteCommand>, AddFavoriteCommandSpecificationHolder, AddFavoriteCommand>();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using Microsoft.AspNetCore.Mvc;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Extensions;
using Validot;
using Weather.API.Domain.Abstractions;
using Weather.API.Domain.Extensions;
using WeatherApi.Domain.Http;

namespace Weather.API.Features.DeleteFavorites
{
public static class ContainerConfigurationExtension
{
public static IEndpointRouteBuilder BuildDeleteFavoriteWeatherEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapDelete("v1/favorite/{id}",
async (int id, [FromServices] IRequestHandler<bool, DeleteFavoriteCommand> handler, CancellationToken cancellationToken) =>
endpointRouteBuilder.MapDelete("favorite/{id}",
async (int id, [FromServices] IHttpRequestHandler<bool, DeleteFavoriteCommand> handler, CancellationToken cancellationToken) =>
await handler.SendAsync(new DeleteFavoriteCommand { Id = id }, cancellationToken))
.Produces<DataResponse<bool>>()
.ProducesDataResponse<bool>()
.WithName("DeleteFavorite")
.WithTags("Delete");

Expand All @@ -22,7 +23,7 @@ await handler.SendAsync(new DeleteFavoriteCommand { Id = id }, cancellationToken

public static IServiceCollection AddDeleteFavorites(this IServiceCollection serviceCollection)
=> serviceCollection
.AddScoped<IRequestHandler<bool, DeleteFavoriteCommand>, DeleteFavoriteHandler>()
.AddScoped<IHttpRequestHandler<bool, DeleteFavoriteCommand>, DeleteFavoriteHandler>()
.AddValidotSingleton<IValidator<DeleteFavoriteCommand>, DeleteFavoriteCommandSpecificationHolder, DeleteFavoriteCommand>();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Ardalis.GuardClauses;
using FluentResults;
using Microsoft.EntityFrameworkCore;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Response;
using Validot;
using Weather.API.Domain.Abstractions;
using Weather.API.Domain.Database.EFContext;
using Weather.API.Domain.Extensions;
using Weather.API.Domain.Logging;
using Weather.API.Domain.Resources;
using WeatherApi.Domain.Http;

namespace Weather.API.Features.DeleteFavorites
{
internal sealed class DeleteFavoriteHandler : IRequestHandler<bool, DeleteFavoriteCommand>
internal sealed class DeleteFavoriteHandler : IHttpRequestHandler<bool, DeleteFavoriteCommand>
{
private readonly WeatherContext _weatherContext;
private readonly IValidator<DeleteFavoriteCommand> _validator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Extensions;
using Validot;
using Weather.API.Domain.Abstractions;
using Weather.API.Domain.Dtos;
using Weather.API.Domain.Extensions;
using WeatherApi.Domain.Http;

namespace Weather.API.Features.Weather.GetCurrent
{
public static class ContainerConfigurationExtension
{
public static IEndpointRouteBuilder BuildGetCurrentWeatherEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapGet("v1/current",
async (double latitude, double longitude, [FromServices] IRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery> handler, CancellationToken cancellationToken) =>
endpointRouteBuilder.MapGet("current",
async (double latitude, double longitude, [FromServices] IHttpRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery> handler, CancellationToken cancellationToken) =>
await handler.SendAsync(new GetCurrentWeatherQuery(latitude, longitude), cancellationToken))
.Produces<DataResponse<CurrentWeatherDto>>()
.ProducesDataResponse<CurrentWeatherDto>()
.WithName("GetCurrentWeather")
.WithTags("Getters");
return endpointRouteBuilder;
}

public static IServiceCollection AddGetCurrentWeather(this IServiceCollection serviceCollection)
=> serviceCollection
.AddScoped<IRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery>, GetCurrentWeatherHandler>()
.AddScoped<IHttpRequestHandler<CurrentWeatherDto, GetCurrentWeatherQuery>, GetCurrentWeatherHandler>()
.AddValidotSingleton<IValidator<CurrentWeatherDto>, CurrentWeatherDtoSpecificationHolder, CurrentWeatherDto>()
.AddValidotSingleton<IValidator<GetCurrentWeatherQuery>, GetCurrentWeatherQuerySpecificationHolder, GetCurrentWeatherQuery>();

Expand Down
Loading
Loading