-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook up Swagger and API Versioning (#4)
* Add user ID to AuthenticationResponse * AssemblyAttribute ApiController * Hook up API versioning
- Loading branch information
1 parent
b69f086
commit 5817e97
Showing
17 changed files
with
338 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
namespace BuberDinner.Api; | ||
|
||
using Asp.Versioning; | ||
using Asp.Versioning.ApiExplorer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using System.Text; | ||
|
||
/// <summary> | ||
/// Configures the Swagger generation options. | ||
/// </summary> | ||
/// <remarks>This allows API versioning to define a Swagger document per API version after the | ||
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container.</remarks> | ||
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions> | ||
{ | ||
private readonly IApiVersionDescriptionProvider provider; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class. | ||
/// </summary> | ||
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param> | ||
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) => this.provider = provider; | ||
|
||
/// <inheritdoc /> | ||
public void Configure(SwaggerGenOptions options) | ||
{ | ||
// add a swagger document for each discovered API version | ||
// note: you might choose to skip or document deprecated API versions differently | ||
foreach (var description in provider.ApiVersionDescriptions) | ||
{ | ||
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description)); | ||
} | ||
} | ||
|
||
private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description) | ||
{ | ||
var text = new StringBuilder("Buber Dinner the AirBnB for dinner."); | ||
var info = new OpenApiInfo() | ||
{ | ||
Title = "Buber Dinner", | ||
Version = description.ApiVersion.ToString(), | ||
Contact = new OpenApiContact() { Name = "Xavier John", Email = "xavier@somewhere.com" }, | ||
License = new OpenApiLicense() { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") } | ||
}; | ||
|
||
if (description.IsDeprecated) | ||
{ | ||
text.Append(" This API version has been deprecated."); | ||
} | ||
|
||
if (description.SunsetPolicy is SunsetPolicy policy) | ||
{ | ||
if (policy.Date is DateTimeOffset when) | ||
{ | ||
text.Append(" The API will be sunset on ") | ||
.Append(when.Date.ToShortDateString()) | ||
.Append('.'); | ||
} | ||
|
||
if (policy.HasLinks) | ||
{ | ||
text.AppendLine(); | ||
|
||
for (var i = 0; i < policy.Links.Count; i++) | ||
{ | ||
var link = policy.Links[i]; | ||
|
||
if (link.Type == "text/html") | ||
{ | ||
text.AppendLine(); | ||
|
||
if (link.Title.HasValue) | ||
{ | ||
text.Append(link.Title.Value).Append(": "); | ||
} | ||
|
||
text.Append(link.LinkTarget.OriginalString); | ||
} | ||
} | ||
} | ||
} | ||
|
||
info.Description = text.ToString(); | ||
|
||
return info; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 22 additions & 2 deletions
24
BuberDinner.Api/src/Netural/Controllers/AuthenticationController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,54 @@ | ||
namespace BuberDinner.Api.Netural.Controllers; | ||
|
||
using Asp.Versioning; | ||
using BuberDinner.Api.Netural.Models.Authentication; | ||
using MapsterMapper; | ||
using Mediator; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
/// <summary> | ||
/// Authentication Controller | ||
/// </summary> | ||
[AllowAnonymous] | ||
[ApiVersionNeutral] | ||
public class AuthenticationController : ApiControllerBase | ||
{ | ||
private readonly ISender _sender; | ||
private readonly IMapper _mapper; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="mapper"></param> | ||
public AuthenticationController(ISender sender, IMapper mapper) | ||
{ | ||
_sender = sender; | ||
_mapper = mapper; | ||
} | ||
|
||
/// <summary> | ||
/// Register a new user. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <returns></returns> | ||
[HttpPost("register")] | ||
public async Task<ActionResult<AuthenticationResponse>> Register(RegisterRequest request) => | ||
await request.ToRegisterCommand() | ||
.BindAsync(command => _sender.Send(command)) | ||
.MapAsync(authResult => _mapper.Map<AuthenticationResponse>(authResult)) | ||
.MapAsync(_mapper.Map<AuthenticationResponse>) | ||
.FinallyAsync(result => MapToActionResult(result)); | ||
|
||
/// <summary> | ||
/// Login for existing user. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <returns></returns> | ||
[HttpPost("login")] | ||
public async Task<ActionResult<AuthenticationResponse>> Login(LoginRequest request) => | ||
await request.ToLoginQuery() | ||
.BindAsync(command => _sender.Send(command)) | ||
.MapAsync(authResult => _mapper.Map<AuthenticationResponse>(authResult)) | ||
.MapAsync(_mapper.Map<AuthenticationResponse>) | ||
.FinallyAsync(result => MapToActionResult(result)); | ||
} |
29 changes: 19 additions & 10 deletions
29
BuberDinner.Api/src/Netural/Controllers/ErrorController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
namespace FunctionalDDD.BuberDinner.Api.Netural.Controllers | ||
{ | ||
using Microsoft.AspNetCore.Mvc; | ||
namespace BuberDinner.Api.Netural.Controllers; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Asp.Versioning; | ||
|
||
[ApiController] | ||
public class ErrorController : ControllerBase | ||
/// <summary> | ||
/// Unhandled error controller. | ||
/// </summary> | ||
[ApiVersionNeutral] | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
public class ErrorController : ControllerBase | ||
{ | ||
/// <summary> | ||
/// Show error | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet] | ||
[Route("/error")] | ||
public IActionResult Error() | ||
{ | ||
[Route("/error")] | ||
public IActionResult Error() | ||
{ | ||
return Problem(); | ||
} | ||
return Problem(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 24 additions & 1 deletion
25
BuberDinner.Api/src/Netural/Models/Authentication/AuthenticationResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
namespace BuberDinner.Api.Netural.Models.Authentication; | ||
|
||
/// <summary> | ||
/// Authentication Response | ||
/// </summary> | ||
public class AuthenticationResponse | ||
{ | ||
//public Guid Id { get; set; } | ||
/// <summary> | ||
/// User Id | ||
/// </summary> | ||
public string? UserId { get; set; } | ||
|
||
|
||
/// <summary> | ||
/// First Name | ||
/// </summary> | ||
public string? FirstName { get; set; } | ||
|
||
/// <summary> | ||
/// Last Name | ||
/// </summary> | ||
public string? LastName { get; set; } | ||
|
||
/// <summary> | ||
/// Email address | ||
/// </summary> | ||
public string? Email { get; set; } | ||
|
||
/// <summary> | ||
/// Token | ||
/// </summary> | ||
public string? Token { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.