Skip to content

Commit

Permalink
FEAT: Minimal APIs implementation (#63)
Browse files Browse the repository at this point in the history
## Description
The classic Controllers implementation is replaced by the new, more
modern and performant Minimal APIs implementation.

## Related Issue
Closes #56.

## Motivation and Context
As explained in #56, the Minimal APIs implementation brings improved
performance to the request execution because of the reduced amount of
dependencies they provide. They are also more flexible to make this a
better fit even for smaller projects as they are easy to compose and
have a better system to document them in Swagger.

## How Has This Been Tested?
This should be transparent and the endpoints respond exactly as the ones
provided by the Controllers.

## Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:
- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [X] All new and existing tests passed.
  • Loading branch information
CesarD authored Jan 19, 2024
1 parent fb6a183 commit b26ebd7
Show file tree
Hide file tree
Showing 37 changed files with 661 additions and 544 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Asp.Versioning.Builder;
using MediatR;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
#if (!disableAuth)
using Monaco.Template.Backend.Api.Auth;
#endif
using Monaco.Template.Backend.Api.DTOs;
using Monaco.Template.Backend.Api.DTOs.Extensions;
using Monaco.Template.Backend.Application.DTOs;
using Monaco.Template.Backend.Application.Features.Company;
using Monaco.Template.Backend.Common.Api.Application;
using Monaco.Template.Backend.Common.Api.MinimalApi;
using Monaco.Template.Backend.Common.Domain.Model;

namespace Monaco.Template.Backend.Api.Endpoints;

public static class Companies
{
public static IEndpointRouteBuilder AddCompanies(this IEndpointRouteBuilder builder, ApiVersionSet versionSet)
{
var companies = builder.CreateApiGroupBuilder(versionSet, "Companies");

companies.MapGet("",
Task<Results<Ok<Page<CompanyDto>>, NotFound>> ([FromServices] ISender sender,
HttpRequest request) =>
sender.ExecuteQueryAsync(new GetCompanyPage.Query(request.Query)),
"GetCompanies",
#if (disableAuth)
"Gets a page of companies");
#else
"Gets a page of companies")
.RequireAuthorization(Scopes.CompaniesRead);
#endif

companies.MapGet("{id:guid}",
Task<Results<Ok<CompanyDto?>, NotFound>> ([FromServices] ISender sender,
[FromRoute] Guid id) =>
sender.ExecuteQueryAsync(new GetCompanyById.Query(id)),
"GetCompany",
#if (disableAuth)
"Gets a company by Id");
#else
"Gets a company by Id")
.RequireAuthorization(Scopes.CompaniesRead);
#endif

companies.MapPost("",
Task<Results<Created<Guid>, NotFound, ValidationProblem>> ([FromServices] ISender sender,
[FromBody] CompanyCreateEditDto dto,
HttpContext context) =>
sender.ExecuteCommandAsync(dto.MapCreateCommand(), "api/v{0}/Companies/{1}", context.GetRequestedApiVersion()!),
"CreateCompany",
#if (disableAuth)
"Create a new company");
#else
"Create a new company")
.RequireAuthorization(Scopes.CompaniesWrite);
#endif

companies.MapPut("{id:guid}",
Task<Results<NoContent, NotFound, ValidationProblem>> ([FromServices] ISender sender,
[FromRoute] Guid id,
[FromBody] CompanyCreateEditDto dto) =>
sender.ExecuteCommandEditAsync(dto.MapEditCommand(id)),
"EditCompany",
#if (disableAuth)
"Edit an existing company by Id");
#else
"Edit an existing company by Id")
.RequireAuthorization(Scopes.CompaniesWrite);
#endif

companies.MapDelete("{id:guid}",
Task<Results<Ok, NotFound, ValidationProblem>> ([FromServices] ISender sender,
[FromRoute] Guid id) =>
sender.ExecuteCommandDeleteAsync(new DeleteCompany.Command(id)),
"DeleteCompany",
#if (disableAuth)
"Delete an existing company by Id");
#else
"Delete an existing company by Id")
.RequireAuthorization(Scopes.CompaniesWrite);
#endif

return builder;
}
}
Loading

0 comments on commit b26ebd7

Please sign in to comment.