public async Task <Transaction> AddTransactionAsync(int walletId, TransactionInputModel inputModel) { if (inputModel.Type == TransactionType.Unknown) { throw new ArgumentException("Type must be defined"); } if (!inputModel.CategoryId.HasValue && string.IsNullOrWhiteSpace(inputModel.CategoryName)) { throw new ArgumentNullException("Category must be defined"); } var transaction = Mapper.Map <Transaction>(inputModel); transaction.Category = await GetCategoryAsync(inputModel.CategoryId, inputModel.CategoryName, inputModel.Type) ?? throw new NotFoundException("Category not found"); transaction.Wallet = await _walletsService.GetAsync(walletId) ?? throw new NotFoundException("Wallet not found"); return(await _transactionsService.AddAsync(transaction)); }
private async Task SaveTransactionsAsync(BankScrapper.Models.Transaction[] transactions, Account account) { if (transactions?.Any() != true) { return; } foreach (var transaction in transactions) { var category = await GetOrCreateCategoryAsync(transaction.Category); var transactionEntity = _mapper.Map <Transaction>(transaction); transactionEntity.Account = account; transactionEntity.AccountId = account.Id; transactionEntity.Category = category; transactionEntity.CategoryId = category?.Id; await _transactionsService.AddAsync(transactionEntity); } }