| 1 | package main
|
|---|
| 2 |
|
|---|
| 3 | import (
|
|---|
| 4 | "database/sql"
|
|---|
| 5 | "fmt"
|
|---|
| 6 |
|
|---|
| 7 | "bp_project/server/db"
|
|---|
| 8 | )
|
|---|
| 9 |
|
|---|
| 10 | // ManageWatchlist - UC0007
|
|---|
| 11 | // Ensures the user has a default watchlist, then allows listing, adding,
|
|---|
| 12 | // removing entries.
|
|---|
| 13 | func ManageWatchlist(s *Session) {
|
|---|
| 14 | wlID, err := ensureDefaultWatchlist(s.UserID)
|
|---|
| 15 | if err != nil {
|
|---|
| 16 | fmt.Println("Error:", err)
|
|---|
| 17 | return
|
|---|
| 18 | }
|
|---|
| 19 | for {
|
|---|
| 20 | fmt.Println("\n-- Watchlist --")
|
|---|
| 21 | fmt.Println("[1] List items")
|
|---|
| 22 | fmt.Println("[2] Add crypto")
|
|---|
| 23 | fmt.Println("[3] Remove crypto")
|
|---|
| 24 | fmt.Println("[0] Back")
|
|---|
| 25 | switch prompt("> ") {
|
|---|
| 26 | case "1":
|
|---|
| 27 | listWatchlist(wlID)
|
|---|
| 28 | case "2":
|
|---|
| 29 | addToWatchlist(wlID)
|
|---|
| 30 | case "3":
|
|---|
| 31 | removeFromWatchlist(wlID)
|
|---|
| 32 | case "0":
|
|---|
| 33 | return
|
|---|
| 34 | default:
|
|---|
| 35 | fmt.Println("Unknown option.")
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | func ensureDefaultWatchlist(userID string) (string, error) {
|
|---|
| 41 | var id string
|
|---|
| 42 | err := db.DB.QueryRow(
|
|---|
| 43 | `SELECT id FROM watchlists WHERE user_id = $1 ORDER BY created_at LIMIT 1`,
|
|---|
| 44 | userID,
|
|---|
| 45 | ).Scan(&id)
|
|---|
| 46 | if err == sql.ErrNoRows {
|
|---|
| 47 | err = db.DB.QueryRow(
|
|---|
| 48 | `INSERT INTO watchlists (user_id, name) VALUES ($1, 'Favorites') RETURNING id`,
|
|---|
| 49 | userID,
|
|---|
| 50 | ).Scan(&id)
|
|---|
| 51 | return id, err
|
|---|
| 52 | }
|
|---|
| 53 | return id, err
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | func listWatchlist(wlID string) {
|
|---|
| 57 | rows, err := db.DB.Query(`
|
|---|
| 58 | SELECT c.symbol, c.name, COALESCE(lp.price, 0)
|
|---|
| 59 | FROM watchlist_items wi
|
|---|
| 60 | JOIN crypto c ON c.id = wi.crypto_id
|
|---|
| 61 | LEFT JOIN markets m ON m.crypto_id = c.id AND m.quote_currency = 'USD'
|
|---|
| 62 | LEFT JOIN v_latest_prices lp ON lp.market_id = m.id
|
|---|
| 63 | WHERE wi.watchlist_id = $1
|
|---|
| 64 | ORDER BY c.symbol`, wlID)
|
|---|
| 65 | if err != nil {
|
|---|
| 66 | fmt.Println("Error:", err)
|
|---|
| 67 | return
|
|---|
| 68 | }
|
|---|
| 69 | defer rows.Close()
|
|---|
| 70 |
|
|---|
| 71 | fmt.Println()
|
|---|
| 72 | fmt.Printf(" %-8s %-20s %15s\n", "Symbol", "Name", "Last price")
|
|---|
| 73 | fmt.Println(" --------------------------------------------------")
|
|---|
| 74 | empty := true
|
|---|
| 75 | for rows.Next() {
|
|---|
| 76 | var sym, name string
|
|---|
| 77 | var price float64
|
|---|
| 78 | if err := rows.Scan(&sym, &name, &price); err != nil {
|
|---|
| 79 | fmt.Println("scan error:", err)
|
|---|
| 80 | return
|
|---|
| 81 | }
|
|---|
| 82 | fmt.Printf(" %-8s %-20s %15.6f\n", sym, name, price)
|
|---|
| 83 | empty = false
|
|---|
| 84 | }
|
|---|
| 85 | if empty {
|
|---|
| 86 | fmt.Println(" (watchlist is empty)")
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | func addToWatchlist(wlID string) {
|
|---|
| 91 | sym := prompt("Crypto symbol to add: ")
|
|---|
| 92 | var cryptoID string
|
|---|
| 93 | err := db.DB.QueryRow(
|
|---|
| 94 | `SELECT id FROM crypto WHERE upper(symbol) = upper($1)`, sym,
|
|---|
| 95 | ).Scan(&cryptoID)
|
|---|
| 96 | if err == sql.ErrNoRows {
|
|---|
| 97 | fmt.Println("Unknown crypto symbol.")
|
|---|
| 98 | return
|
|---|
| 99 | }
|
|---|
| 100 | if err != nil {
|
|---|
| 101 | fmt.Println("Error:", err)
|
|---|
| 102 | return
|
|---|
| 103 | }
|
|---|
| 104 | _, err = db.DB.Exec(
|
|---|
| 105 | `INSERT INTO watchlist_items (watchlist_id, crypto_id)
|
|---|
| 106 | VALUES ($1, $2)
|
|---|
| 107 | ON CONFLICT (watchlist_id, crypto_id) DO NOTHING`,
|
|---|
| 108 | wlID, cryptoID,
|
|---|
| 109 | )
|
|---|
| 110 | if err != nil {
|
|---|
| 111 | fmt.Println("Error:", err)
|
|---|
| 112 | return
|
|---|
| 113 | }
|
|---|
| 114 | fmt.Println("Added.")
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | func removeFromWatchlist(wlID string) {
|
|---|
| 118 | sym := prompt("Crypto symbol to remove: ")
|
|---|
| 119 | res, err := db.DB.Exec(`
|
|---|
| 120 | DELETE FROM watchlist_items
|
|---|
| 121 | WHERE watchlist_id = $1
|
|---|
| 122 | AND crypto_id = (SELECT id FROM crypto WHERE upper(symbol) = upper($2))`,
|
|---|
| 123 | wlID, sym)
|
|---|
| 124 | if err != nil {
|
|---|
| 125 | fmt.Println("Error:", err)
|
|---|
| 126 | return
|
|---|
| 127 | }
|
|---|
| 128 | n, _ := res.RowsAffected()
|
|---|
| 129 | if n == 0 {
|
|---|
| 130 | fmt.Println("Not in watchlist.")
|
|---|
| 131 | return
|
|---|
| 132 | }
|
|---|
| 133 | fmt.Println("Removed.")
|
|---|
| 134 | }
|
|---|