/// <inheritdoc />
        public async Task AddTransaction(UserTransactionDto dto)
        {
            var user = await _userRepository.GetUserWithTransactions(dto.UserId);

            if (user == null)
            {
                throw new BusinessException(nameof(dto.UserId), "User does not exist", ErrorStatus.DataNotFound);
            }
            VerifyTransaction(dto, user);
            user.Budget -= dto.Quantity * dto.Price;
            user.Transactions.Add(new UserTransaction
            {
                UserId    = dto.UserId,
                CompanyId = dto.CompanyId,
                Date      = dto.Date,
                Price     = dto.Price,
                Quantity  = dto.Quantity
            });
            var saveTask   = _userRepository.Save();
            var clearCache = _transactionsRepository.ClearTransactionsCache(dto.UserId);
            await Task.WhenAll(saveTask, clearCache);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task EditBudget(int userId, decimal newBudget)
        {
            var user = await _userRepository.GetUser(userId);

            if (user != null)
            {
                user.Budget = newBudget;
            }
            else
            {
                throw new BusinessException(nameof(userId), "User does not exist", ErrorStatus.DataNotFound);
            }
            await _userRepository.Save();

            await _transactionsRepository.ClearTransactionsCache(userId);
        }