228 lines
7.2 KiB
C#
228 lines
7.2 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using AGSS.Models.Template;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace AGSS.Controllers.Dict
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
// [Produces("application/json")]
|
|
public class DictController : ControllerBase
|
|
{
|
|
private readonly DictService _dictService;
|
|
private readonly ILogger<DictController> _logger;
|
|
|
|
public DictController(DictService dictService, ILogger<DictController> logger)
|
|
{
|
|
_dictService = dictService;
|
|
_logger = logger;
|
|
}
|
|
|
|
|
|
// 1. 获取父级列表
|
|
[HttpGet("parents")]
|
|
public async Task<ActionResult> GetParents([FromQuery] string? label)
|
|
{
|
|
try
|
|
{
|
|
var result = await _dictService.GetParentsAsync(label);
|
|
return Ok(new ReturnTemplate(200, "获取成功啦!", result));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(500, "失败,服务器类错误!", ex));
|
|
}
|
|
}
|
|
|
|
// 2. 创建父级项
|
|
|
|
// [Authorize(Roles = "Admin")]
|
|
[HttpPost("parents")]
|
|
public async Task<ActionResult> CreateParent([FromBody] CreateParentRequest request)
|
|
{
|
|
try
|
|
{
|
|
var result = await _dictService.CreateParentAsync(request.Label, request.Value);
|
|
return Ok(new ReturnTemplate(200, "创建成功啦!", result));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(500, "错误", ex.Message));
|
|
}
|
|
}
|
|
|
|
// 请求模型
|
|
public class CreateParentRequest
|
|
{
|
|
[Required(ErrorMessage = "字典标签不能为空")]
|
|
[StringLength(100, ErrorMessage = "标签长度不能超过100字符")]
|
|
public string Label { get; set; } = null!;
|
|
|
|
[Required(ErrorMessage = "字典值不能为空")]
|
|
[StringLength(50, ErrorMessage = "值长度不能超过50字符")]
|
|
public string Value { get; set; } = null!;
|
|
}
|
|
|
|
// 2.1 更新父级项
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpPut("parents/{uuid}")]
|
|
public async Task<ActionResult> UpdateParent(
|
|
[FromRoute] Guid uuid,
|
|
[FromBody] UpdateParentRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _dictService.UpdateParentAsync(uuid, request.Label);
|
|
return Ok(new ReturnTemplate(200, "创建成功啦!", ""));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(200, "失败", ex));
|
|
}
|
|
}
|
|
|
|
public class UpdateParentRequest
|
|
{
|
|
[Required(ErrorMessage = "字典标签不能为空")]
|
|
[StringLength(100, ErrorMessage = "标签长度不能超过100字符")]
|
|
public string Label { get; set; } = null!;
|
|
}
|
|
|
|
// 3. 删除父级项
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
[HttpDelete("parents/{uuid}")]
|
|
public async Task<ActionResult> DeleteParent([FromRoute] Guid uuid)
|
|
{
|
|
try
|
|
{
|
|
await _dictService.DeleteParentAsync(uuid);
|
|
return Ok(new ReturnTemplate(200, "删除成功啦!", ""));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(200, "失败", ex));
|
|
}
|
|
}
|
|
|
|
|
|
// 4. 创建子项
|
|
[HttpPost("children")]
|
|
public async Task<ActionResult> CreateChild([FromBody] CreateChildRequest request)
|
|
{
|
|
try
|
|
{
|
|
var result = await _dictService.CreateChildAsync(
|
|
request.ParentId,
|
|
request.ParentValue,
|
|
request.Label,
|
|
request.Value,
|
|
request.LabelEn,
|
|
request.Remark,
|
|
request.Tag);
|
|
|
|
return Ok(new ReturnTemplate(200, "创建成功啦!", result));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(200, "我们遇到了一些问题,无法删除!", ""));
|
|
}
|
|
}
|
|
|
|
public class CreateChildRequest
|
|
{
|
|
[Required(ErrorMessage = "父级ID不能为空")] public Guid ParentId { get; set; }
|
|
|
|
[Required(ErrorMessage = "父级值不能为空")] public string ParentValue { get; set; } = null!;
|
|
|
|
[Required(ErrorMessage = "字典标签不能为空")] public string Label { get; set; } = null!;
|
|
|
|
[Required(ErrorMessage = "字典值不能为空")] public string Value { get; set; } = null!;
|
|
|
|
public string? LabelEn { get; set; }
|
|
public string? Remark { get; set; }
|
|
public string? Tag { get; set; }
|
|
}
|
|
|
|
// 4.1 更新子项
|
|
[HttpPut("children/{uuid}")]
|
|
public async Task<ActionResult> UpdateChild(
|
|
[FromRoute] Guid uuid,
|
|
[FromBody] UpdateChildRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _dictService.UpdateChildAsync(
|
|
uuid,
|
|
request.Label,
|
|
request.Value,
|
|
request.LabelEn,
|
|
request.Remark,
|
|
request.Tag);
|
|
|
|
return Ok(new ReturnTemplate(200, "更新成功啦!", ""));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(200, "失败!", ""));
|
|
}
|
|
}
|
|
|
|
public class UpdateChildRequest
|
|
{
|
|
[Required(ErrorMessage = "字典标签不能为空")] public string Label { get; set; } = null!;
|
|
|
|
[Required(ErrorMessage = "字典值不能为空")] public string Value { get; set; } = null!;
|
|
|
|
public string? LabelEn { get; set; }
|
|
public string? Remark { get; set; }
|
|
public string? Tag { get; set; }
|
|
}
|
|
|
|
// 5. 删除子项
|
|
[HttpDelete("children/{uuid}")]
|
|
public async Task<ActionResult> DeleteChild([FromRoute] Guid uuid)
|
|
{
|
|
try
|
|
{
|
|
await _dictService.DeleteChildAsync(uuid);
|
|
return Ok(new ReturnTemplate(200, "删除成功啦!", ""));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(200, "删除失败!", ""));
|
|
}
|
|
}
|
|
|
|
// 6. 获取子项
|
|
[HttpGet("children")]
|
|
public async Task<ActionResult> GetChildren(
|
|
[FromQuery, Required] string value,
|
|
[FromQuery] string? tag = null)
|
|
{
|
|
try
|
|
{
|
|
var result = await _dictService.GetChildrenAsync(value, tag);
|
|
return Ok(new ReturnTemplate(200, "查询成功啦!", result));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new ReturnTemplate(200, "查询失败!", ""));
|
|
}
|
|
}
|
|
}
|
|
|
|
// 统一响应模型
|
|
public class ApiResponse
|
|
{
|
|
public int Code { get; set; }
|
|
public string Message { get; set; } = null!;
|
|
}
|
|
|
|
public class ApiResponse<T> : ApiResponse
|
|
{
|
|
public T? Data { get; set; }
|
|
}
|
|
} |