Beispiel #1
0
        public bool AcceptRejectPending(TransferRecord transfer, int selection)
        {
            Dictionary<int, int> accountToUser = accountDAO.AccountToUser();

            //does ID match request?
            int loggedinUserId = GetUserId();
            int sourceUserId = accountToUser[transfer.AccountFrom];
            if (loggedinUserId != sourceUserId)
            {
                return false;
            }

            //is this accept or reject (1 = accept, 2 = reject

            if (selection == 2)
            {
                //Reject
                //update transfer info
                Log log = new Log
                {
                    TransferId = transfer.TransferId,
                    FromId = transfer.AccountFrom,
                    ToId = transfer.AccountTo,
                    Amount = transfer.Amount,
                    Type = TransferType.REQUEST,
                    Status = StatusType.REJECTED
                };

                if (!transferDAO.UpdateTransfer(log))
                {
                    return false;
                }
                else
                {
                    return true;
                }

            }
            else if (selection == 1)
            {
                //accept

                //is there enough money?
                Account sourceAccount = accountDAO.GetAccount(loggedinUserId);
                if (sourceAccount.Balance < transfer.Amount)
                {
                    return false;
                }

                //move money
                Transfer temp = new Transfer
                {
                    FromUser = sourceUserId,
                    ToUser = accountToUser[transfer.AccountTo],
                    Amount = transfer.Amount
                };

                if (!accountDAO.Withdrawal(temp))
                {
                    return false;
                }

                if (!accountDAO.Deposit(temp))
                {
                    return false;
                }

                //update transfer info
                Log log = new Log
                {
                    TransferId = transfer.TransferId,
                    FromId = transfer.AccountFrom,
                    ToId = transfer.AccountTo,
                    Amount = transfer.Amount,
                    Type = TransferType.REQUEST,
                    Status = StatusType.APPROVED
                };

                if (!transferDAO.UpdateTransfer(log))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                //invalid selection
                return false;
            }
        }