using GozareshgirProgramManager.Application._Common.Interfaces; using GozareshgirProgramManager.Application._Common.Models; using GozareshgirProgramManager.Application.Modules.TaskChat.DTOs; using GozareshgirProgramManager.Application.Services.FileManagement; using GozareshgirProgramManager.Domain._Common; using GozareshgirProgramManager.Domain._Common.Exceptions; using GozareshgirProgramManager.Domain.FileManagementAgg.Entities; using GozareshgirProgramManager.Domain.FileManagementAgg.Enums; using GozareshgirProgramManager.Domain.FileManagementAgg.Repositories; using GozareshgirProgramManager.Domain.ProjectAgg.Entities; using GozareshgirProgramManager.Domain.ProjectAgg.Enums; using GozareshgirProgramManager.Domain.ProjectAgg.Repositories; using Microsoft.AspNetCore.Http; namespace GozareshgirProgramManager.Application.Modules.Projects.Commands.CreateBugSection; public class CreateBugSectionCommandHandler : IBaseCommandHandler { readonly IUnitOfWork _unitOfWork; readonly IBugSectionRepository _bugSectionRepository; readonly IFileUploadService _fileUploadService; private readonly IAuthHelper _authHelper; public CreateBugSectionCommandHandler(IUnitOfWork unitOfWork, IBugSectionRepository bugSectionRepository, IAuthHelper authHelper, IFileUploadService fileUploadService) { _unitOfWork = unitOfWork; _bugSectionRepository = bugSectionRepository; _authHelper = authHelper; _fileUploadService = fileUploadService; } public async Task Handle(CreateBugSectionCommand request, CancellationToken cancellationToken) { var currentUserId = _authHelper.GetCurrentUserId() ?? throw new UnAuthorizedException("کاربر احراز هویت نشده است"); #region Validation if (_bugSectionRepository.Exists(x => x.TaskId == request.TaskId)) return OperationResult.Failure("برای این بخش قبلا تسک باگ ایجاد شده است"); if (string.IsNullOrWhiteSpace(request.InitialDescription)) return OperationResult.Failure("توضیحات باگ خالی است"); if (request.OriginalAssignedUserId == 0) return OperationResult.Failure("کاربر انتخاب نشده است"); #endregion var bug = new BugSection(request.TaskId, request.InitialDescription, request.OriginalAssignedUserId, request.Priority); await _bugSectionRepository.CreateAsync(bug); if (request.Files.Any()) { foreach (var file in request.Files) { var uploadedFile = await _fileUploadService.UploadFileAsync ( file, FileCategory.BugSection, currentUserId ); if (!uploadedFile.IsSuccess) { return OperationResult.Failure(uploadedFile.ErrorMessage ?? "خطا در آپلود فایل"); } bug.AddDocument(new BugDocument(uploadedFile.FileId!.Value)); } } await _unitOfWork.SaveChangesAsync(cancellationToken); return OperationResult.Success(); } } public record CreateBugSectionCommand(Guid TaskId, string InitialDescription, long OriginalAssignedUserId, ProjectTaskPriority Priority, List Files) : IBaseCommand;