Added role update endpoint
This commit is contained in:
@@ -115,6 +115,66 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
|
||||
}
|
||||
|
||||
[JwtAuthorization()]
|
||||
[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)
|
||||
{
|
||||
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))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (
|
||||
await this._roleService.CheckIfNameIsValid(request.Data.Name) ||
|
||||
await this._roleService.CheckIfNameIsValid(request.Data.Name, guid)
|
||||
)
|
||||
{
|
||||
role = await this._roleService.UpdateRoleAsync(request.Data, role);
|
||||
|
||||
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)]
|
||||
|
||||
@@ -13,6 +13,7 @@ public interface IRoleService
|
||||
Task<Role?> GetRoleByGuidAsync(string guid);
|
||||
Task<bool> CheckIfNameIsValid(string name, string? guid = "");
|
||||
Task<Role?> CreateRoleAsync(CreateRoleRequestData data);
|
||||
Task<Role?> UpdateRoleAsync(CreateRoleRequestData data, Role role);
|
||||
Task<Role?> GetRoleForUser(string? guid);
|
||||
Task<bool?> DeleteRoleAsync(Role role);
|
||||
}
|
||||
@@ -113,6 +114,30 @@ public class RoleService : BaseService, IRoleService
|
||||
return role;
|
||||
}
|
||||
|
||||
public async Task<Role?> UpdateRoleAsync(CreateRoleRequestData data, Role role)
|
||||
{
|
||||
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||
|
||||
try
|
||||
{
|
||||
role.Name = data.Name;
|
||||
role.IsNotEditable = data.IsNotEditable;
|
||||
role.UpdateTime = DateTime.UtcNow;
|
||||
role.UpdateUserId = this.GetCurrentUserId();
|
||||
|
||||
_sqlServerContext.Roles.Update(role);
|
||||
await _sqlServerContext.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
Logger.Error(exception, $"[RoleService][UpdateRoleAsync]");
|
||||
}
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
public async Task<Role?> GetRoleForUser(string? guid)
|
||||
{
|
||||
Role? role = null;
|
||||
|
||||
Reference in New Issue
Block a user