Removing duplicated lines - wip
This commit is contained in:
@@ -6,6 +6,7 @@ using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Core.Filters;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
@@ -21,29 +22,16 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
this._authService = authService;
|
||||
}
|
||||
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPost("authenticate")]
|
||||
[ProducesResponseType<AuthenticateResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest request)
|
||||
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest request) //NOSONAR
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (
|
||||
request == null ||
|
||||
request.Data == null ||
|
||||
String.IsNullOrEmpty(request.Data.Email) ||
|
||||
String.IsNullOrEmpty(request.Data.Password)
|
||||
)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
var data = await this._authService.AuthenticateAsync(request.Data);
|
||||
|
||||
if (data == null)
|
||||
|
||||
@@ -12,7 +12,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
protected readonly IMapper? _mapper;
|
||||
protected readonly IConfiguration _configuration;
|
||||
protected readonly AppSettings _appSettings;
|
||||
protected readonly string _requestNotWellFormed = "Request is not well formed";
|
||||
protected readonly string _somethingWentWrong = "Something went wrong";
|
||||
|
||||
protected BaseController(
|
||||
|
||||
@@ -7,6 +7,7 @@ using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
using BasicDotnetTemplate.MainProject.Core.Filters;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
@@ -23,6 +24,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpGet("get/{guid}")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
@@ -32,15 +34,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(guid))
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
var role = await this._roleService.GetRoleByGuidAsync(guid);
|
||||
|
||||
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||
@@ -65,25 +58,15 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPost("create")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> CreateRoleAsync([FromBody] CreateRoleRequest request)
|
||||
public async Task<IActionResult> CreateRoleAsync([FromBody] CreateRoleRequest request) //NOSONAR
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (request == null || request.Data == null || String.IsNullOrEmpty(request.Data.Name)
|
||||
)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (await this._roleService.CheckIfNameIsValid(request.Data.Name))
|
||||
{
|
||||
var role = await this._roleService.CreateRoleAsync(request.Data);
|
||||
@@ -116,29 +99,15 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPut("update/{guid}")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> UpdateRoleAsync([FromBody] CreateRoleRequest request, string guid)
|
||||
public async Task<IActionResult> UpdateRoleAsync([FromBody] CreateRoleRequest request, string guid) //NOSONAR
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (
|
||||
request == null ||
|
||||
request.Data == null ||
|
||||
String.IsNullOrEmpty(request.Data.Name) ||
|
||||
String.IsNullOrEmpty(guid)
|
||||
)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
var role = await this._roleService.GetRoleByGuidAsync(guid);
|
||||
|
||||
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||
@@ -181,6 +150,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpDelete("{guid}")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
@@ -190,15 +160,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(guid))
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
var role = await this._roleService.GetRoleByGuidAsync(guid);
|
||||
|
||||
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||
|
||||
@@ -7,6 +7,7 @@ using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
using BasicDotnetTemplate.MainProject.Core.Filters;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
@@ -24,8 +25,10 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
this._userService = userService;
|
||||
this._roleService = roleService;
|
||||
}
|
||||
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpGet("get/{guid}")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
@@ -35,15 +38,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(guid))
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
var user = await this._userService.GetUserByGuidAsync(guid);
|
||||
|
||||
if (user == null || String.IsNullOrEmpty(user.Guid))
|
||||
@@ -67,30 +61,16 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
// [JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPost("create")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> CreateUserAsync([FromBody] CreateUserRequest request)
|
||||
public async Task<IActionResult> CreateUserAsync([FromBody] CreateUserRequest request) //NOSONAR
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (request == null || request.Data == null ||
|
||||
String.IsNullOrEmpty(request.Data.FirstName) ||
|
||||
String.IsNullOrEmpty(request.Data.LastName) ||
|
||||
String.IsNullOrEmpty(request.Data.Email) ||
|
||||
String.IsNullOrEmpty(request.Data.Password)
|
||||
)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (await this._userService.CheckIfEmailIsValid(request.Data.Email))
|
||||
{
|
||||
var role = await this._roleService.GetRoleForUser(request.Data.RoleGuid);
|
||||
@@ -129,26 +109,15 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPut("update/{guid}")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> UpdateUserAsync([FromBody] UpdateUserRequest request, string guid)
|
||||
public async Task<IActionResult> UpdateUserAsync([FromBody] UpdateUserRequest request, string guid) //NOSONAR
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (request == null || request.Data == null ||
|
||||
String.IsNullOrEmpty(request.Data.FirstName) ||
|
||||
String.IsNullOrEmpty(request.Data.LastName)
|
||||
)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
var user = await this._userService.GetUserByGuidAsync(guid);
|
||||
if(user == null)
|
||||
{
|
||||
@@ -175,6 +144,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPut("update/{guid}/password")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
@@ -183,16 +153,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(newPassword))
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
var user = await this._userService.GetUserByGuidAsync(guid);
|
||||
if(user == null)
|
||||
{
|
||||
@@ -219,6 +179,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpPut("update/{guid}/role")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
@@ -227,16 +188,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(roleGuid))
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
var role = await this._roleService.GetRoleForUser(roleGuid);
|
||||
if (role == null)
|
||||
{
|
||||
@@ -269,6 +220,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[ModelStateValidationHandledByFilterAttribute]
|
||||
[HttpDelete("{guid}")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
@@ -278,15 +230,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(guid))
|
||||
{
|
||||
return BadRequest(_requestNotWellFormed);
|
||||
}
|
||||
var user = await this._userService.GetUserByGuidAsync(guid);
|
||||
|
||||
if (user == null || String.IsNullOrEmpty(user.Guid))
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Core.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that ModelState validation is handled automatically by an Action Filter.
|
||||
/// Used to suppress SonarCloud warnings about missing ModelState.IsValid checks.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class ModelStateValidationHandledByFilterAttribute : Attribute
|
||||
{ }
|
||||
}
|
||||
30
MainProject/Core/Filters/ValidationActionFilter.cs
Normal file
30
MainProject/Core/Filters/ValidationActionFilter.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Core.Filters
|
||||
{
|
||||
public class ValidationActionFilter : IAsyncActionFilter
|
||||
{
|
||||
private readonly string _requestNotWellFormedMessage = "Request is not well formed";
|
||||
|
||||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(new { message = _requestNotWellFormedMessage, errors = context.ModelState });
|
||||
return;
|
||||
}
|
||||
|
||||
var requestBody = context.ActionArguments.Values.FirstOrDefault(arg => arg != null && !arg.GetType().IsPrimitive && !(arg is string));
|
||||
|
||||
if (requestBody == null)
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(new { message = _requestNotWellFormedMessage });
|
||||
return;
|
||||
}
|
||||
|
||||
await next();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
|
||||
public class CreateRoleRequestData
|
||||
{
|
||||
public string Name { get; set; } = String.Empty;
|
||||
[Required(ErrorMessage = "Name is required")]
|
||||
public required string Name { get; set; }
|
||||
public required bool IsNotEditable { get; set; }
|
||||
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
|
||||
public class CreateUserRequestData : UpdateUserRequestData
|
||||
{
|
||||
public string Email { get; set; } = String.Empty;
|
||||
public string Password { get; set; } = String.Empty;
|
||||
[Required(ErrorMessage = "Email is required")]
|
||||
[StringLength(200, ErrorMessage = "Email's maxLength: 200")]
|
||||
public required string Email { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Password is required")]
|
||||
public required string Password { get; set; }
|
||||
|
||||
public string? RoleGuid { get; set; }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
|
||||
public class UpdateUserRequestData
|
||||
{
|
||||
public string FirstName { get; set; } = String.Empty;
|
||||
public string LastName { get; set; } = String.Empty;
|
||||
[Required(ErrorMessage = "FirstName is required")]
|
||||
[StringLength(200, ErrorMessage = "FirstName's maxLength: 200")]
|
||||
public required string FirstName { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "LastName is required")]
|
||||
[StringLength(200, ErrorMessage = "LastName's maxLength: 200")]
|
||||
public required string LastName { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
|
||||
using BasicDotnetTemplate.MainProject.Core.Filters;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||
@@ -140,7 +140,10 @@ public static class ProgramUtils
|
||||
|
||||
builder.Services.AddAuthentication();
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<ValidationActionFilter>();
|
||||
});
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
Logger.Info("[ProgramUtils][AddServices] Done services");
|
||||
|
||||
Reference in New Issue
Block a user