diff --git a/bank b/bank new file mode 100755 index 0000000..9d54d5f --- /dev/null +++ b/bank @@ -0,0 +1,184 @@ +#!/usr/bin/env sh + + +### Config +### # Get script directory +### SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +### # Source DB config +### source $SCRIPT_DIR/.dndbank.conf +source ${HOME}/.dndbank.conf + +### Error Handling +fail_badsel() { + printf "\n$(tput setaf 3)ERROR 10:$(tput sgr0) $(tput bold)Bad selection!$(tput sgr0)\n\n" + # Restart at main menu + show_menu +} + +### Menu +pre_menu() { + echo "Press [Enter] to continue..." + read +} + +show_menu() { + [ "$1" == "init" ] || pre_menu + printf 'Select a function by number: + +1. Create Account +2. Deposit +3. Withdrawal +4. Show All Balances +5. Show All Transactions +6. Show Transactions for Account +9. Delete Account (!) + +0. Quit + + +' + read -p "Selection: " -n1 action + case $action in + 1) + echo " Create Account" + create_account + ;; + 2) + echo " Make a Desposit" + make_deposit + ;; + 3) + echo " Make a Withdrawal" + make_withdrawal + ;; + 4) + echo " Show All Balances" + show_balance + ;; + 5) + echo " Show All Transactions" + show_transactions_all + ;; + 6) + echo " Show Transactions for Account" + show_transactions + ;; + 9) + echo " Delete Account" + delete_account + ;; + [0qQ]) + echo " Quit" + exit 0 + ;; + *) + fail_badsel + ;; + esac +} + + +### Primary Functions ### +dbconnect="psql -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} -d ${DB_NAME}" + +create_account() { + read -p "Enter Account Name: " name + tstamp=$(date +%Y-%m-%d\ %H:%M:%S) + read -p "Creating account: '${name}'. Are you sure? (y/N)" -n1 CONFIRM + case $CONFIRM in + [Yy]) + printf "\n$(tput bold)Creating account...\n\n$(tput sgr0)" + eval "$dbconnect -c \"INSERT INTO account (name,created) VALUES ('${name}','${tstamp}');\"" && + eval "$dbconnect -c \"INSERT INTO balance (id,gold) VALUES ((SELECT id FROM account WHERE name = '${name}'),'0');\"" && + printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" || + printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)" + ;; + *) + printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)" + ;; + esac + show_menu +} + +delete_account() { + printf "\n$(tput bold)$(tput setaf 3)This action is not yet supported!\n\n$(tput sgr0)" + # # Close/Delete Account + # DELETE FROM balance WHERE id = (SELECT id FROM account WHERE name = ${name}); + # DELETE FROM transaction WHERE account = (SELECT id FROM account WHERE name = ${name}); + # DELETE FROM account WHERE name = ${name}; + show_menu +} + +make_deposit() { + eval "$dbconnect -c \"SELECT * FROM account;\"" + read -p "Enter Account ID: " ID + read -p "Enter amount of GP to deposit: " amount + tstamp=$(date +%Y-%m-%d\ %H:%M:%S) + read -p "Depositing ${amount} into account ${ID}. Are you sure? (y/N)" -n1 CONFIRM + case $CONFIRM in + [Yy]) + printf "\n$(tput bold)Depositing...\n\n$(tput sgr0)" + eval "$dbconnect -c \"INSERT INTO transaction (account,deposit,timestamp) VALUES ('${ID}','${amount}','${tstamp}');\"" + eval "$dbconnect -c \"UPDATE balance SET gold = gold + '${amount}' WHERE id = '${ID}';\"" + printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" || + printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)" + printf "\nNew balance:\n" + eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id where balance.id = ${ID};\"" + ;; + *) + printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)" + ;; + esac + show_menu +# # Transaction (Deposit) +# INSERT INTO transaction (account,deposit,timestamp) VALUES ((SELECT id FROM account WHERE name = ${name}),${amount},${tstamp}); +# UPDATE balance SET gold = gold + 123.45 WHERE id = (SELECT id FROM account WHERE name = ${name}); +} + +make_withdrawal() { + eval "$dbconnect -c \"SELECT * FROM account;\"" + read -p "Enter Account ID: " ID + read -p "Enter amount of GP to withdraw: " amount + tstamp=$(date +%Y-%m-%d\ %H:%M:%S) + read -p "Withdrawing ${amount} from account ${ID}. Are you sure? (y/N)" -n1 CONFIRM + case $CONFIRM in + [Yy]) + printf "\n$(tput bold)Withdrawing...\n\n$(tput sgr0)" + eval "$dbconnect -c \"INSERT INTO transaction (account,withdrawal,timestamp) VALUES ('${ID}','${amount}','${tstamp}');\"" + eval "$dbconnect -c \"UPDATE balance SET gold = gold - '${amount}' WHERE id = '${ID}';\"" + printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" || + printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)" + printf "\nNew balance:\n" + eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id where balance.id = ${ID};\"" + ;; + *) + printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)" + ;; + esac + show_menu +# # Transaction (Withdraw) +# INSERT INTO transaction (account,withdrawal,timestamp) VALUES ((SELECT id FROM account WHERE name = ${name}),${amount},${tstamp}); +# UPDATE balance SET gold = gold - 12.34 WHERE id = (SELECT id FROM account WHERE name = ${name}); +} + +show_balance() { + eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id ORDER BY name;\"" + show_menu +} + +show_transactions_all() { + eval "$dbconnect -c \"SELECT transaction.id,account.name,withdrawal,deposit,timestamp FROM transaction INNER JOIN account ON account.id = transaction.account ORDER BY timestamp;\"" + show_menu +} + +show_transactions() { + read -p "Enter search term (e.g. character name): " name + echo + eval "$dbconnect -c \"SELECT transaction.id,account.name,withdrawal,deposit,timestamp FROM transaction INNER JOIN account ON account.id = transaction.account ORDER BY timestamp;\"" | grep -iE "$name|timestamp|------" + echo + show_menu +} + +show_menu init +exit 1 +