Replace api with controller instead of minimalapi
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5" />
|
||||
<PackageReference Include="Scalar.AspNetCore" Version="2.13.18" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Emergence.api.Interfaces
|
||||
{
|
||||
public interface IEndpoint
|
||||
{
|
||||
void MapEndPoint(IEndpointRouteBuilder app);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Emergence.models
|
||||
{
|
||||
public class TenantModel
|
||||
{
|
||||
public Guid Id = new Guid();
|
||||
public string TenantCode = string.Empty;
|
||||
public string CompanyName = string.Empty;
|
||||
public bool IsInactive = false;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Emergence.models
|
||||
{
|
||||
public class UserModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Email { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,10 @@ namespace Emergence.services.Extensions
|
||||
{
|
||||
public static class ServiceBuilderExtension
|
||||
{
|
||||
public static IServiceCollection AddServices(this IServiceCollection services)
|
||||
public static IServiceCollection AddEmergenceServices(this IServiceCollection services)
|
||||
{
|
||||
services = services.AddTransient<IUserService, UserService>();
|
||||
services = services.AddTransient<ITenantService, TenantService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ using System.Text;
|
||||
|
||||
namespace Emergence.services.Interface
|
||||
{
|
||||
public interface IService<T> where T : class
|
||||
public interface IService<T, I> where T : class
|
||||
{
|
||||
public Task<IList<T>> GetAllAsync();
|
||||
public Task<T> GetByIdAsync(int id);
|
||||
public Task<T> GetByIdAsync(I id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
using Emergence.models;
|
||||
|
||||
namespace Emergence.services.Interface
|
||||
{
|
||||
public interface ITenantService : IService<TenantModel, Guid>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using Emergence.models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Emergence.services.Interface
|
||||
{
|
||||
public interface IUserService : IService<UserModel>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Emergence.models;
|
||||
using Emergence.services.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Emergence.services.Services;
|
||||
|
||||
public class TenantService : ITenantService
|
||||
{
|
||||
private List<TenantModel> testList = [
|
||||
new TenantModel {TenantCode = "FJP", CompanyName ="Fred J Potter" },
|
||||
new TenantModel {TenantCode = "JAZZ", CompanyName ="Jazzbond" },
|
||||
new TenantModel {TenantCode = "TDT", CompanyName ="The Digital Tailor" },
|
||||
new TenantModel {TenantCode = "HBAS", CompanyName ="Hicks Building and Asbestos Services" },
|
||||
];
|
||||
|
||||
public async Task<IList<TenantModel>> GetAllAsync()
|
||||
{
|
||||
return testList;
|
||||
}
|
||||
|
||||
public async Task<TenantModel> GetByIdAsync(Guid id)
|
||||
{
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
return testList.FirstOrDefault(f => f.Id == id);
|
||||
#pragma warning restore CS8603 // Possible null reference return.
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
using Emergence.models;
|
||||
using Emergence.services.Interface;
|
||||
|
||||
namespace Emergence.services.Services;
|
||||
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private List<UserModel> testList = [
|
||||
new UserModel {Id = 1, Username = "chris", Email ="chris@fjp.com.au" },
|
||||
new UserModel {Id = 2, Username = "kim", Email ="kim@fjp.com.au" },
|
||||
new UserModel {Id = 3, Username = "amanda", Email ="amanda@fjp.com.au" },
|
||||
new UserModel {Id = 4, Username = "reception", Email ="reception@fjp.com.au" },
|
||||
];
|
||||
|
||||
public async Task<IList<UserModel>> GetAllAsync()
|
||||
{
|
||||
return testList;
|
||||
}
|
||||
|
||||
public async Task<UserModel> GetByIdAsync(int id)
|
||||
{
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
return testList.FirstOrDefault(f => f.Id == id);
|
||||
#pragma warning restore CS8603 // Possible null reference return.
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
<Solution>
|
||||
<Project Path="Emergence.api/Emergence.api.csproj" />
|
||||
<Project Path="Emergence.api/Emergence.api.csproj" Id="fd02b97a-65d1-4e9f-91b1-347ec75c9f0a" />
|
||||
<Project Path="Emergence.models/Emergence.models.csproj" Id="289779a8-43b3-44ec-994b-2ac4fbc29327" />
|
||||
<Project Path="Emergence.services/Emergence.services.csproj" Id="4a98db16-1ee8-486a-bcb9-5c9fa705a616" />
|
||||
</Solution>
|
||||
|
||||
Reference in New Issue
Block a user