Пример #1
0
        public UserTokenTransaction AddTokenTransaction(long userId, DateTime addedDate,
                                                        DateTime confirmedDate, TokenTransactionInsertModel model, TokenTransactionType transactionType)
        {
            var tokenTransaction = _mapper.Map <TokenTransactionInsertModel, UserTokenTransaction>(model);

            tokenTransaction.CustomerId      = userId;
            tokenTransaction.AddedDate       = addedDate;
            tokenTransaction.ConfirmedDate   = confirmedDate;
            tokenTransaction.TransactionType = transactionType;

            _context.UserTokenTransactions.Add(tokenTransaction);

            var user = _context.Users.FirstOrDefault(u => u.Id == userId);

            if (user == null)
            {
                throw new UserDoesNotExistException();
            }
            if (transactionType == TokenTransactionType.Add)
            {
                user.Balance += model.Amount;
            }
            else
            {
                user.Balance -= model.Amount;
            }

            _context.Users.Update(user);
            _context.SaveChanges();
            return(tokenTransaction);
        }
Пример #2
0
 public async Task <UserTokenTransactionModel> CreateTokenTransaction(long userId,
                                                                      TokenTransactionInsertModel model, TokenTransactionType transactionType)
 {
     using (var transaction = await _context.Database.BeginTransactionAsync())
     {
         try
         {
             var currentDateTime  = DateTime.UtcNow;
             var tokenTransaction = _tokenHelper.AddTokenTransaction(userId, currentDateTime,
                                                                     currentDateTime, model, transactionType);
             transaction.Commit();
             return(_mapper.Map <UserTokenTransaction, UserTokenTransactionModel>(tokenTransaction));
         }
         catch (Exception)
         {
             transaction.Rollback();
             throw;
         }
     }
 }
Пример #3
0
        public async Task <IActionResult> CreateSpecificUserTokenTransaction(long userId, [FromBody] TokenTransactionInsertModel model, TokenTransactionType transactionType)
        {
            var result = await _tokenService.CreateTokenTransaction(userId, model, transactionType);

            return(ApiResponder.RespondSuccess(result));
        }