Added RoleController
This commit is contained in:
162
MainProject/Controllers/RoleController.cs
Normal file
162
MainProject/Controllers/RoleController.cs
Normal file
@@ -0,0 +1,162 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using BasicDotnetTemplate.MainProject.Core.Attributes;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Request.Role;
|
||||
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;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
[Route("[controller]")]
|
||||
public class RoleController : BaseController
|
||||
{
|
||||
private readonly IRoleService _roleService;
|
||||
public RoleController(
|
||||
IConfiguration configuration,
|
||||
IRoleService roleService
|
||||
) : base(configuration)
|
||||
{
|
||||
this._roleService = roleService;
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[HttpGet("get/{guid}")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetRoleByGuidAsync(string guid)
|
||||
{
|
||||
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))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var roleDto = _mapper?.Map<RoleDto>(role);
|
||||
|
||||
return Success(String.Empty, roleDto);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = "Something went wrong";
|
||||
if (!String.IsNullOrEmpty(exception.Message))
|
||||
{
|
||||
message += $". {exception.Message}";
|
||||
}
|
||||
return InternalServerError(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[HttpPost("create")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> CreateRoleAsync([FromBody] CreateRoleRequest request)
|
||||
{
|
||||
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);
|
||||
|
||||
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||
{
|
||||
return BadRequest("Not created");
|
||||
}
|
||||
|
||||
var roleDto = _mapper?.Map<RoleDto>(role);
|
||||
|
||||
return Success(String.Empty, roleDto);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("Invalid name");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = "Something went wrong";
|
||||
if (!String.IsNullOrEmpty(exception.Message))
|
||||
{
|
||||
message += $". {exception.Message}";
|
||||
}
|
||||
return InternalServerError(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[HttpDelete("{guid}")]
|
||||
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> DeleteRoleByGuidAsync(string guid)
|
||||
{
|
||||
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))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
await this._roleService.DeleteRoleAsync(role);
|
||||
|
||||
return Success(String.Empty);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = "Something went wrong";
|
||||
if (!String.IsNullOrEmpty(exception.Message))
|
||||
{
|
||||
message += $". {exception.Message}";
|
||||
}
|
||||
return InternalServerError(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
using SqlServerDatabase = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using AutoMapper;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||
@@ -8,6 +9,7 @@ public class AutoMapperConfiguration : Profile
|
||||
{
|
||||
public AutoMapperConfiguration()
|
||||
{
|
||||
CreateMap<SqlServerDatabase.Role, RoleDto>();
|
||||
CreateMap<SqlServerDatabase.User, UserDto>();
|
||||
|
||||
}
|
||||
|
||||
14
MainProject/Models/Api/Common/Role/RoleDto.cs
Normal file
14
MainProject/Models/Api/Common/Role/RoleDto.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
|
||||
public class RoleDto
|
||||
{
|
||||
#nullable enable
|
||||
public string? Guid { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public bool? IsNotEditable { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
10
MainProject/Models/Api/Request/Role/CreateRoleRequest.cs
Normal file
10
MainProject/Models/Api/Request/Role/CreateRoleRequest.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Request.Role;
|
||||
|
||||
public class CreateRoleRequest
|
||||
{
|
||||
#nullable enable
|
||||
public CreateRoleRequestData? Data { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
8
MainProject/Models/Api/Response/Role/GetRoleResponse.cs
Normal file
8
MainProject/Models/Api/Response/Role/GetRoleResponse.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Response.Role;
|
||||
|
||||
public class GetRoleResponse : BaseResponse<RoleDto>
|
||||
{
|
||||
public GetRoleResponse(int status, string? message, RoleDto? data) : base(status, message, data) { }
|
||||
}
|
||||
Reference in New Issue
Block a user