Exemplo n.º 1
0
        public bool AddTransaction(string sender, string recipient, decimal amount, string fromAccount, string toAccount, decimal ratePerDay, string message = null)
        {
            User    from    = FirstUser(u => u.Name.Equals(sender));
            User    to      = FirstUser(u => u.Name.Equals(recipient));
            Account fromAcc = from?.GetAccount(fromAccount);
            Account toAcc   = to.GetAccount(toAccount);

            // Errant states
            if (
                to == null ||
                (from == null && !to.IsAdministrator) ||
                toAcc == null ||
                (from != null && fromAcc == null) ||
                (from != null && fromAcc.ComputeBalance(ratePerDay) < amount)
                )
            {
                return(false);
            }

            Transaction tx = new Transaction(from == null ? "System" : from.Name, to.Name, amount, message, fromAccount, toAccount);

            toAcc.History.Add(tx);
            toAcc.UpdateBalance(amount, ratePerDay);
            AddUser(to, false); // Let's not flush unnecessarily
            //UpdateUser(to); // For debugging: Force a flush
            if (from != null)
            {
                fromAcc.History.Add(tx);
                fromAcc.UpdateBalance(-amount, ratePerDay);
                AddUser(from, false);
            }
            return(true);
        }