Skip to content

Commit

Permalink
Add Api Versioning, Add Exception Handler.
Browse files Browse the repository at this point in the history
Add Base Controller and config MediatR.
  • Loading branch information
mohammadKarimi committed Jan 9, 2024
1 parent 587b51e commit e656f1f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/SwiftLink.Presentation/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -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<ISender>();
}
15 changes: 15 additions & 0 deletions src/SwiftLink.Presentation/Controllers/LinkController.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
30 changes: 27 additions & 3 deletions src/SwiftLink.Presentation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Diagnostics;
using SwiftLink.Application;
using SwiftLink.Infrastructure;
using SwiftLink.Shared;
Expand All @@ -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<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature?.Error;
await context.Response.WriteAsync(Result.Failure(Error.DefaultMessage).ToString()!);
});
});

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
Expand Down
6 changes: 2 additions & 4 deletions src/SwiftLink.Presentation/SwiftLink.Presentation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" Version="8.0.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -20,10 +22,6 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SwiftLink.Infrastructure\SwiftLink.Infrastructure.csproj" />
</ItemGroup>
Expand Down

0 comments on commit e656f1f

Please sign in to comment.