84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
using AmtScanner.Api.Data;
|
|
using AmtScanner.Api.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AmtScanner.Api.Controllers;
|
|
|
|
/// <summary>
|
|
/// 角色控制器
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
public class RoleController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public RoleController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取角色列表(分页)
|
|
/// </summary>
|
|
[HttpGet("list")]
|
|
public async Task<ActionResult<ApiResponse<PaginatedResponse<RoleListItemDto>>>> GetRoleList(
|
|
[FromQuery] int current = 1,
|
|
[FromQuery] int size = 10,
|
|
[FromQuery] string? roleName = null,
|
|
[FromQuery] string? roleCode = null)
|
|
{
|
|
var query = _context.Roles.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(roleName))
|
|
{
|
|
query = query.Where(r => r.RoleName.Contains(roleName));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(roleCode))
|
|
{
|
|
query = query.Where(r => r.RoleCode.Contains(roleCode));
|
|
}
|
|
|
|
var total = await query.CountAsync();
|
|
var roles = await query
|
|
.OrderBy(r => r.Id)
|
|
.Skip((current - 1) * size)
|
|
.Take(size)
|
|
.Select(r => new RoleListItemDto
|
|
{
|
|
RoleId = r.Id,
|
|
RoleName = r.RoleName,
|
|
RoleCode = r.RoleCode,
|
|
Description = r.Description,
|
|
Enabled = r.Enabled,
|
|
CreateTime = r.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(ApiResponse<PaginatedResponse<RoleListItemDto>>.Success(new PaginatedResponse<RoleListItemDto>
|
|
{
|
|
Records = roles,
|
|
Current = current,
|
|
Size = size,
|
|
Total = total
|
|
}));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 角色列表项 DTO
|
|
/// </summary>
|
|
public class RoleListItemDto
|
|
{
|
|
public int RoleId { get; set; }
|
|
public string RoleName { get; set; } = string.Empty;
|
|
public string RoleCode { get; set; } = string.Empty;
|
|
public string? Description { get; set; }
|
|
public bool Enabled { get; set; }
|
|
public string CreateTime { get; set; } = string.Empty;
|
|
}
|