Replace api with controller instead of minimalapi

This commit is contained in:
2026-04-01 11:11:19 +10:30
parent f26ff6e04a
commit 5407c7db55
17 changed files with 98 additions and 147 deletions
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using Emergence.models;
using Emergence.services.Interface;
using Microsoft.AspNetCore.Http.HttpResults;
namespace Emergence.api.Controllers;
[ApiController]
[Route("[controller]", Name = "Tenant", Order = 1)]
public class TenantController : ControllerBase
{
private readonly ITenantService _tenantService;
public TenantController(ITenantService tenantService)
{
_tenantService = tenantService;
}
[HttpGet(Name = "GetTenants"), ProducesResponseType(StatusCodes.Status200OK), ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<Results<Ok<IList<TenantModel>>, NotFound>> GetAsync()
{
var tenants = await _tenantService.GetAllAsync();
return tenants is IList<TenantModel> result ? TypedResults.Ok(result) : TypedResults.NotFound();
}
}
+1
View File
@@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5" />
<PackageReference Include="Scalar.AspNetCore" Version="2.13.18" />
</ItemGroup>
<ItemGroup>
+4 -8
View File
@@ -1,11 +1,7 @@
@Emergence.api_HostAddress = http://localhost:5235
@Emergence.api_HostAddress = http://localhost:5283
GET {{Emergence.api_HostAddress}}/users/
GET {{Emergence.api_HostAddress}}/tenant/
Accept: application/json
###
GET {{Emergence.api_HostAddress}}/users/2
Accept: application/json
###
GET {{Emergence.api_HostAddress}}/openapi/emergence.yaml
Accept: application/json
###
GET {{Emergence.api_HostAddress}}/openapi/v1.json
-30
View File
@@ -1,30 +0,0 @@
using Emergence.api.Interfaces;
using Emergence.models;
using Emergence.services.Interface;
using Microsoft.AspNetCore.Http.HttpResults;
namespace Emergence.api.Endpoints;
public class UserEndpoints : IEndpoint
{
private readonly IUserService _userService;
public UserEndpoints(IUserService userService)
{
_userService = userService;
}
public void MapEndPoint(IEndpointRouteBuilder app)
{
var endpoints = app.MapGroup("/users")
.WithTags("User Items");
endpoints.MapGet("/", GetAll).Produces<List<UserModel>>(StatusCodes.Status200OK).Produces(StatusCodes.Status404NotFound).WithName("Get All Users");
endpoints.MapGet("/{id}", GetById).Produces<string>(StatusCodes.Status200OK).Produces(StatusCodes.Status404NotFound).WithName("Get User By Id");
}
public async Task<Results<Ok<List<UserModel>>, NotFound>> GetAll() =>
await _userService.GetAllAsync() is List<UserModel> result ? TypedResults.Ok(result) : TypedResults.NotFound();
public async Task<Results<Ok<UserModel>, NotFound>> GetById(int id) =>
await _userService.GetByIdAsync(id) is UserModel result ? TypedResults.Ok(result) : TypedResults.NotFound();
}
@@ -1,39 +0,0 @@
using Emergence.api.Interfaces;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Reflection;
namespace Emergence.api.Extensions;
public static class EndpointExtension
{
public static IServiceCollection AddEndpoints(this IServiceCollection services, Assembly assembly)
{
// Find all classes that implement IEndpoint and register them for DI
ServiceDescriptor[] serviceDescriptors = assembly
.DefinedTypes
.Where(type => type is { IsAbstract: false, IsInterface: false } &&
type.IsAssignableTo(typeof(IEndpoint)))
.Select(type => ServiceDescriptor.Transient(typeof(IEndpoint), type))
.ToArray();
services.TryAddEnumerable(serviceDescriptors);
return services;
}
public static IApplicationBuilder MapEndpoints(this WebApplication app, RouteGroupBuilder? routeGroupBuilder = null)
{
IEnumerable<IEndpoint> endpoints = app.Services
.GetRequiredService<IEnumerable<IEndpoint>>();
IEndpointRouteBuilder builder =
routeGroupBuilder is null ? app : routeGroupBuilder;
foreach (IEndpoint endpoint in endpoints)
{
endpoint.MapEndPoint(builder);
}
return app;
}
}
-7
View File
@@ -1,7 +0,0 @@
namespace Emergence.api.Interfaces
{
public interface IEndpoint
{
void MapEndPoint(IEndpointRouteBuilder app);
}
}
+11 -7
View File
@@ -1,22 +1,26 @@
using Emergence.api.Extensions;
using Emergence.services.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddEmergenceServices();
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddServices();
builder.Services.AddEndpoints(typeof(Program).Assembly);
var app = builder.Build();
app.UseHttpsRedirection();
app.MapEndpoints();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi("/openapi/emergence.yaml");
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
+2 -2
View File
@@ -5,7 +5,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5235",
"applicationUrl": "http://localhost:5283",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -14,7 +14,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7228;http://localhost:5235",
"applicationUrl": "https://localhost:7284;http://localhost:5283",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}