Skip to content

Commit

Permalink
migration to small api toolkit (#2)
Browse files Browse the repository at this point in the history
* migration to small api toolkit

* update packages & add yamlDotNet

ChoETL.Yaml has problems with dynamic objects

* fix codacy
  • Loading branch information
Gramli committed Jun 19, 2024
1 parent b4a0dc9 commit 7eb243d
Show file tree
Hide file tree
Showing 58 changed files with 199 additions and 377 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Solution contains four layers:
## Technologies
* [ASP.NET Core 7](https://learn.microsoft.com/en-us/aspnet/core/introduction-to-aspnet-core?view=aspnetcore-7.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)
* [Mapster](https://github.com/MapsterMapper/Mapster)
* [FluentResuls](https://github.com/altmann/FluentResults)
* [Validot](https://github.com/bartoszlenar/Validot)
Expand Down
27 changes: 15 additions & 12 deletions src/File.API/EndpointBuilders/FileEndpointsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using File.Core.Queries;
using File.Domain.Commands;
using File.Domain.Dtos;
using File.Domain.Http;
using File.Domain.Queries;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.Response;
using SmallApiToolkit.Extensions;

namespace File.API.EndpointBuilders
{
Expand All @@ -17,6 +19,7 @@ public static IEndpointRouteBuilder BuildFileEndpoints(this IEndpointRouteBuilde
{
return endpointRouteBuilder
.MapGroup("file")
.MapVersionGroup(1)
.BuildUploadEndpoints()
.BuildDownloadEndpoints()
.BuildGetEndpoints()
Expand All @@ -26,31 +29,31 @@ public static IEndpointRouteBuilder BuildFileEndpoints(this IEndpointRouteBuilde

private static IEndpointRouteBuilder BuildUploadEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapPost("v1/upload",
endpointRouteBuilder.MapPost("upload",
async (IFormFileCollection files, [FromServices] IAddFilesCommandHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(new AddFilesCommand(files.Select(file => new FormFileProxy(file))), cancellationToken))
.DisableAntiforgery()
.Produces<DataResponse<bool>>()
.ProducesDataResponse<bool>()
.WithName("AddFiles")
.WithTags("Post");
return endpointRouteBuilder;
}

private static IEndpointRouteBuilder BuildDownloadEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapGet("v1/download",
endpointRouteBuilder.MapGet("download",
async (int id, [FromServices] IDownloadFileQueryHandler handler, CancellationToken cancellationToken) =>
await handler.GetFileAsync(new DownloadFileQuery(id), cancellationToken))
.DisableAntiforgery()
.Produces<FileContentHttpResult>()
.WithName("DownloadFile")
.WithTags("Get");

endpointRouteBuilder.MapGet("v1/downloadAsJson",
endpointRouteBuilder.MapGet("downloadAsJson",
async (int id, [FromServices] IDownloadFileQueryHandler handler, CancellationToken cancellationToken) =>
await handler.GetJsonFileAsync(new DownloadFileQuery(id), cancellationToken))
.DisableAntiforgery()
.Produces<DataResponse<StringContentFileDto>>()
.ProducesDataResponse<StringContentFileDto>()
.WithName("DownloadFileAsJson")
.WithTags("Get");

Expand All @@ -59,34 +62,34 @@ await handler.GetJsonFileAsync(new DownloadFileQuery(id), cancellationToken))

private static IEndpointRouteBuilder BuildGetEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapGet("v1/files-info",
endpointRouteBuilder.MapGet("files-info",
async ([FromServices] IGetFilesInfoQueryHandler handler, CancellationToken cancellationToken) =>
await handler.SendAsync(EmptyRequest.Instance, cancellationToken))
.Produces<DataResponse<IEnumerable<FileInfoDto>>>()
.ProducesDataResponse<IEnumerable<FileInfoDto>>()
.WithName("GetFilesInfo")
.WithTags("Get");
return endpointRouteBuilder;
}

private static IEndpointRouteBuilder BuildParseEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapPost("v1/export",
endpointRouteBuilder.MapPost("export",
async ([FromBody]ExportFileQuery parseFileQuery,[FromServices] IExportFileQueryHandler handler, CancellationToken cancellationToken) =>
await handler.GetFileAsync(parseFileQuery, cancellationToken))
.DisableAntiforgery()
.Produces<FileContentHttpResult>()
.ProducesDataResponse<FileContentHttpResult>()
.WithName("Export")
.WithTags("Post");
return endpointRouteBuilder;
}

private static IEndpointRouteBuilder BuildExportEndpoints(this IEndpointRouteBuilder endpointRouteBuilder)
{
endpointRouteBuilder.MapPost("v1/convert",
endpointRouteBuilder.MapPost("convert",
async (IFormFile file, string formatToExport, [FromServices] IConvertToQueryHandler handler, CancellationToken cancellationToken) =>
await handler.GetFileAsync(new ConvertToQuery(new FormFileProxy(file), formatToExport), cancellationToken))
.DisableAntiforgery()
.Produces<FileContentHttpResult>()
.ProducesDataResponse<FileContentHttpResult>()
.WithName("ConvertTo")
.WithTags("Post");
return endpointRouteBuilder;
Expand Down
25 changes: 4 additions & 21 deletions src/File.API/Extensions/IHandlerExtension.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
using File.Core.Abstractions;
using File.Domain.Dtos;
using File.Domain.Http;
using System.Buffers.Text;
using File.Domain.Dtos;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Response;

namespace File.API.Extensions
{
internal static class IHandlerExtension
{
internal static async Task<IResult> SendAsync<TResponse, TRequest>(this IRequestHandler<TResponse, TRequest> requestHandler, TRequest request, CancellationToken cancellationToken)
{
var response = await requestHandler.HandleAsync(request, cancellationToken);
return Results.Json(response, statusCode: (int)response.StatusCode);
}

internal static async Task<IResult> GetFileAsync<TResponse, TRequest>(this IRequestHandler<TResponse, TRequest> requestHandler, TRequest request, CancellationToken cancellationToken) where TResponse : FileDto
{
var response = await requestHandler.HandleAsync(request, cancellationToken);
if(response.Data is not null)
{
return Results.File(response.Data.Data, response.Data.ContentType, response.Data.FileName);
}
return Results.NotFound();
}

internal static async Task<IResult> GetJsonFileAsync<TResponse, TRequest>(this IRequestHandler<TResponse, TRequest> requestHandler, TRequest request, CancellationToken cancellationToken) where TResponse : FileDto
internal static async Task<IResult> GetJsonFileAsync<TResponse, TRequest>(this IHttpRequestHandler<TResponse, TRequest> requestHandler, TRequest request, CancellationToken cancellationToken) where TResponse : FileDto
{
var response = await requestHandler.HandleAsync(request, cancellationToken);
if (response.Data is not null)
Expand Down
7 changes: 4 additions & 3 deletions src/File.API/File.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="4.2.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Ardalis.GuardClauses" Version="4.5.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="SmallApiToolkit" Version="1.0.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/File.API/Files/FormFileProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace File.API.Files
{
internal sealed class FormFileProxy : IFile
internal sealed class FormFileProxy : IFileProxy
{
private readonly IFormFile _formFile;
public string ContentType => _formFile.ContentType;
Expand Down
56 changes: 0 additions & 56 deletions src/File.API/Midlewares/ExceptionMiddleware.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/File.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using File.API.Configuration;
using File.API.EndpointBuilders;
using File.API.Extensions;
using File.API.Middlewares;
using File.Core.Configuration;
using File.Infrastructure.Configuration;
using SmallApiToolkit.Middleware;

var builder = WebApplication.CreateBuilder(args);

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

namespace File.Core.Abstractions
{
public interface IAddFilesCommandHandler : IRequestHandler<bool, AddFilesCommand>
public interface IAddFilesCommandHandler : IHttpRequestHandler<bool, AddFilesCommand>
{
}
}
3 changes: 2 additions & 1 deletion src/File.Core/Abstractions/IConvertToQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using File.Core.Queries;
using File.Domain.Dtos;
using SmallApiToolkit.Core.RequestHandlers;

namespace File.Core.Abstractions
{
public interface IConvertToQueryHandler : IRequestHandler<FileDto, ConvertToQuery>
public interface IConvertToQueryHandler : IHttpRequestHandler<FileDto, ConvertToQuery>
{
}
}
3 changes: 2 additions & 1 deletion src/File.Core/Abstractions/IDownloadFileQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using File.Domain.Dtos;
using File.Domain.Queries;
using SmallApiToolkit.Core.RequestHandlers;

namespace File.Core.Abstractions
{
public interface IDownloadFileQueryHandler : IRequestHandler<FileDto,DownloadFileQuery>
public interface IDownloadFileQueryHandler : IHttpRequestHandler<FileDto,DownloadFileQuery>
{
}
}
3 changes: 2 additions & 1 deletion src/File.Core/Abstractions/IExportFileQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using File.Domain.Dtos;
using File.Domain.Queries;
using SmallApiToolkit.Core.RequestHandlers;

namespace File.Core.Abstractions
{
public interface IExportFileQueryHandler : IRequestHandler<FileDto, ExportFileQuery>
public interface IExportFileQueryHandler : IHttpRequestHandler<FileDto, ExportFileQuery>
{
}
}
2 changes: 1 addition & 1 deletion src/File.Core/Abstractions/IFileByOptionsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace File.Core.Abstractions
//TODO CONVERSION VALIDATION
internal interface IFileByOptionsValidator
{
Result<bool> Validate(IFile file);
Result<bool> Validate(IFileProxy file);
Result<bool> Validate(string extension);
Result<bool> ValidateConversion(string sourceExtension, string destinationExtension);
}
Expand Down
4 changes: 2 additions & 2 deletions src/File.Core/Abstractions/IFileConvertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace File.Core.Abstractions
{
public interface IFileConvertService
{
Task<Result<IFile>> ConvertTo(IFile sourceFile, string destinationFormat, CancellationToken cancellationToken);
Task<Result<IFile>> ExportTo(FileDto sourceFile, string destinationFormat, CancellationToken cancellationToken);
Task<Result<IFileProxy>> ConvertTo(IFileProxy sourceFile, string destinationFormat, CancellationToken cancellationToken);
Task<Result<IFileProxy>> ExportTo(FileDto sourceFile, string destinationFormat, CancellationToken cancellationToken);
}
}
5 changes: 3 additions & 2 deletions src/File.Core/Abstractions/IGetFilesInfoQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using File.Domain.Dtos;
using File.Domain.Http;
using SmallApiToolkit.Core.RequestHandlers;
using SmallApiToolkit.Core.Response;

namespace File.Core.Abstractions
{
public interface IGetFilesInfoQueryHandler : IRequestHandler<IEnumerable<FileInfoDto>, EmptyRequest>
public interface IGetFilesInfoQueryHandler : IHttpRequestHandler<IEnumerable<FileInfoDto>, EmptyRequest>
{
}
}
9 changes: 0 additions & 9 deletions src/File.Core/Abstractions/IRequestHandler.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/File.Core/Commands/AddFilesCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
using File.Core.Resources;
using File.Domain.Commands;
using File.Domain.Extensions;
using File.Domain.Http;
using File.Domain.Logging;
using Microsoft.Extensions.Logging;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.Response;

namespace File.Core.Commands
{
Expand Down
2 changes: 1 addition & 1 deletion src/File.Core/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace File.Core.Extensions
{
internal static class FileExtensions
{
public async static Task<FileDto> CreateFileDto(this IFile file, CancellationToken cancellationToken)
public async static Task<FileDto> CreateFileDto(this IFileProxy file, CancellationToken cancellationToken)
{
return new FileDto
{
Expand Down
11 changes: 6 additions & 5 deletions src/File.Core/File.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -7,13 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="4.2.0" />
<PackageReference Include="Ardalis.GuardClauses" Version="4.5.0" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Validot" Version="2.4.1" />
<PackageReference Include="SmallApiToolkit.Core" Version="1.0.0.3" />
<PackageReference Include="Validot" Version="2.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/File.Core/Queries/ConvertToQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
using File.Core.Resources;
using File.Domain.Dtos;
using File.Domain.Extensions;
using File.Domain.Http;
using File.Domain.Logging;
using Microsoft.Extensions.Logging;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.Response;

namespace File.Core.Queries
{
Expand Down
3 changes: 2 additions & 1 deletion src/File.Core/Queries/DownloadFileQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using File.Core.Resources;
using File.Domain.Dtos;
using File.Domain.Extensions;
using File.Domain.Http;
using File.Domain.Logging;
using File.Domain.Queries;
using Microsoft.Extensions.Logging;
using SmallApiToolkit.Core.Extensions;
using SmallApiToolkit.Core.Response;
using Validot;

namespace File.Core.Queries
Expand Down
Loading

0 comments on commit 7eb243d

Please sign in to comment.