예제 #1
0
        public CardDTO CreateCard(Guid id, string cardHolder)
        {
            try
            {
                _context.BeginTransaction();

                var userBankAccount = _context.BankAccounts.GetAll().FirstOrDefault(x => x.User.Id == id);

                if (userBankAccount == null)
                {
                    return(null);
                }

                var newBankCard = new BankCard
                {
                    CardHolder    = cardHolder,
                    Amount        = 0,
                    BankAccountId = userBankAccount.Id,
                    CardNumber    = _helperService.GenerateCardNumber(),
                    CVC           = _helperService.GenerateCVC()
                };

                _context.BankCards.Add(newBankCard);
                _context.BankAccounts.Edit(userBankAccount);
                _context.Save();

                _context.Commit();

                return(newBankCard.Adapt <CardDTO>());
            }
            catch (Exception e)
            {
                _context.Rollback();
                _logger.LogError(e.Message);
                throw;
            }
        }