Exemplo n.º 1
0
        public async Task <EsUpdateJudgeCommandResponse> Handle(EsUpdateJudgeCommand request, CancellationToken cancellationToken)
        {
            var validator       = new EsUpdateJudgeCommandValidator();
            var validatorResult = await validator.ValidateAsync(request);

            if (!validatorResult.IsValid)
            {
                return(new EsUpdateJudgeCommandResponse(validatorResult));
            }

            var judgedto = _mapper.Map <JudgeDto>(request);
            var judge    = _mapper.Map <Judge>(judgedto);

            var eventstoreResult = Get <JudgeAggregate>(judge.UniqueId.GetAggregateKey());

            if (!eventstoreResult.Success)
            {
                return(await Task.FromResult(new
                                             EsUpdateJudgeCommandResponse(eventstoreResult.RemoveGeneric())));
            }

            if ((eventstoreResult.Value.Version - 1) > judge.Version)
            {
                return(new EsUpdateJudgeCommandResponse
                           (ExecutionStatus.EventStoreConcurrencyError
                               (@$ "You sended old version. Your version {judge.Version}. Should be :{eventstoreResult.Value.Version - 1}")));
            }

            eventstoreResult.Value.Update(judge);
            _sessionForEventSourcing.Commit();

            return(new EsUpdateJudgeCommandResponse());
        }