Implemented roles and action timestamps. Added oermissions selector and interfaces
This commit was merged in pull request #3.
This commit is contained in:
123
Moonlight.Api/Http/Controllers/Admin/RolesController.cs
Normal file
123
Moonlight.Api/Http/Controllers/Admin/RolesController.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Mappers;
|
||||
using Moonlight.Shared.Http.Requests;
|
||||
using Moonlight.Shared.Http.Requests.Roles;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
using Moonlight.Shared.Http.Responses.Admin;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/roles")]
|
||||
public class RolesController : Controller
|
||||
{
|
||||
private readonly DatabaseRepository<Role> RoleRepository;
|
||||
|
||||
public RolesController(DatabaseRepository<Role> roleRepository)
|
||||
{
|
||||
RoleRepository = roleRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<PagedData<RoleResponse>>> GetAsync(
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int length,
|
||||
[FromQuery] FilterOptions? filterOptions
|
||||
)
|
||||
{
|
||||
// Validation
|
||||
if (startIndex < 0)
|
||||
return Problem("Invalid start index specified", statusCode: 400);
|
||||
|
||||
if (length is < 1 or > 100)
|
||||
return Problem("Invalid length specified");
|
||||
|
||||
// Query building
|
||||
|
||||
var query = RoleRepository
|
||||
.Query();
|
||||
|
||||
// Filters
|
||||
if (filterOptions != null)
|
||||
{
|
||||
foreach (var filterOption in filterOptions.Filters)
|
||||
{
|
||||
query = filterOption.Key switch
|
||||
{
|
||||
nameof(Role.Name) =>
|
||||
query.Where(role => EF.Functions.ILike(role.Name, $"%{filterOption.Value}%")),
|
||||
|
||||
_ => query
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Pagination
|
||||
var data = await query
|
||||
.ProjectToResponse()
|
||||
.Skip(startIndex)
|
||||
.Take(length)
|
||||
.ToArrayAsync();
|
||||
|
||||
var total = await query.CountAsync();
|
||||
|
||||
return new PagedData<RoleResponse>(data, total);
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<RoleResponse>> GetAsync([FromRoute] int id)
|
||||
{
|
||||
var role = await RoleRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (role == null)
|
||||
return Problem("No role with this id found", statusCode: 404);
|
||||
|
||||
return RoleMapper.MapToResponse(role);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<RoleResponse>> CreateAsync([FromBody] CreateRoleRequest request)
|
||||
{
|
||||
var role = RoleMapper.MapToRole(request);
|
||||
|
||||
var finalRole = await RoleRepository.AddAsync(role);
|
||||
|
||||
return RoleMapper.MapToResponse(finalRole);
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
public async Task<ActionResult<RoleResponse>> UpdateAsync([FromRoute] int id, [FromBody] UpdateRoleRequest request)
|
||||
{
|
||||
var role = await RoleRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (role == null)
|
||||
return Problem("No role with this id found", statusCode: 404);
|
||||
|
||||
RoleMapper.Merge(role, request);
|
||||
|
||||
await RoleRepository.UpdateAsync(role);
|
||||
|
||||
return RoleMapper.MapToResponse(role);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<ActionResult> DeleteAsync([FromRoute] int id)
|
||||
{
|
||||
var role = await RoleRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (role == null)
|
||||
return Problem("No role with this id found", statusCode: 404);
|
||||
|
||||
await RoleRepository.RemoveAsync(role);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,8 @@ public class UsersController : Controller
|
||||
if (length is < 1 or > 100)
|
||||
return Problem("Invalid length specified");
|
||||
|
||||
// Query building
|
||||
|
||||
var query = UserRepository
|
||||
.Query();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user