Exemplo n.º 1
0
        public async Task <DataHolder <DepositDto> > CreateDepositAsync(CreateDepositDto depositDto, Guid userId)
        {
            var depositType = await _depositTypeRepository.GetByIdAsync(depositDto.DepositTypeId);

            if (depositType == null)
            {
                return(DataHolder <DepositDto> .CreateFailure("Deposit doesn't exist"));
            }

            if (!await CanUserOpenDepositAsync(userId, depositDto.DepositTypeId))
            {
                return(DataHolder <DepositDto> .CreateFailure("Can't open deposit"));
            }

            var deposit = new Deposit
            {
                UserId        = userId,
                DepositType   = depositType,
                DepositTypeId = depositType.Id,
                Expire        = DateTime.UtcNow.AddMonths(depositType.Months)
            };

            depositType.Deposits.Add(deposit);

            var insertResult = await _depositTypeRepository.UpdateAsync(depositType);

            if (insertResult <= 0)
            {
                return(DataHolder <DepositDto> .CreateFailure("Can't create deposit"));
            }

            return(DataHolder <DepositDto> .CreateSuccess(_mapper.Map <Deposit, DepositDto>(deposit)));
        }
Exemplo n.º 2
0
        public async Task <ResultDto> CreateDepositAsync(CreateDepositDto createDepositDto)
        {
            if (createDepositDto.Amount <= 0)
            {
                return(new ResultDto {
                    Success = false, Message = "Amount should be greater than 0"
                });
            }

            using (var transaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    var deposit     = _mapper.Map <Deposit>(createDepositDto);
                    var depositPlan = await _unitOfWork.DepositPlans.FindAsync(createDepositDto.DepositPlanID);

                    deposit.StartDate = _dateService.Today;
                    deposit.EndDate   = _dateService.Today.AddDays(depositPlan.DayPeriod);

                    var(mainAccount, percentAccount) = await _accountsService.OpenDepositAccountsAsync(deposit.ClientID);

                    deposit.MainAccount    = mainAccount;
                    deposit.PercentAccount = percentAccount;

                    await _unitOfWork.SaveAsync();

                    decimal amount         = createDepositDto.Amount;
                    var     cashboxAccount = await _unitOfWork.Accounts.GetBankCashboxAccount();

                    var developmentFundAccount = await _unitOfWork.Accounts.GetBankDevelopmentFundAccount();

                    cashboxAccount.DebitValue += amount;
                    await _transactionsService.CreateTransaction(cashboxAccount, mainAccount, amount);

                    await _transactionsService.CreateTransaction(mainAccount, developmentFundAccount, amount);

                    _unitOfWork.Deposits.Add(deposit);
                    await _unitOfWork.SaveAsync();

                    await transaction.CommitAsync();

                    return(new ResultDto {
                        Success = true
                    });
                }
                catch (Exception ex)
                {
                    await transaction.RollbackAsync();

                    return(new ResultDto {
                        Success = false, Message = ex.Message
                    });
                }
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult <DepositDto> > PostAsync(CreateDepositDto depositDto)
        {
            var deposit = new DepositEntity
            {
                CustomerCID = depositDto.CustomerCID,
                LastAmount  = depositDto.LastAmount,
                Balance     = depositDto.Balance
            };

            await _depositRepository.CreateAsync(deposit);

            await _publishEndpoint.Publish(new DepositCreated(
                                               deposit.CustomerCID, deposit.LastAmount, deposit.Balance
                                               ));

            return(CreatedAtAction(nameof(GetByIDAsync), new { id = deposit.CustomerCID }, deposit));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([FromBody] CreateDepositDto createDepositDto)
        {
            var userIdHolder = await _userService.GetUserId(User.Identity.Name);

            if (userIdHolder.Status == DataHolderStatus.Unauthorized)
            {
                return(Unauthorized());
            }

            var createdHolder = await _depositService.CreateDepositAsync(createDepositDto, new Guid(userIdHolder.Data));

            if (createdHolder.Status == DataHolderStatus.Failure)
            {
                return(BadRequest(createdHolder.Message));
            }

            return(Ok(createdHolder.Data));
        }