diff --git a/src/SwiftLink.Infrastructure/Migrations/20240109132423_Init.Designer.cs b/src/SwiftLink.Infrastructure/Persistence/Migrations/20240109132423_Init.Designer.cs similarity index 100% rename from src/SwiftLink.Infrastructure/Migrations/20240109132423_Init.Designer.cs rename to src/SwiftLink.Infrastructure/Persistence/Migrations/20240109132423_Init.Designer.cs diff --git a/src/SwiftLink.Infrastructure/Migrations/20240109132423_Init.cs b/src/SwiftLink.Infrastructure/Persistence/Migrations/20240109132423_Init.cs similarity index 100% rename from src/SwiftLink.Infrastructure/Migrations/20240109132423_Init.cs rename to src/SwiftLink.Infrastructure/Persistence/Migrations/20240109132423_Init.cs diff --git a/src/SwiftLink.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs b/src/SwiftLink.Infrastructure/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs similarity index 100% rename from src/SwiftLink.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs rename to src/SwiftLink.Infrastructure/Persistence/Migrations/ApplicationDbContextModelSnapshot.cs diff --git a/src/SwiftLink.Presentation/Controllers/BaseController.cs b/src/SwiftLink.Presentation/Controllers/BaseController.cs new file mode 100644 index 0000000..4eb504e --- /dev/null +++ b/src/SwiftLink.Presentation/Controllers/BaseController.cs @@ -0,0 +1,12 @@ +using MediatR; +using Microsoft.AspNetCore.Mvc; + +namespace SwiftLink.Presentation.Controllers; + +[Route("api/v{v:apiVersion}/[controller]/[action]")] +[ApiController] +public abstract class BaseController : Controller +{ + private ISender? _mediatR; + protected ISender MediatR => _mediatR ??= HttpContext.RequestServices.GetRequiredService(); +} \ No newline at end of file diff --git a/src/SwiftLink.Presentation/Controllers/LinkController.cs b/src/SwiftLink.Presentation/Controllers/LinkController.cs new file mode 100644 index 0000000..817ca6c --- /dev/null +++ b/src/SwiftLink.Presentation/Controllers/LinkController.cs @@ -0,0 +1,15 @@ +using Asp.Versioning; +using Microsoft.AspNetCore.Mvc; +using SwiftLink.Application.UseCases.Links.GenerateCommand; + +namespace SwiftLink.Presentation.Controllers; + +[ApiVersion("1.0")] +public class LinkController : BaseController +{ + [HttpPost] + public IActionResult Generate([FromBody] GenerateShortCodeCommand command) + { + return Ok(MediatR.Send(command)); + } +} diff --git a/src/SwiftLink.Presentation/Program.cs b/src/SwiftLink.Presentation/Program.cs index 2b4fdc9..78cd9e3 100644 --- a/src/SwiftLink.Presentation/Program.cs +++ b/src/SwiftLink.Presentation/Program.cs @@ -1,3 +1,5 @@ +using Asp.Versioning; +using Microsoft.AspNetCore.Diagnostics; using SwiftLink.Application; using SwiftLink.Infrastructure; using SwiftLink.Shared; @@ -10,17 +12,39 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); + builder.Services.RegisterApplicationServices() .RegisterInfrastructureServices(builder.Configuration); - - builder.Services.AddStackExchangeRedisCache(options => + + builder.Services.AddApiVersioning(options => + { + options.DefaultApiVersion = new ApiVersion(1); + options.ReportApiVersions = true; + options.AssumeDefaultVersionWhenUnspecified = true; + options.ApiVersionReader = ApiVersionReader.Combine( + new UrlSegmentApiVersionReader(), + new HeaderApiVersionReader("X-Api-Version")); + }).AddApiExplorer(options => { - options.Configuration = builder.Configuration["AppSettings:RedisCacheUrl"]; + options.GroupNameFormat = "'v'V"; + options.SubstituteApiVersionInUrl = true; }); } var app = builder.Build(); { + app.UseExceptionHandler(error => + { + error.Run(async context => + { + context.Response.StatusCode = StatusCodes.Status500InternalServerError; + context.Response.ContentType = "application/json"; + var exceptionHandlerPathFeature = context.Features.Get(); + var exception = exceptionHandlerPathFeature?.Error; + await context.Response.WriteAsync(Result.Failure(Error.DefaultMessage).ToString()!); + }); + }); + app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); diff --git a/src/SwiftLink.Presentation/SwiftLink.Presentation.csproj b/src/SwiftLink.Presentation/SwiftLink.Presentation.csproj index 3a7512e..6f0ab6a 100644 --- a/src/SwiftLink.Presentation/SwiftLink.Presentation.csproj +++ b/src/SwiftLink.Presentation/SwiftLink.Presentation.csproj @@ -12,6 +12,8 @@ + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,10 +22,6 @@ - - - -