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); }
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; } } }
public async Task <IActionResult> CreateSpecificUserTokenTransaction(long userId, [FromBody] TokenTransactionInsertModel model, TokenTransactionType transactionType) { var result = await _tokenService.CreateTokenTransaction(userId, model, transactionType); return(ApiResponder.RespondSuccess(result)); }