添加Admin角色授权并优化DictController代码格式

This commit is contained in:
罗澜大帅哥 2025-07-16 21:54:40 +08:00
parent 0ddc553ce4
commit 5d9da6360a
2 changed files with 29 additions and 21 deletions

View File

@ -10,12 +10,12 @@ namespace AGSS.Controllers.Dict
// [Produces("application/json")]
public class DictController : ControllerBase
{
private readonly DictService _dictService;
private readonly DictService _dictService;
private readonly ILogger<DictController> _logger;
private readonly ILogger<DictController> _logger;
public DictController(DictService dictService, ILogger<DictController> logger)
public DictController(DictService dictService, ILogger<DictController> logger)
{
_dictService = dictService;
_logger = logger;
@ -23,7 +23,7 @@ namespace AGSS.Controllers.Dict
// 1. 获取父级列表
[HttpGet("parents")]
[HttpGet("parents")]
public async Task<ActionResult> GetParents([FromQuery] string? label)
{
try
@ -48,17 +48,15 @@ namespace AGSS.Controllers.Dict
[HttpPost("parents")]
public async Task<ActionResult> CreateParent([FromBody] CreateParentRequest request)
{
var result = await _dictService.CreateParentAsync(request.Label, request.Value);
return Ok(new ReturnTemplate(200, "创建成功啦!", result));
var result = await _dictService.CreateParentAsync(request.Label, request.Value);
return Ok(new ReturnTemplate(200, "创建成功啦!", result));
}
// 请求模型
public class CreateParentRequest
public class CreateParentRequest
{
[Required(ErrorMessage = "字典标签不能为空")]
[Required(ErrorMessage = "字典标签不能为空")]
[StringLength(100, ErrorMessage = "标签长度不能超过100字符")]
public string Label { get; set; } = null!;
@ -73,7 +71,7 @@ public class CreateParentRequest
// 2.1 更新父级项
[Authorize(Roles = "Admin")]
[HttpPut("parents/{uuid}")]
public async Task<ActionResult> UpdateParent(
[FromRoute] Guid uuid,
@ -112,7 +110,6 @@ public class CreateParentRequest
/// </summary>
/// <param name="uuid">The unique identifier (UUID) of the parent dictionary entry to be deleted.</param>
/// <returns>An action result indicating the success or failure of the deletion operation. On success, returns a 200 status code with a confirmation message. In case of an exception, it returns a 200 status code with an error message and exception details.</returns>
[Authorize(Roles = "Admin")]
[HttpDelete("parents/{uuid}")]
public async Task<ActionResult> DeleteParent([FromRoute] Guid uuid)
{
@ -170,7 +167,8 @@ public class CreateParentRequest
/// <remarks>
/// The value of this property should correspond to an existing dictionary item that is not a child itself (i.e., it does not have its own ParentId).
/// </remarks>
[Required(ErrorMessage = "父级ID不能为空")] public Guid ParentId { get; set; }
[Required(ErrorMessage = "父级ID不能为空")]
public Guid ParentId { get; set; }
[Required(ErrorMessage = "父级值不能为空")] public string ParentValue { get; set; } = null!;
@ -180,7 +178,8 @@ public class CreateParentRequest
/// <remarks>
/// The label should be descriptive and meaningful to easily identify the purpose or content of the dictionary item. It plays a crucial role in the user interface and backend logic, serving as a key identifier for related operations.
/// </remarks>
[Required(ErrorMessage = "字典标签不能为空")] public string Label { get; set; } = null!;
[Required(ErrorMessage = "字典标签不能为空")]
public string Label { get; set; } = null!;
[Required(ErrorMessage = "字典值不能为空")] public string Value { get; set; } = null!;
@ -191,6 +190,7 @@ public class CreateParentRequest
/// This property is part of the request model for creating or updating a child dictionary item. It allows for the inclusion of an English label, enhancing multilingual support within the application.
/// </remarks>
public string? LabelEn { get; set; }
public string? Remark { get; set; }
/// <summary>
@ -206,7 +206,7 @@ public class CreateParentRequest
/// <param name="uuid">The unique identifier of the child dictionary item to update.</param>
/// <param name="request">The request object containing the new values for the child dictionary item.</param>
/// <returns>An action result indicating the success or failure of the operation, including a message and an optional data payload.</returns>
[HttpPut("children/{uuid}")]
[HttpPut("children/{uuid}")]
public async Task<ActionResult> UpdateChild(
[FromRoute] Guid uuid,
[FromBody] UpdateChildRequest request)
@ -242,7 +242,8 @@ public class CreateParentRequest
/// <remarks>
/// The Label is used to provide a meaningful name for the dictionary item that can be displayed in user interfaces or referenced in other parts of the application. It is crucial for the identification and management of dictionary items within the system.
/// </remarks>
[Required(ErrorMessage = "字典标签不能为空")] public string Label { get; set; } = null!;
[Required(ErrorMessage = "字典标签不能为空")]
public string Label { get; set; } = null!;
/// <summary>
/// Represents the value of a dictionary item. This property is required and must be provided when updating or creating a dictionary child item.
@ -250,7 +251,8 @@ public class CreateParentRequest
/// <remarks>
/// The value is a unique identifier for the dictionary item within its parent context. It should be meaningful and reflect the nature of the item it represents.
/// </remarks>
[Required(ErrorMessage = "字典值不能为空")] public string Value { get; set; } = null!;
[Required(ErrorMessage = "字典值不能为空")]
public string Value { get; set; } = null!;
/// <summary>
/// Represents the English label for a dictionary item. This property can be used to store an English translation or alternative name for the dictionary entry, facilitating internationalization and localization efforts.
@ -280,7 +282,7 @@ public class CreateParentRequest
/// </summary>
/// <param name="uuid">The unique identifier (UUID) of the child item to be deleted.</param>
/// <returns>An action result indicating the success or failure of the deletion operation, encapsulated in a ReturnTemplate object which includes a status code, message, and additional data if any.</returns>
[HttpDelete("children/{uuid}")]
[HttpDelete("children/{uuid}")]
public async Task<ActionResult> DeleteChild([FromRoute] Guid uuid)
{
try
@ -317,7 +319,7 @@ public class CreateParentRequest
/// Represents a response object that is returned by API endpoints.
/// This class encapsulates the status code, message, and optionally data to provide a consistent structure for API responses.
/// </summary>
public class ApiResponse
public class ApiResponse
{
/// <summary>
/// Represents the status code of the API response. This property is used to indicate the outcome of an operation, such as success, error, or a specific HTTP status.

View File

@ -1,6 +1,7 @@
using AGSS.Models.DTOs;
using AGSS.Models.Template;
using AGSS.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace AGSS.Controllers.Menu;
@ -20,6 +21,7 @@ public class MenuController : ControllerBase
/// 新增父级菜单
/// </summary>
[HttpPost("createParent")]
[Authorize(Roles = "Admin")]
public async Task<ReturnTemplate> CreateParentMenu([FromBody] MenuInitialRequest request)
{
return await _menuService.CreateParentMenu(request);
@ -28,6 +30,7 @@ public class MenuController : ControllerBase
/// <summary>
/// 编辑父级菜单
/// </summary>
[Authorize(Roles = "Admin")]
[HttpPut("updateParent")]
public async Task<ReturnTemplate> UpdateParentMenu([FromBody] MenuUpdateRequest request)
{
@ -38,6 +41,7 @@ public class MenuController : ControllerBase
/// 新增子级菜单
/// </summary>
[HttpPost("createChild")]
[Authorize(Roles = "Admin")]
public async Task<ReturnTemplate> CreateChildMenu([FromBody] MenuRequest request)
{
return await _menuService.CreateChildMenu(request);
@ -47,6 +51,7 @@ public class MenuController : ControllerBase
/// 编辑子级菜单
/// </summary>
[HttpPut("updateChild")]
[Authorize(Roles = "Admin")]
public async Task<ReturnTemplate> UpdateChildMenu([FromBody] MenuUpdateRequestSon request)
{
return await _menuService.UpdateChildMenu(request);
@ -65,6 +70,7 @@ public class MenuController : ControllerBase
/// 删除菜单
/// </summary>
[HttpDelete("delete/{uuid}")]
[Authorize(Roles = "Admin")]
public async Task<ReturnTemplate> DeleteMenu(string uuid)
{
return await _menuService.DeleteMenu(uuid);