- 从多个文件中移除未使用的 System.Linq.Dynamic.Core 引用 - 从多个文件中移除未使用的 Microsoft.EntityFrameworkCore 引用 - 从 IM 模块中移除未使用的 MongoDB.Driver.Linq 引用 - 清理 Handler 文件中的冗余命名空间引用 - 优化代码结构减少不必要的依赖引入 - 提高代码整洁性和编译效率
33 lines
1.2 KiB
C#
33 lines
1.2 KiB
C#
using BuildFlow.Shared.Domain.Common;
|
|
using File.Application.Features.Folders.Commands;
|
|
using File.Domain.Exceptions;
|
|
using File.Infrastructure.Persistence;
|
|
using MediatR;
|
|
|
|
namespace File.Application.Features.Folders.Handlers;
|
|
|
|
/// <summary>移动文件命令处理器。校验目标文件夹归属与文件归属后改挂。</summary>
|
|
|
|
public class MoveFileCommandHandler(FileDbContext db, ICurrentUserContext currentUser)
|
|
: IRequestHandler<MoveFileCommand, Unit>
|
|
{
|
|
public async Task<Unit> Handle(MoveFileCommand req, CancellationToken ct)
|
|
{
|
|
var owner = currentUser.GetRequiredUserId();
|
|
|
|
if (req.TargetFolderId is not null)
|
|
{
|
|
var target = await db.Folders.FirstOrDefaultAsync(f => f.Id == req.TargetFolderId, ct);
|
|
if (target is null) throw new NotFoundException("目标文件夹不存在");
|
|
if (target.OwnerId != owner) throw new ForbiddenException("无权访问目标文件夹");
|
|
}
|
|
|
|
var file = await db.Files.FirstOrDefaultAsync(f => f.Id == req.Id && f.OwnerId == owner, ct)
|
|
?? throw new NotFoundException("文件不存在或无权操作");
|
|
|
|
file.FolderId = req.TargetFolderId;
|
|
await db.SaveChangesAsync(ct);
|
|
return Unit.Value;
|
|
}
|
|
}
|