-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
513a533
commit 00b8b0d
Showing
19 changed files
with
477 additions
and
522 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,27 +4,27 @@ namespace Browl.Service.AuthSecurity.API.Configuration; | |
|
||
public static class SwaggerConfig | ||
{ | ||
public static IServiceCollection AddSwaggerConfiguration(this IServiceCollection services) | ||
{ | ||
IServiceCollection unused = services.AddSwaggerGen(c => | ||
{ | ||
c.SwaggerDoc("v1", new OpenApiInfo() | ||
{ | ||
Title = "Browl Service AuthSecurity API", | ||
Description = "This API guarantees the security and proper authentication of users in the application.", | ||
Contact = new OpenApiContact() { Name = "Rondinele Guimarães", Email = "[email protected]" }, | ||
License = new OpenApiLicense() { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") } | ||
}); | ||
public static IServiceCollection AddSwaggerConfiguration(this IServiceCollection services) | ||
{ | ||
var unused = services.AddSwaggerGen(c => | ||
{ | ||
c.SwaggerDoc("v1", new OpenApiInfo() | ||
{ | ||
Title = "Browl Service AuthSecurity API", | ||
Description = "This API guarantees the security and proper authentication of users in the application.", | ||
Contact = new OpenApiContact() { Name = "Rondinele Guimarães", Email = "[email protected]" }, | ||
License = new OpenApiLicense() { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") } | ||
}); | ||
}); | ||
}); | ||
|
||
return services; | ||
} | ||
return services; | ||
} | ||
|
||
public static IApplicationBuilder UseSwaggerConfiguration(this IApplicationBuilder app) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(c => | ||
var unused1 = app.UseSwagger(); | ||
var unused = app.UseSwaggerUI(c => | ||
{ | ||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); | ||
}); | ||
|
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
105 changes: 49 additions & 56 deletions
105
...wl.Service.AuthSecurity/Browl.Service.AuthSecurity.API/Controllers/Base/MainController.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,72 +1,65 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Browl.Service.AuthSecurity.API.Controllers.Base | ||
namespace Browl.Service.AuthSecurity.API.Controllers.Base; | ||
|
||
/// <summary> | ||
/// Abstract base controller class that provides common functionality for API controllers. | ||
/// </summary> | ||
/// <remarks> | ||
/// This abstract class defines: | ||
/// - Errors: An IList property to hold a list of error strings. | ||
/// - CustomResponse(): A protected method that returns an ActionResult with custom error handling. | ||
/// - AddErrorProcessing(): A protected method to add an error string to the Errors collection. | ||
/// - ClearErrorsProcessing(): A protected method to clear the Errors collection. | ||
/// | ||
/// Controllers that inherit from this base class can utilize these common members and methods for consistent error handling and responses. | ||
/// </remarks> | ||
[ApiController] | ||
public abstract class MainController : Controller | ||
{ | ||
// Collection to store error messages | ||
protected IList<string> Errors = new List<string>(); | ||
|
||
/// <summary> | ||
/// Abstract base controller class that provides common functionality for API controllers. | ||
/// Custom response method to handle API responses | ||
/// </summary> | ||
/// <remarks> | ||
/// This abstract class defines: | ||
/// - Errors: An IList property to hold a list of error strings. | ||
/// - CustomResponse(): A protected method that returns an ActionResult with custom error handling. | ||
/// - AddErrorProcessing(): A protected method to add an error string to the Errors collection. | ||
/// - ClearErrorsProcessing(): A protected method to clear the Errors collection. | ||
/// | ||
/// Controllers that inherit from this base class can utilize these common members and methods for consistent error handling and responses. | ||
/// </remarks> | ||
[ApiController] | ||
public abstract class MainController : Controller | ||
/// <param name="result">Optional result object</param> | ||
/// <returns>Action result</returns> | ||
protected ActionResult CustomResponse(object? result = null) | ||
{ | ||
// Collection to store error messages | ||
protected IList<string> Errors = new List<string>(); | ||
|
||
/// <summary> | ||
/// Custom response method to handle API responses | ||
/// </summary> | ||
/// <param name="result">Optional result object</param> | ||
/// <returns>Action result</returns> | ||
protected ActionResult CustomResponse(object? result = null) | ||
// Check if the model state is valid and there are no errors | ||
if (ModelState.IsValid && Errors.Count == 0) | ||
{ | ||
// Check if the model state is valid and there are no errors | ||
if (ModelState.IsValid && Errors.Count == 0) | ||
{ | ||
return Ok(result); | ||
} | ||
|
||
var errorDictionary = new Dictionary<string, string[]>(); | ||
|
||
// Add model state errors to the dictionary | ||
if (ModelState.ErrorCount > 0) | ||
{ | ||
var modelErrors = ModelState.Values.SelectMany(v => v.Errors); | ||
errorDictionary.Add("Messages", modelErrors.Select(e => e.ErrorMessage).ToArray()); | ||
} | ||
|
||
// Add custom errors to the dictionary | ||
if (Errors.Count > 0) | ||
{ | ||
errorDictionary.Add("Messages", Errors.ToArray()); | ||
} | ||
|
||
// Return a bad request response with the validation problem details | ||
return BadRequest(new ValidationProblemDetails(errorDictionary)); | ||
return Ok(result); | ||
} | ||
|
||
/// <summary> | ||
/// Add an error message to the collection | ||
/// </summary> | ||
/// <param name="error">Error message</param> | ||
protected void AddErrorProcessing(string error) | ||
var errorDictionary = new Dictionary<string, string[]>(); | ||
|
||
// Add model state errors to the dictionary | ||
if (ModelState.ErrorCount > 0) | ||
{ | ||
Errors.Add(error); | ||
var modelErrors = ModelState.Values.SelectMany(v => v.Errors); | ||
errorDictionary.Add("Messages", modelErrors.Select(e => e.ErrorMessage).ToArray()); | ||
} | ||
|
||
/// <summary> | ||
/// Clear all error messages from the collection | ||
/// </summary> | ||
protected void ClearErrorsProcessing() | ||
// Add custom errors to the dictionary | ||
if (Errors.Count > 0) | ||
{ | ||
Errors.Clear(); | ||
errorDictionary.Add("Messages", Errors.ToArray()); | ||
} | ||
|
||
// Return a bad request response with the validation problem details | ||
return BadRequest(new ValidationProblemDetails(errorDictionary)); | ||
} | ||
|
||
/// <summary> | ||
/// Add an error message to the collection | ||
/// </summary> | ||
/// <param name="error">Error message</param> | ||
protected void AddErrorProcessing(string error) => Errors.Add(error); | ||
|
||
/// <summary> | ||
/// Clear all error messages from the collection | ||
/// </summary> | ||
protected void ClearErrorsProcessing() => Errors.Clear(); | ||
} |
Oops, something went wrong.