Exemplo n.º 1
0
        public decimal TransferFunds(Transfer t)
        {
            Account sender   = AccountDAO.GetAccountById(t.AccountFrom);
            Account receiver = AccountDAO.GetAccountById(t.AccountTo);

            return(TransferDAO.TransferFunds(t.Amount, sender, receiver, (int)GetCurrentUserId()));
        }
Exemplo n.º 2
0
        public ActionResult <Transfer> CreateTransfer(Transfer transferToCreate)
        {
            Transfer createdTransfer = null;

            //step 1) create the transfer
            //are either of the accounts null?
            if (accountDAO.GetAccountById(transferToCreate.AccountFromId) == null || accountDAO.GetAccountById(transferToCreate.AccountToId) == null)
            {
                return(UnprocessableEntity("One or both Account Id's are invalid."));//422
            }
            //is someone trying to transfer zero or negative money?
            if ((float)transferToCreate.Amount <= 0)
            {
                return(UnprocessableEntity("The amount to transfer must be greater than zero!"));//422
            }
            //does the from account have enough money to complete the transfer? only applicable to SEND transfers
            if ((transferToCreate.TransferType == TransferType.Send) && accountDAO.GetAccountById(transferToCreate.AccountFromId).Balance < transferToCreate.Amount)
            {
                return(UnprocessableEntity("Insufficient funds to complete the transfer."));//422
            }

            //set the transfer status based on the transfer type

            /* So this might sound weird, but hear me out. If the transfer is type send, we will default the status to rejected.
             * Why? we're going to add a transfer to the database fully. if no errors occurred, then we will execute it
             * This is done in two steps.
             *
             * If the creation of the transfer was successful, but the execution was not successful, we want to handle this.
             */
            switch (transferToCreate.TransferType)
            {
            case TransferType.Send:
                transferToCreate.TransferStatus = TransferStatus.Rejected;
                break;

            case TransferType.Request:
                transferToCreate.TransferStatus = TransferStatus.Pending;
                break;
            }
            createdTransfer = transferDAO.CreateTransfer(transferToCreate);
            //okay the transfer is in the DB. Now we need to try to execute it
            if (createdTransfer == null)
            {
                return(StatusCode(500, "The server was unable to create the transfer"));
            }
            if (createdTransfer.TransferType == TransferType.Send)
            {
                createdTransfer = ExecuteTransfer(createdTransfer);
                if (createdTransfer.TransferStatus == TransferStatus.Rejected)
                {
                    //this should not happen ever
                    return(StatusCode(500, "The Server rejected the transfer."));
                }
            }
            return(Created($"{createdTransfer.TransferId}", createdTransfer));
        }
Exemplo n.º 3
0
        public ActionResult <List <object> > GetPublicInfoForAccount(int accountId)
        {
            Account       account = accountDAO.GetAccountById(accountId);
            User          user    = null;
            List <object> result  = new List <object>();

            if (account != null)
            {
                account.Balance = decimal.MinValue;//protects sensitive data
                user            = userDAO.GetUser(account.UserId);
                if (user == null)
                {
                    return(StatusCode(500, "The account exists, but it's owner does not. this should not be possible"));
                }
                result.Add(user);
                result.Add(account);
                return(Ok(result));
            }
            else
            {
                return(BadRequest("That account doesn't exist."));
            }
        }
Exemplo n.º 4
0
 public AccountEntity GetAccountById(int id)
 {
     return(_accountDAO.GetAccountById(id));
 }