ChangeStepWeightCommandHandler.cs 1.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
using Ardalis.Result;
using PSManagement.Domain.Projects.DomainErrors;
using PSManagement.Domain.Projects.Entities;
using PSManagement.Domain.Projects.Repositories;
using PSManagement.SharedKernel.CQRS.Command;
using System.Threading;
using System.Threading.Tasks;

namespace PSManagement.Application.Steps.UseCases.Commands.ChangeStepWeight
{
    public class ChangeStepWeightCommandHandler : ICommandHandler<ChangeStepWeightCommand, Result>
    {
        private readonly IStepsRepository _stepsRepository;
        

        public ChangeStepWeightCommandHandler(IStepsRepository stepsRepository)
        {
            _stepsRepository = stepsRepository;
        }

        public async Task<Result> Handle(ChangeStepWeightCommand request, CancellationToken cancellationToken)
        {
            Step step = await _stepsRepository.GetByIdAsync(request.StepId);
            if (step is null)
            {
                return Result.Invalid(StepsErrors.InvalidEntryError);
            }
            else
            {
                if (request.Weight < 0) {
                    return Result.Invalid(StepsErrors.InvalidWeightError);
                }

hasan khaddour's avatar
hasan khaddour committed
34
                step.UpdateWeight(request.Weight);
35 36 37 38 39 40 41 42 43 44 45 46
                await _stepsRepository.UpdateAsync(step);

                
                return Result.Success();



            }
        }
    }

}