diff --git a/bank b/bank index 17d6439..5931b39 100755 --- a/bank +++ b/bank @@ -26,15 +26,20 @@ show_menu() { printf 'Select a function by number: 1. Create Account + 2. Deposit 3. Withdrawal 4. Transfer -5. Show All Balances -6. Show All Transactions -7. Show Transactions for Account -9. Delete Account (!) -0. Quit +5. Show All Balances +6. Show Transaction History +7. Show Transactions for Account +8. Show Transfer History + +9. Disable Account (!) +0. Enable Account + +Q. Quit ' @@ -68,11 +73,19 @@ show_menu() { echo " Show Transactions for Account" show_transactions ;; - 9) - echo " Delete Account" - delete_account + 8) + echo " Show All Gold Transfers" + show_transfers ;; - [0qQ]) + 9) + echo " Disable Account (!)" + disable_account + ;; + 0) + echo " Enable Account" + enable_account + ;; + [Qq]) echo " Quit" exit 0 ;; @@ -105,9 +118,51 @@ create_account() { hold_menu } -delete_account() { - printf "\n$(tput bold)$(tput setaf 3)This action is not yet supported!\n\n$(tput sgr0)" - # # Close/Delete Account +disable_account() { + #printf "\n$(tput bold)$(tput setaf 3)This action is not yet supported!\n\n$(tput sgr0)" + + eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE enabled = true;\"" + read -p "Enter Account ID: " ID + NAME=$(eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE id = ${ID};\"" | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" | awk -F '|' '{print $2}') + read -p "$(tput bold)$(tput setaf 3)DISABLING$(tput sgr0) account #${ID}:$(tput bold)${NAME}$(tput sgr0)... Are you sure? (y/N)" -n1 CONFIRM + case $CONFIRM in + [Yy]) + printf "\n$(tput bold)$(tput setaf 3)Disabling account...\n\n$(tput sgr0)" + eval "$dbconnect -c \"UPDATE account SET enabled = false 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 "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)" + ;; + esac + + # # Delete Account (DO NOT USE) + # 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}; + hold_menu +} + +enable_account() { + echo "DISABLED Accounts:" + eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE enabled = false;\"" + read -p "Enter Account ID: " ID + NAME=$(eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE id = ${ID};\"" | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" | awk -F '|' '{print $2}') + read -p "$(tput bold)Enabling$(tput sgr0) account #${ID}:$(tput bold)${NAME}$(tput sgr0)... Are you sure? (y/N)" -n1 CONFIRM + case $CONFIRM in + [Yy]) + printf "\n$(tput bold)$(tput setaf 3)Disabling account...\n\n$(tput sgr0)" + eval "$dbconnect -c \"UPDATE account SET enabled = true 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 "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)" + ;; + esac + + # # Delete Account (DO NOT USE) # 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}; @@ -115,7 +170,7 @@ delete_account() { } make_deposit() { - eval "$dbconnect -c \"SELECT * FROM account;\"" + eval "$dbconnect -c \"SELECT id,name FROM account WHERE enabled = true;\"" read -p "Enter Account ID: " ID read -p "Enter amount of gp to deposit: " amount tstamp=$(date +%Y-%m-%d\ %H:%M:%S) @@ -141,7 +196,7 @@ make_deposit() { } make_withdrawal() { - eval "$dbconnect -c \"SELECT * FROM account;\"" + eval "$dbconnect -c \"SELECT id,name FROM account WHERE enabled = true;\"" read -p "Enter Account ID: " ID read -p "Enter amount of gp to withdraw: " amount tstamp=$(date +%Y-%m-%d\ %H:%M:%S) @@ -167,12 +222,36 @@ make_withdrawal() { } bank_transfer() { - printf "\n$(tput bold)$(tput setaf 3)This action is not yet supported!\n\n$(tput sgr0)" + # show balances first + eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id WHERE account.enabled = true ORDER BY name;\"" + + read -p "Enter Account ID to transfer $(tput bold)FROM$(tput sgr0): " FROM + read -p "Enter Account ID to transfer $(tput bold)TO$(tput sgr0): " TO + read -p "Enter amount of gp to transfer: " amount + tstamp=$(date +%Y-%m-%d\ %H:%M:%S) + read -p "Transfer ${amount}gp from account ${FROM} to account ${TO}. Are you sure? (y/N)" -n1 CONFIRM + case $CONFIRM in + [Yy]) + printf "\n$(tput bold)Transferring...\n\n$(tput sgr0)" + eval "$dbconnect -c \"INSERT INTO transaction (account,withdrawal,timestamp) VALUES ('${FROM}','${amount}','${tstamp}');\"" + eval "$dbconnect -c \"UPDATE balance SET gold = gold - '${amount}' WHERE id = '${FROM}';\"" + eval "$dbconnect -c \"INSERT INTO transaction (account,deposit,timestamp) VALUES ('${TO}','${amount}','${tstamp}');\"" + eval "$dbconnect -c \"UPDATE balance SET gold = gold + '${amount}' WHERE id = '${TO}';\"" + eval "$dbconnect -c \"INSERT INTO transfer (from_id,to_id,gold,timestamp) VALUES ('${FROM}','${TO}','${amount}','${tstamp}');\"" + 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 balances:\n" + eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id WHERE balance.id = ${FROM} OR balance.id = ${TO};\"" + ;; + *) + printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)" + ;; + esac hold_menu } show_balance() { - eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id ORDER BY name;\"" + eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id WHERE account.enabled = true ORDER BY name;\"" hold_menu } @@ -189,6 +268,11 @@ show_transactions() { hold_menu } +show_transfers() { + eval "$dbconnect -c \"SELECT transfer.id,f.name AS from,t.name AS to,gold,timestamp FROM transfer LEFT JOIN account AS f ON f.id = transfer.from_id LEFT JOIN account AS t ON t.id = transfer.to_id;\"" + hold_menu +} + while true; do show_menu init done diff --git a/dnd_bank.psql b/dnd_bank.psql index 6c2ad7f..8bf3405 100644 --- a/dnd_bank.psql +++ b/dnd_bank.psql @@ -19,3 +19,13 @@ CREATE TABLE transaction ( FOREIGN KEY (account) REFERENCES account (id) ); +CREATE TABLE transfer ( + id SERIAL PRIMARY KEY, + from_id INT NOT NULL, + to_id INT NOT NULL, + gold NUMERIC(16,2) NOT NULL, + timestamp TIMESTAMP NOT NULL, + FOREIGN KEY (from_id) REFERENCES account (id), + FOREIGN KEY (to_id) REFERENCES account (id) +); +