public async Task <OperationResult> Handle(UpdateLoanCommand request, CancellationToken cancellationToken) { await ValidateUpdateCommand(request); var loanDto = _mapper.Map <UpdateLoanCommand, Loan>(request); await _unitOfWork.Loans.UpdateOneAsync(x => x.Id == request.Id, loanDto, _skipProps, cancellationToken); _unitOfWork.Commit(); return(new OperationResult(nameof(HttpStatusCode.OK), true)); }
private async Task ValidateUpdateCommand(UpdateLoanCommand command) { if (!Enum.IsDefined(typeof(LoanTypeEnum), command.LoanTypeId)) { throw new ArgumentOutOfRangeException(nameof(command.LoanTypeId)); } if (command.Id <= 0) { throw new ArgumentOutOfRangeException(nameof(command.Id)); } if (command.Balance <= 0) { throw new ArgumentOutOfRangeException(nameof(command.Balance)); } if (string.IsNullOrWhiteSpace(command.Ccy) || command.Ccy.Length != 3) { throw new ArgumentNullException(nameof(command.Ccy)); } if (command.DateFrom == null || command.DateFrom.Date < DateTime.Today) { throw new ArgumentOutOfRangeException(nameof(command.DateFrom)); } if (command.DateTo == null || command.DateTo <= command.DateFrom) { throw new ArgumentOutOfRangeException(nameof(command.DateTo)); } var exists = await _unitOfWork.Loans.Exists(x => x.Id == command.Id && x.UserId == _userId && x.LoanStatusId == (int)LoanStatusEnum.Sent || x.LoanStatusId == (int)LoanStatusEnum.Processing); if (!exists) { throw new Exception("you don't have right to edit this statement"); } }
public async Task <ActionResult <OperationResult> > UpdateAsync([Required][FromBody] UpdateLoanCommand command, CancellationToken cancellationToken = default) => Ok(await Mediator.Send(command, cancellationToken));