Exemplo n.º 1
0
        public async Task<Transaction> CreateTransaction(Book book, Transaction transaction)
        {
            DateTime timeStamp = DateTime.Now;

            if (book == null) throw new PiggyBankDataException("Book object is missing");
            if (transaction == null) throw new PiggyBankDataException("Transaction object is missing");

            transaction.Book = book;
            AccountManager acc = new AccountManager(_dbContext);
            transaction.DebitAccount = await acc.FindAccount(transaction.DebitAccount.Id);
            transaction.CreditAccount = await acc.FindAccount(transaction.CreditAccount.Id);
            ValidateTransaction(transaction);
            transaction.TimeStamp = timeStamp;
            PiggyBankUtility.CheckMandatory(transaction);
            _dbContext.Transactions.Add(transaction);
            await _dbContext.SaveChangesAsync();

            return transaction;
        }
Exemplo n.º 2
0
        public async Task<Transaction> UpdateTransaction(Transaction transaction)
        {
            DateTime timeStamp = DateTime.Now;
            if (transaction == null) throw new PiggyBankDataException("Transaction object is missing");

            Transaction transactionToUpdate = await FindTransaction(transaction.Id);
            if (!transactionToUpdate.IsValid) throw new PiggyBankDataNotFoundException("Transaction [" + transaction.Id + "] cannot be found");
            if (transactionToUpdate.IsClosed) throw new PiggyBankDataException("Closed Transaction cannot be created / updated");
            if (transaction.TimeStamp != transactionToUpdate.TimeStamp) throw new PiggyBankDataConcurrencyException("Transaction [" + transaction.Id + "] is being updated by other process");

            transaction.Book = transactionToUpdate.Book;
            AccountManager acc = new AccountManager(_dbContext);
            transaction.DebitAccount = await acc.FindAccount(transaction.DebitAccount.Id);
            transaction.CreditAccount = await acc.FindAccount(transaction.CreditAccount.Id);
            ValidateTransaction(transaction);
            transaction.TimeStamp = timeStamp;
            PiggyBankUtility.CheckMandatory(transaction);
            PiggyBankUtility.UpdateModel(transactionToUpdate, transaction);
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) { throw new PiggyBankDataConcurrencyException("Transaction [" + transaction.Id + "] is being updated by other process"); }

            return transactionToUpdate;
        }
Exemplo n.º 3
0
        private void ValidateTransaction(Transaction transaction)
        {
            int bookId = transaction.Book.Id;
            string bookCurrency = transaction.Book.Currency;

            // DR/CR account validation
            if (transaction.DebitAccount == null || !transaction.DebitAccount.IsValid) throw new PiggyBankDataException("Invalid Debit Account[" + transaction.DebitAccount.Id + "]");
            if (transaction.CreditAccount == null || !transaction.CreditAccount.IsValid) throw new PiggyBankDataException("Invalid Credit Account[" + transaction.CreditAccount.Id + "]");

            // Book validation
            if (transaction.DebitAccount.Book.Id != bookId) throw new PiggyBankDataException("Debit Account[" + transaction.DebitAccount.Id + "] cannot be found in Book [" + bookId + "]");
            if (transaction.CreditAccount.Book.Id != bookId) throw new PiggyBankDataException("Credit Account[" + transaction.CreditAccount.Id + "] cannot be found in Book [" + bookId + "]");

            // Currency validation
            if (transaction.DebitAccount.Currency != transaction.Currency && transaction.DebitAccount.Currency != bookCurrency) throw new PiggyBankDataException("Invalid DebitAccount.Currency[" + transaction.DebitAccount.Currency + "]");
            if (transaction.CreditAccount.Currency != transaction.Currency && transaction.CreditAccount.Currency != bookCurrency) throw new PiggyBankDataException("Invalid CreditAccount.Currency[" + transaction.CreditAccount.Currency + "]");

            // Book amount validation
            if (transaction.BookAmount <= 0) throw new PiggyBankDataException("Invalid Transaction.BookAmount [" + transaction.BookAmount + "]");

            // Amount validation
            if (transaction.BookAmount < 0) throw new PiggyBankDataException("Invalid Transaction.Amount [" + transaction.Amount + "]");

            // IsClosed validation
            if (transaction.IsClosed) throw new PiggyBankDataException("Closed Transaction cannot be created / updated");

        }
Exemplo n.º 4
0
 private void UpdateAccountClosing(Transaction transaction, Account account, int debitSign, DateTime closingDate, DateTime timestamp)
 {
     if (account.Closing == null)
     {
         account.Closing = new AccountClosing();
         account.Closing.Account = account;
         account.Closing.Id = account.Id;
     }
     account.Closing.Amount += account.DebitSign * debitSign * transaction.Amount;
     account.Closing.BookAmount += account.DebitSign * debitSign * transaction.BookAmount;
     account.Closing.ClosingDate = closingDate;
     account.Closing.TimeStamp = timestamp;
 }