23 changed files with 877 additions and 101 deletions
@ -0,0 +1,25 @@ |
|||
using MediatR; |
|||
|
|||
namespace CellularManagement.Application.Features.Roles.Commands.CreateRole; |
|||
|
|||
/// <summary>
|
|||
/// 创建角色命令
|
|||
/// </summary>
|
|||
/// <example>
|
|||
/// {
|
|||
/// "name": "Manager",
|
|||
/// "description": "部门经理角色,拥有管理权限"
|
|||
/// }
|
|||
/// </example>
|
|||
public sealed record CreateRoleCommand( |
|||
/// <summary>
|
|||
/// 角色名称
|
|||
/// </summary>
|
|||
/// <example>Manager</example>
|
|||
string Name, |
|||
|
|||
/// <summary>
|
|||
/// 角色描述
|
|||
/// </summary>
|
|||
/// <example>部门经理角色,拥有管理权限</example>
|
|||
string? Description) : IRequest<OperationResult<CreateRoleResponse>>; |
|||
@ -0,0 +1,10 @@ |
|||
namespace CellularManagement.Application.Features.Roles.Commands.CreateRole; |
|||
|
|||
/// <summary>
|
|||
/// 创建角色响应
|
|||
/// </summary>
|
|||
public sealed record CreateRoleResponse( |
|||
/// <summary>
|
|||
/// 角色ID
|
|||
/// </summary>
|
|||
string RoleId); |
|||
@ -0,0 +1,12 @@ |
|||
using MediatR; |
|||
|
|||
namespace CellularManagement.Application.Features.Roles.Commands.DeleteRole; |
|||
|
|||
/// <summary>
|
|||
/// 删除角色命令
|
|||
/// </summary>
|
|||
public sealed record DeleteRoleCommand( |
|||
/// <summary>
|
|||
/// 角色ID
|
|||
/// </summary>
|
|||
string RoleId) : IRequest<OperationResult<DeleteRoleResponse>>; |
|||
@ -0,0 +1,10 @@ |
|||
namespace CellularManagement.Application.Features.Roles.Commands.DeleteRole; |
|||
|
|||
/// <summary>
|
|||
/// 删除角色响应
|
|||
/// </summary>
|
|||
public sealed record DeleteRoleResponse( |
|||
/// <summary>
|
|||
/// 是否删除成功
|
|||
/// </summary>
|
|||
bool Success); |
|||
@ -0,0 +1,144 @@ |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.Extensions.Logging; |
|||
using MediatR; |
|||
using CellularManagement.Domain.Entities; |
|||
using CellularManagement.Domain.Repositories; |
|||
using CellularManagement.Application.Features.Roles.Commands.CreateRole; |
|||
using CellularManagement.Application.Features.Roles.Commands.DeleteRole; |
|||
using CellularManagement.Application.Features.Roles.Queries.GetRole; |
|||
|
|||
namespace CellularManagement.Application.Features.Roles.Commands; |
|||
|
|||
/// <summary>
|
|||
/// 角色命令处理器
|
|||
/// </summary>
|
|||
public sealed class RoleCommandHandler : |
|||
IRequestHandler<CreateRoleCommand, OperationResult<CreateRoleResponse>>, |
|||
IRequestHandler<DeleteRoleCommand, OperationResult<DeleteRoleResponse>>, |
|||
IRequestHandler<GetRoleQuery, OperationResult<GetRoleResponse>> |
|||
{ |
|||
private readonly RoleManager<AppRole> _roleManager; |
|||
private readonly ILogger<RoleCommandHandler> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化处理器
|
|||
/// </summary>
|
|||
public RoleCommandHandler( |
|||
RoleManager<AppRole> roleManager, |
|||
ILogger<RoleCommandHandler> logger) |
|||
{ |
|||
_roleManager = roleManager; |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理创建角色请求
|
|||
/// </summary>
|
|||
public async Task<OperationResult<CreateRoleResponse>> Handle( |
|||
CreateRoleCommand request, |
|||
CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
// 检查角色是否已存在
|
|||
var existingRole = await _roleManager.FindByNameAsync(request.Name); |
|||
if (existingRole != null) |
|||
{ |
|||
_logger.LogWarning("角色 {RoleName} 已存在", request.Name); |
|||
return OperationResult<CreateRoleResponse>.CreateFailure("角色已存在"); |
|||
} |
|||
|
|||
// 创建角色
|
|||
var role = new AppRole |
|||
{ |
|||
Name = request.Name, |
|||
Description = request.Description |
|||
}; |
|||
|
|||
var result = await _roleManager.CreateAsync(role); |
|||
if (!result.Succeeded) |
|||
{ |
|||
var errors = result.Errors.Select(e => e.Description).ToList(); |
|||
_logger.LogWarning("创建角色失败: {Errors}", string.Join(", ", errors)); |
|||
return OperationResult<CreateRoleResponse>.CreateFailure(errors); |
|||
} |
|||
|
|||
_logger.LogInformation("角色 {RoleName} 创建成功", request.Name); |
|||
|
|||
return OperationResult<CreateRoleResponse>.CreateSuccess( |
|||
new CreateRoleResponse(role.Id)); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "创建角色 {RoleName} 失败", request.Name); |
|||
return OperationResult<CreateRoleResponse>.CreateFailure("创建角色失败,请稍后重试"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理删除角色请求
|
|||
/// </summary>
|
|||
public async Task<OperationResult<DeleteRoleResponse>> Handle( |
|||
DeleteRoleCommand request, |
|||
CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
// 查找角色
|
|||
var role = await _roleManager.FindByIdAsync(request.RoleId); |
|||
if (role == null) |
|||
{ |
|||
_logger.LogWarning("角色 {RoleId} 不存在", request.RoleId); |
|||
return OperationResult<DeleteRoleResponse>.CreateFailure("角色不存在"); |
|||
} |
|||
|
|||
// 删除角色
|
|||
var result = await _roleManager.DeleteAsync(role); |
|||
if (!result.Succeeded) |
|||
{ |
|||
var errors = result.Errors.Select(e => e.Description).ToList(); |
|||
_logger.LogWarning("删除角色失败: {Errors}", string.Join(", ", errors)); |
|||
return OperationResult<DeleteRoleResponse>.CreateFailure(errors); |
|||
} |
|||
|
|||
_logger.LogInformation("角色 {RoleId} 删除成功", request.RoleId); |
|||
|
|||
return OperationResult<DeleteRoleResponse>.CreateSuccess( |
|||
new DeleteRoleResponse(true)); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "删除角色 {RoleId} 失败", request.RoleId); |
|||
return OperationResult<DeleteRoleResponse>.CreateFailure("删除角色失败,请稍后重试"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 处理获取角色请求
|
|||
/// </summary>
|
|||
public async Task<OperationResult<GetRoleResponse>> Handle( |
|||
GetRoleQuery request, |
|||
CancellationToken cancellationToken) |
|||
{ |
|||
try |
|||
{ |
|||
// 查找角色
|
|||
var role = await _roleManager.FindByIdAsync(request.RoleId); |
|||
if (role == null) |
|||
{ |
|||
_logger.LogWarning("角色 {RoleId} 不存在", request.RoleId); |
|||
return OperationResult<GetRoleResponse>.CreateFailure("角色不存在"); |
|||
} |
|||
|
|||
_logger.LogInformation("获取角色 {RoleId} 成功", request.RoleId); |
|||
|
|||
return OperationResult<GetRoleResponse>.CreateSuccess( |
|||
new GetRoleResponse(role.Id, role.Name, role.Description)); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "获取角色 {RoleId} 失败", request.RoleId); |
|||
return OperationResult<GetRoleResponse>.CreateFailure("获取角色失败,请稍后重试"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using MediatR; |
|||
|
|||
namespace CellularManagement.Application.Features.Roles.Queries.GetRole; |
|||
|
|||
/// <summary>
|
|||
/// 获取角色查询
|
|||
/// </summary>
|
|||
public sealed record GetRoleQuery( |
|||
/// <summary>
|
|||
/// 角色ID
|
|||
/// </summary>
|
|||
string RoleId) : IRequest<OperationResult<GetRoleResponse>>; |
|||
@ -0,0 +1,20 @@ |
|||
namespace CellularManagement.Application.Features.Roles.Queries.GetRole; |
|||
|
|||
/// <summary>
|
|||
/// 获取角色响应
|
|||
/// </summary>
|
|||
public sealed record GetRoleResponse( |
|||
/// <summary>
|
|||
/// 角色ID
|
|||
/// </summary>
|
|||
string Id, |
|||
|
|||
/// <summary>
|
|||
/// 角色名称
|
|||
/// </summary>
|
|||
string Name, |
|||
|
|||
/// <summary>
|
|||
/// 角色描述
|
|||
/// </summary>
|
|||
string? Description); |
|||
@ -1,44 +0,0 @@ |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace CellularManagement.Infrastructure.Migrations |
|||
{ |
|||
/// <inheritdoc />
|
|||
public partial class AddDefaultRoles : Migration |
|||
{ |
|||
/// <inheritdoc />
|
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
// 插入默认角色
|
|||
migrationBuilder.InsertData( |
|||
table: "Roles", |
|||
columns: new[] { "Id", "Name", "NormalizedName", "ConcurrencyStamp" }, |
|||
values: new object[,] |
|||
{ |
|||
{ |
|||
"1", // Admin 角色 ID
|
|||
"Admin", |
|||
"ADMIN", |
|||
Guid.NewGuid().ToString() |
|||
}, |
|||
{ |
|||
"2", // User 角色 ID
|
|||
"User", |
|||
"USER", |
|||
Guid.NewGuid().ToString() |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
// 删除插入的角色
|
|||
migrationBuilder.DeleteData( |
|||
table: "Roles", |
|||
keyColumn: "Id", |
|||
keyValues: new object[] { "1", "2" }); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,208 @@ |
|||
// <auto-generated />
|
|||
using System; |
|||
using CellularManagement.Infrastructure.Context; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
|||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace CellularManagement.Infrastructure.Migrations |
|||
{ |
|||
[DbContext(typeof(AppDbContext))] |
|||
[Migration("20250429071554_AddRoleDescription")] |
|||
partial class AddRoleDescription |
|||
{ |
|||
/// <inheritdoc />
|
|||
protected override void BuildTargetModel(ModelBuilder modelBuilder) |
|||
{ |
|||
#pragma warning disable 612, 618
|
|||
modelBuilder |
|||
.HasAnnotation("ProductVersion", "8.0.0") |
|||
.HasAnnotation("Relational:MaxIdentifierLength", 63); |
|||
|
|||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); |
|||
|
|||
modelBuilder.Entity("CellularManagement.Domain.Entities.AppRole", b => |
|||
{ |
|||
b.Property<string>("Id") |
|||
.HasColumnType("text") |
|||
.HasComment("角色ID,主键"); |
|||
|
|||
b.Property<string>("ConcurrencyStamp") |
|||
.IsConcurrencyToken() |
|||
.HasColumnType("text") |
|||
.HasComment("并发控制戳"); |
|||
|
|||
b.Property<string>("Description") |
|||
.HasMaxLength(500) |
|||
.HasColumnType("character varying(500)") |
|||
.HasComment("角色描述"); |
|||
|
|||
b.Property<string>("Name") |
|||
.IsRequired() |
|||
.HasMaxLength(256) |
|||
.HasColumnType("character varying(256)") |
|||
.HasComment("角色名称"); |
|||
|
|||
b.Property<string>("NormalizedName") |
|||
.IsRequired() |
|||
.HasMaxLength(256) |
|||
.HasColumnType("character varying(256)") |
|||
.HasComment("标准化角色名称(大写)"); |
|||
|
|||
b.HasKey("Id"); |
|||
|
|||
b.HasIndex("Name") |
|||
.IsUnique() |
|||
.HasDatabaseName("IX_Roles_Name"); |
|||
|
|||
b.HasIndex("NormalizedName") |
|||
.IsUnique() |
|||
.HasDatabaseName("RoleNameIndex"); |
|||
|
|||
b.ToTable("Roles", null, t => |
|||
{ |
|||
t.HasComment("角色表"); |
|||
}); |
|||
}); |
|||
|
|||
modelBuilder.Entity("CellularManagement.Domain.Entities.AppUser", b => |
|||
{ |
|||
b.Property<string>("Id") |
|||
.HasColumnType("text") |
|||
.HasComment("用户ID,主键"); |
|||
|
|||
b.Property<int>("AccessFailedCount") |
|||
.HasColumnType("integer") |
|||
.HasComment("登录失败次数"); |
|||
|
|||
b.Property<string>("ConcurrencyStamp") |
|||
.IsConcurrencyToken() |
|||
.HasColumnType("text") |
|||
.HasComment("并发控制戳"); |
|||
|
|||
b.Property<string>("Email") |
|||
.IsRequired() |
|||
.HasMaxLength(256) |
|||
.HasColumnType("character varying(256)") |
|||
.HasComment("电子邮箱"); |
|||
|
|||
b.Property<bool>("EmailConfirmed") |
|||
.HasColumnType("boolean") |
|||
.HasComment("邮箱是否已验证"); |
|||
|
|||
b.Property<bool>("LockoutEnabled") |
|||
.HasColumnType("boolean") |
|||
.HasComment("是否启用账户锁定"); |
|||
|
|||
b.Property<DateTimeOffset?>("LockoutEnd") |
|||
.HasColumnType("timestamp with time zone") |
|||
.HasComment("账户锁定结束时间"); |
|||
|
|||
b.Property<string>("NormalizedEmail") |
|||
.HasMaxLength(256) |
|||
.HasColumnType("character varying(256)") |
|||
.HasComment("标准化电子邮箱(大写)"); |
|||
|
|||
b.Property<string>("NormalizedUserName") |
|||
.HasMaxLength(256) |
|||
.HasColumnType("character varying(256)") |
|||
.HasComment("标准化用户名(大写)"); |
|||
|
|||
b.Property<string>("PasswordHash") |
|||
.HasColumnType("text") |
|||
.HasComment("密码哈希值"); |
|||
|
|||
b.Property<string>("PhoneNumber") |
|||
.IsRequired() |
|||
.HasColumnType("text") |
|||
.HasComment("电话号码"); |
|||
|
|||
b.Property<bool>("PhoneNumberConfirmed") |
|||
.HasColumnType("boolean") |
|||
.HasComment("电话号码是否已验证"); |
|||
|
|||
b.Property<string>("SecurityStamp") |
|||
.HasColumnType("text") |
|||
.HasComment("安全戳,用于并发控制"); |
|||
|
|||
b.Property<bool>("TwoFactorEnabled") |
|||
.HasColumnType("boolean") |
|||
.HasComment("是否启用双因素认证"); |
|||
|
|||
b.Property<string>("UserName") |
|||
.IsRequired() |
|||
.HasMaxLength(256) |
|||
.HasColumnType("character varying(256)") |
|||
.HasComment("用户名"); |
|||
|
|||
b.HasKey("Id"); |
|||
|
|||
b.HasIndex("Email") |
|||
.IsUnique() |
|||
.HasDatabaseName("IX_Users_Email"); |
|||
|
|||
b.HasIndex("NormalizedEmail") |
|||
.HasDatabaseName("EmailIndex"); |
|||
|
|||
b.HasIndex("NormalizedUserName") |
|||
.IsUnique() |
|||
.HasDatabaseName("UserNameIndex"); |
|||
|
|||
b.HasIndex("PhoneNumber") |
|||
.IsUnique() |
|||
.HasDatabaseName("IX_Users_PhoneNumber"); |
|||
|
|||
b.HasIndex("UserName") |
|||
.IsUnique() |
|||
.HasDatabaseName("IX_Users_UserName"); |
|||
|
|||
b.ToTable("Users", null, t => |
|||
{ |
|||
t.HasComment("用户表"); |
|||
}); |
|||
}); |
|||
|
|||
modelBuilder.Entity("CellularManagement.Domain.Entities.UserRole", b => |
|||
{ |
|||
b.Property<string>("UserId") |
|||
.HasColumnType("text"); |
|||
|
|||
b.Property<string>("RoleId") |
|||
.HasColumnType("text"); |
|||
|
|||
b.HasKey("UserId", "RoleId"); |
|||
|
|||
b.HasIndex("RoleId"); |
|||
|
|||
b.ToTable("UserRoles", null, t => |
|||
{ |
|||
t.HasComment("用户角色关系表"); |
|||
}); |
|||
}); |
|||
|
|||
modelBuilder.Entity("CellularManagement.Domain.Entities.UserRole", b => |
|||
{ |
|||
b.HasOne("CellularManagement.Domain.Entities.AppRole", "Role") |
|||
.WithMany() |
|||
.HasForeignKey("RoleId") |
|||
.OnDelete(DeleteBehavior.Cascade) |
|||
.IsRequired(); |
|||
|
|||
b.HasOne("CellularManagement.Domain.Entities.AppUser", "User") |
|||
.WithMany() |
|||
.HasForeignKey("UserId") |
|||
.OnDelete(DeleteBehavior.Cascade) |
|||
.IsRequired(); |
|||
|
|||
b.Navigation("Role"); |
|||
|
|||
b.Navigation("User"); |
|||
}); |
|||
#pragma warning restore 612, 618
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace CellularManagement.Infrastructure.Migrations |
|||
{ |
|||
/// <inheritdoc />
|
|||
public partial class AddRoleDescription : Migration |
|||
{ |
|||
/// <inheritdoc />
|
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.AddColumn<string>( |
|||
name: "Description", |
|||
table: "Roles", |
|||
type: "character varying(500)", |
|||
maxLength: 500, |
|||
nullable: true, |
|||
comment: "角色描述"); |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.DropColumn( |
|||
name: "Description", |
|||
table: "Roles"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,201 @@ |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using MediatR; |
|||
using CellularManagement.Application.Features.Roles.Commands.CreateRole; |
|||
using CellularManagement.Application.Features.Roles.Commands.DeleteRole; |
|||
using CellularManagement.Application.Features.Roles.Queries.GetRole; |
|||
using CellularManagement.Application.Common; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.AspNetCore.Http; |
|||
using CellularManagement.Presentation.Abstractions; |
|||
|
|||
namespace CellularManagement.Presentation.Controllers; |
|||
|
|||
/// <summary>
|
|||
/// 角色管理控制器
|
|||
/// 提供角色管理相关的 API 接口,包括创建、删除和查询角色功能
|
|||
/// </summary>
|
|||
//[Authorize(Roles = "Admin")] // 只有管理员可以访问
|
|||
public class RolesController : ApiController |
|||
{ |
|||
private readonly ILogger<RolesController> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化角色控制器
|
|||
/// </summary>
|
|||
/// <param name="mediator">MediatR 中介者,用于处理命令和查询</param>
|
|||
/// <param name="logger">日志记录器</param>
|
|||
public RolesController( |
|||
IMediator mediator, |
|||
ILogger<RolesController> logger) : base(mediator) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 创建新角色
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// 示例请求:
|
|||
///
|
|||
/// POST /api/roles/create
|
|||
/// {
|
|||
/// "name": "Manager",
|
|||
/// "description": "部门经理角色,拥有管理权限"
|
|||
/// }
|
|||
///
|
|||
/// 成功响应:
|
|||
///
|
|||
/// {
|
|||
/// "isSuccess": true,
|
|||
/// "data": {
|
|||
/// "roleId": "550e8400-e29b-41d4-a716-446655440000"
|
|||
/// },
|
|||
/// "errorMessages": null
|
|||
/// }
|
|||
///
|
|||
/// 失败响应:
|
|||
///
|
|||
/// {
|
|||
/// "isSuccess": false,
|
|||
/// "data": null,
|
|||
/// "errorMessages": ["角色已存在"]
|
|||
/// }
|
|||
///
|
|||
/// </remarks>
|
|||
/// <param name="command">创建角色命令,包含角色名称和描述</param>
|
|||
/// <returns>
|
|||
/// 创建结果,包含:
|
|||
/// - 成功:返回角色ID
|
|||
/// - 失败:返回错误信息
|
|||
/// </returns>
|
|||
/// <response code="200">创建成功,返回角色ID</response>
|
|||
/// <response code="400">创建失败,返回错误信息</response>
|
|||
[HttpPost] |
|||
[ProducesResponseType(typeof(OperationResult<CreateRoleResponse>), StatusCodes.Status200OK)] |
|||
[ProducesResponseType(typeof(OperationResult<CreateRoleResponse>), StatusCodes.Status400BadRequest)] |
|||
public async Task<ActionResult<OperationResult<CreateRoleResponse>>> CreateRole([FromBody] CreateRoleCommand command) |
|||
{ |
|||
try |
|||
{ |
|||
var result = await mediator.Send(command); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("角色 {RoleName} 创建成功", command.Name); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("角色 {RoleName} 创建失败: {Error}", |
|||
command.Name, |
|||
result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return Ok(result); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "创建角色 {RoleName} 时发生异常", command.Name); |
|||
return StatusCode(StatusCodes.Status500InternalServerError, |
|||
OperationResult<CreateRoleResponse>.CreateFailure("系统错误,请稍后重试")); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 删除角色
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// 示例请求:
|
|||
///
|
|||
/// DELETE /api/roles/delete/{roleId}
|
|||
///
|
|||
/// </remarks>
|
|||
/// <param name="roleId">角色ID</param>
|
|||
/// <returns>
|
|||
/// 删除结果,包含:
|
|||
/// - 成功:返回删除成功标志
|
|||
/// - 失败:返回错误信息
|
|||
/// </returns>
|
|||
/// <response code="200">删除成功</response>
|
|||
/// <response code="400">删除失败,返回错误信息</response>
|
|||
/// <response code="404">角色不存在</response>
|
|||
[HttpDelete("{roleId}")] |
|||
[ProducesResponseType(typeof(OperationResult<DeleteRoleResponse>), StatusCodes.Status200OK)] |
|||
[ProducesResponseType(typeof(OperationResult<DeleteRoleResponse>), StatusCodes.Status400BadRequest)] |
|||
[ProducesResponseType(typeof(OperationResult<DeleteRoleResponse>), StatusCodes.Status404NotFound)] |
|||
public async Task<ActionResult<OperationResult<DeleteRoleResponse>>> DeleteRole(string roleId) |
|||
{ |
|||
try |
|||
{ |
|||
var command = new DeleteRoleCommand(roleId); |
|||
var result = await mediator.Send(command); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("角色 {RoleId} 删除成功", roleId); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("角色 {RoleId} 删除失败: {Error}", |
|||
roleId, |
|||
result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return Ok(result); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "删除角色 {RoleId} 时发生异常", roleId); |
|||
return StatusCode(StatusCodes.Status500InternalServerError, |
|||
OperationResult<DeleteRoleResponse>.CreateFailure("系统错误,请稍后重试")); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取角色信息
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// 示例请求:
|
|||
///
|
|||
/// GET /api/roles/get/{roleId}
|
|||
///
|
|||
/// </remarks>
|
|||
/// <param name="roleId">角色ID</param>
|
|||
/// <returns>
|
|||
/// 角色信息,包含:
|
|||
/// - 成功:返回角色详细信息
|
|||
/// - 失败:返回错误信息
|
|||
/// </returns>
|
|||
/// <response code="200">获取成功,返回角色信息</response>
|
|||
/// <response code="404">角色不存在</response>
|
|||
[HttpGet("{roleId}")] |
|||
[ProducesResponseType(typeof(OperationResult<GetRoleResponse>), StatusCodes.Status200OK)] |
|||
[ProducesResponseType(typeof(OperationResult<GetRoleResponse>), StatusCodes.Status404NotFound)] |
|||
public async Task<ActionResult<OperationResult<GetRoleResponse>>> GetRole(string roleId) |
|||
{ |
|||
try |
|||
{ |
|||
var query = new GetRoleQuery(roleId); |
|||
var result = await mediator.Send(query); |
|||
|
|||
if (result.IsSuccess) |
|||
{ |
|||
_logger.LogInformation("获取角色 {RoleId} 信息成功", roleId); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogWarning("获取角色 {RoleId} 信息失败: {Error}", |
|||
roleId, |
|||
result.ErrorMessages?.FirstOrDefault()); |
|||
} |
|||
|
|||
return Ok(result); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "获取角色 {RoleId} 信息时发生异常", roleId); |
|||
return StatusCode(StatusCodes.Status500InternalServerError, |
|||
OperationResult<GetRoleResponse>.CreateFailure("系统错误,请稍后重试")); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue