48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using GozareshgirProgramManager.Application._Common.Interfaces;
|
|
using GozareshgirProgramManager.Application._Common.Models;
|
|
using GozareshgirProgramManager.Domain._Common;
|
|
using GozareshgirProgramManager.Domain.ProjectAgg.Repositories;
|
|
using MediatR;
|
|
|
|
namespace GozareshgirProgramManager.Application.Modules.Projects.Commands.AddPhaseToProject;
|
|
|
|
public class AddPhaseToProjectCommandHandler : IRequestHandler<AddPhaseToProjectCommand, OperationResult>
|
|
{
|
|
private readonly IProjectRepository _projectRepository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public AddPhaseToProjectCommandHandler(
|
|
IProjectRepository projectRepository,
|
|
IUnitOfWork unitOfWork)
|
|
{
|
|
_projectRepository = projectRepository;
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task<OperationResult> Handle(AddPhaseToProjectCommand request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
// Get project
|
|
var project = await _projectRepository.GetByIdAsync(request.ProjectId);
|
|
if (project == null)
|
|
{
|
|
return OperationResult.NotFound("پروژه یافت نشد");
|
|
}
|
|
|
|
// Add phase
|
|
var phase = project.AddPhase(request.Name, request.Description);
|
|
phase.SetOrderIndex(request.OrderIndex);
|
|
|
|
// Save changes
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
|
|
return OperationResult.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return OperationResult.Failure($"خطا در افزودن فاز: {ex.Message}");
|
|
}
|
|
}
|
|
}
|