using AmtScanner.Api.Data; using AmtScanner.Api.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace AmtScanner.Api.Controllers; /// /// 角色控制器 /// [ApiController] [Route("api/[controller]")] [Authorize] public class RoleController : ControllerBase { private readonly AppDbContext _context; public RoleController(AppDbContext context) { _context = context; } /// /// 获取角色列表(分页) /// [HttpGet("list")] public async Task>>> 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>.Success(new PaginatedResponse { Records = roles, Current = current, Size = size, Total = total })); } } /// /// 角色列表项 DTO /// 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; }