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)));
        }
        public async Task <DataHolder <CreditCard> > ChangeCreditLimitAsync(ChangeCreditLimitDto limitDto, Guid userId)
        {
            if (!limitDto.Id.HasValue)
            {
                return(DataHolder <CreditCard> .CreateFailure("Invalid credit card Id"));
            }

            var creditCard = await GetCreditCardById(limitDto.Id.Value, userId);

            if (creditCard == null)
            {
                return(DataHolder <CreditCard> .CreateFailure("Credit card doesn't exist"));
            }

            creditCard.CreditLimit = limitDto.NewLimit;

            var updateResult = await _creditCardRepository.UpdateAsync(creditCard);

            if (updateResult < 0)
            {
                return(DataHolder <CreditCard> .CreateFailure("Can't update credit card"));
            }

            return(DataHolder <CreditCard> .CreateSuccess(creditCard));
        }
Beispiel #3
0
        public async Task <DataHolder <User> > RegisterUserAsync(RegisterUserDto registerUserDto)
        {
            if (registerUserDto == null)
            {
                throw new ArgumentNullException(nameof(registerUserDto));
            }

            if (registerUserDto.TaxpayerСard.Photo.Length > MaxFileSize ||
                registerUserDto.Passport.FirsPage.Length > MaxFileSize ||
                registerUserDto.Passport.SecondPage.Length > MaxFileSize ||
                registerUserDto.Passport.ResidencePage.Length > MaxFileSize)
            {
                return(DataHolder <User> .CreateFailure($"File size can't be > {MaxFileSize} bytes"));
            }

            var user        = _mapper.Map <RegisterUserDto, User>(registerUserDto);
            var passportDto = registerUserDto.Passport;

            SetTaxpayedToUser(user, registerUserDto.TaxpayerСard.Code, await WriteStreamToBufferAsync(registerUserDto.TaxpayerСard.Photo.OpenReadStream()));
            SetPassportToUser(user,
                              await WriteStreamToBufferAsync(passportDto.FirsPage.OpenReadStream()),
                              await WriteStreamToBufferAsync(passportDto.SecondPage.OpenReadStream()),
                              await WriteStreamToBufferAsync(passportDto.ResidencePage.OpenReadStream()));

            var createdStatus = await _userManager.CreateAsync(user, registerUserDto.Password);

            if (!createdStatus.Succeeded)
            {
                return(DataHolder <User> .CreateFailure(createdStatus.CreateErrorsString()));
            }

            return(DataHolder <User> .CreateSuccess(user));
        }
        public async Task <DataHolder <CreditCard> > GetById(Guid id, Guid userId)
        {
            var creditCard = await GetCreditCardById(id, userId);

            if (creditCard == null)
            {
                return(DataHolder <CreditCard> .CreateFailure("Card doesn't exist"));
            }

            return(DataHolder <CreditCard> .CreateSuccess(creditCard));
        }
        public async Task <DataHolder <CreditCard> > ChangePinAsync(ChangePinDto pinDto, Guid userId)
        {
            var creditCard = await GetCreditCardById(pinDto.Id, userId);

            if (creditCard == null)
            {
                return(DataHolder <CreditCard> .CreateFailure("Card don't exits"));
            }

            creditCard.Pin = pinDto.NewPin;

            var updateRes = await _creditCardRepository.UpdateAsync(creditCard);

            if (updateRes < 0)
            {
                return(DataHolder <CreditCard> .CreateFailure("Can't update credit card"));
            }

            return(DataHolder <CreditCard> .CreateSuccess(creditCard));
        }
        public async Task <DataHolder <Pager <TransactionMoneyLogDto> > > GetTransactionMoneyLogAsync(Guid cardId, int itemPerPage, int page)
        {
            var curUserInfo = await _userRepository
                              .Table
                              .AsNoTracking()
                              .FirstOrDefaultAsync(u => u.CreditCards.Any(cc => cc.Id == cardId));

            if (curUserInfo == null)
            {
                return(DataHolder <Pager <TransactionMoneyLogDto> > .CreateFailure("Invalid card id"));
            }

            var logs = _transactionMoneyLogRepository
                       .Table
                       .Where(log => log.UserFromId == curUserInfo.Id);
            var pager = new Pager <TransactionMoneyLogDto>
            {
                Total = await logs.CountAsync(),
                Page  = page,
                Data  = await logs
                        .Join(_userRepository.Table,
                              log => log.UserToId,
                              u => u.Id,
                              (log, user) => new TransactionMoneyLogDto
                {
                    Date            = log.Date,
                    Currency        = log.Currency,
                    Amount          = log.Amount,
                    DestinationUser = new UserLogDto {
                        Name = user.Name, LastName = user.LastName
                    },
                    IsInput = log.IsInput
                })
                        .Page(itemPerPage, page)
                        .ToListAsync()
            };


            return(DataHolder <Pager <TransactionMoneyLogDto> > .CreateSuccess(pager));
        }
        public async Task <DataHolder <CreditCardOrder> > CreateOrderAsync(CreateCreditCardOrderDto dto, User user)
        {
            var creditCardOrder = _mapper.Map <CreateCreditCardOrderDto, CreditCardOrder>(dto);

            if (creditCardOrder.DeliveryType == CreditCardOrderDeliveryType.Address &&
                (string.IsNullOrWhiteSpace(dto.City) ||
                 string.IsNullOrWhiteSpace(dto.FlatNumber) ||
                 string.IsNullOrWhiteSpace(dto.HouseNumber)))
            {
                return(DataHolder <CreditCardOrder> .CreateFailure("City or flat number order house number is empty"));
            }

            creditCardOrder.Status = CreditCardOrderStatus.New;
            creditCardOrder.User   = user;

            var insertResult = await _creditCardOrderRepository.InsertAsync(creditCardOrder);

            if (insertResult < 0)
            {
                return(DataHolder <CreditCardOrder> .CreateFailure("Cant create order"));
            }

            return(DataHolder <CreditCardOrder> .CreateSuccess(creditCardOrder));
        }
        public async Task <DataHolder <CreditCard> > SendMoneyAsync(SendMoneyDto sendMoneyDto, Guid userId)
        {
            var fromCard = await _userRepository
                           .Table
                           .Where(user => user.Id == userId)
                           .SelectMany(user => user.CreditCards)
                           .FirstOrDefaultAsync(card => card.Id == sendMoneyDto.FromCardId);

            if (fromCard == null)
            {
                return(DataHolder <CreditCard> .CreateFailure("Incorrect user id or credit card number"));
            }

            var destCard = await _creditCardRepository
                           .Table
                           .FirstOrDefaultAsync(card => card.CardNumber == sendMoneyDto.ToCardNumber);

            if (destCard == null)
            {
                return(DataHolder <CreditCard> .CreateFailure("Invalid destination card number"));
            }

            var amount = await _convertCurrencyService.ConvertAsync(sendMoneyDto.Currency, (decimal)sendMoneyDto.Amount);

            if (!fromCard.SubtractMoney(amount))
            {
                return(DataHolder <CreditCard> .CreateFailure("Not enough money"));
            }
            if (!destCard.AddMoney(amount))
            {
                return(DataHolder <CreditCard> .CreateFailure("Exceeded the money limit"));
            }

            var updateResult = await _creditCardRepository.UpdateAsync(new List <CreditCard> {
                fromCard, destCard
            });

            if (updateResult <= 0)
            {
                return(DataHolder <CreditCard> .CreateFailure("Can't update balance"));
            }

            await _transactionMoneyLogRepository.InsertAsync(new []
            {
                new TransactionMoneyLog
                {
                    Amount     = amount,
                    Currency   = sendMoneyDto.Currency,
                    UserFromId = userId,
                    UserToId   = destCard.UserId,
                    IsInput    = false
                },
                new TransactionMoneyLog
                {
                    Amount     = amount,
                    Currency   = sendMoneyDto.Currency,
                    UserFromId = destCard.UserId,
                    UserToId   = userId,
                    IsInput    = true
                }
            });

            return(DataHolder <CreditCard> .CreateSuccess(fromCard));
        }