52 lines
2.3 KiB
C#
52 lines
2.3 KiB
C#
using GozareshgirProgramManager.Application._Common.Interfaces;
|
|
using GozareshgirProgramManager.Application._Common.Models;
|
|
using GozareshgirProgramManager.Domain._Common;
|
|
using GozareshgirProgramManager.Domain.ProjectAgg.Entities;
|
|
using GozareshgirProgramManager.Domain.ProjectAgg.Entities.Task.TaskSection;
|
|
using GozareshgirProgramManager.Domain.ProjectAgg.Enums;
|
|
using GozareshgirProgramManager.Domain.ProjectAgg.Repositories;
|
|
|
|
namespace GozareshgirProgramManager.Application.Modules.TaskSectionTimeRequests.Commands.CreateTimeRequest;
|
|
|
|
public record CreateTimeRequestCommand(int Hours, int Minutes, string Description,
|
|
TaskSectionTimeRequestType RequestType,Guid TaskSectionId) : IBaseCommand;
|
|
|
|
public class CreateTimeRequestCommandHandler : IBaseCommandHandler<CreateTimeRequestCommand>
|
|
{
|
|
private readonly IAuthHelper _authHelper;
|
|
private readonly ITaskSectionTimeRequestRepository _timeRequestRepository;
|
|
private readonly ITaskSectionRepository _taskSectionRepository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public CreateTimeRequestCommandHandler
|
|
(ITaskSectionTimeRequestRepository timeRequestRepository, IAuthHelper authHelper, IUnitOfWork unitOfWork, ITaskSectionRepository taskSectionRepository)
|
|
{
|
|
_timeRequestRepository = timeRequestRepository;
|
|
_authHelper = authHelper;
|
|
_unitOfWork = unitOfWork;
|
|
_taskSectionRepository = taskSectionRepository;
|
|
}
|
|
|
|
public async Task<OperationResult> Handle(CreateTimeRequestCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var currentUser = _authHelper.GetCurrentUserId();
|
|
if (!currentUser.HasValue)
|
|
{
|
|
return OperationResult.Unauthorized();
|
|
}
|
|
|
|
if (!_taskSectionRepository.Exists(x=>x.Id == request.TaskSectionId))
|
|
{
|
|
return OperationResult.NotFound("وظیفه فرعی مورد نظر یافت نشد");
|
|
}
|
|
|
|
|
|
var requestTimeSpan = TimeSpan.FromHours(request.Hours) + TimeSpan.FromMinutes(request.Minutes);
|
|
|
|
var entity = new TaskSectionTimeRequest(currentUser.Value, request.Description, requestTimeSpan,
|
|
request.RequestType,request.TaskSectionId);
|
|
await _timeRequestRepository.CreateAsync(entity);
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return OperationResult.Success();
|
|
}
|
|
} |