Пример #1
0
        /// <summary>
        /// Add or edit loan
        /// </summary>
        /// <param name="loanId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditLoanAsync(int loanId, CreateLoanRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();


            if (loanId != 0)
            {
                var loan = await _loansRepo.FindByIdAsync(loanId);

                try
                {
                    if (loan != null)
                    {
                        loan.CustomerId    = request.CustomerId;
                        loan.BankAccountId = request.BankAccountId;
                        loan.LoanType      = request.LoanType;
                        loan.Amount        = request.Amount;
                        loan.DueDate       = request.DueDate;

                        await _loansRepo.UpdateAsync(loan);
                    }
                    else
                    {
                        responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(loan), $"loan of id: { loanId} not found"));
                        return(responseModel);
                    }
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }
            else
            {
                try
                {
                    await _loansRepo.AddAsync(CreateLoan(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }
        public Task HandleAsync(CreateLoanCommand command)
        {
            var loan = mapper.Map <LoanEntity>(command);

            return(loansRepository.AddAsync(loan));
        }