Ignore:
Timestamp:
09/24/26 17:43:19 (6 days ago)
Author:
Stefan <trsunovstefan@…>
Branches:
main
Children:
0cee8ec
Parents:
a531b45
Message:

Wiki docs, phase 6 and phase 7 added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/watchlist.go

    ra531b45 ref1c1c7  
    8888}
    8989
     90// addToWatchlist lists the cryptos that are not on the watchlist yet,
     91// numbered, and adds the one the user picks.
    9092func 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.")
     93        rows, err := db.DB.Query(`
     94                SELECT c.id, c.symbol, c.name
     95                  FROM crypto c
     96                 WHERE NOT EXISTS (SELECT 1 FROM watchlist_items wi
     97                                    WHERE wi.watchlist_id = $1 AND wi.crypto_id = c.id)
     98                 ORDER BY c.symbol`, wlID)
     99        if err != nil {
     100                fmt.Println("Error:", err)
    98101                return
    99102        }
     103        type option struct{ id, symbol, name string }
     104        var list []option
     105        for rows.Next() {
     106                var o option
     107                if err := rows.Scan(&o.id, &o.symbol, &o.name); err != nil {
     108                        rows.Close()
     109                        fmt.Println("scan error:", err)
     110                        return
     111                }
     112                list = append(list, o)
     113        }
     114        rows.Close()
     115        if len(list) == 0 {
     116                fmt.Println("Every crypto is already on your watchlist.")
     117                return
     118        }
     119        fmt.Println()
     120        fmt.Printf("  %-4s  %-8s  %s\n", "#", "Symbol", "Name")
     121        fmt.Println("  ------------------------------")
     122        for i, o := range list {
     123                fmt.Printf("  %-4d  %-8s  %s\n", i+1, o.symbol, o.name)
     124        }
     125        k, err := pickNumber("Crypto # to add: ", len(list))
    100126        if err != nil {
    101                 fmt.Println("Error:", err)
     127                fmt.Println(err)
    102128                return
    103129        }
    … …  
    106132                 VALUES ($1, $2)
    107133                 ON CONFLICT (watchlist_id, crypto_id) DO NOTHING`,
    108                 wlID, cryptoID,
     134                wlID, list[k].id,
    109135        )
    110136        if err != nil {
    … …  
    112138                return
    113139        }
    114         fmt.Println("Added.")
     140        fmt.Printf("Added %s.\n", list[k].symbol)
    115141}
    116142
     143// removeFromWatchlist lists the watchlist's cryptos, numbered, and removes
     144// the one the user picks.
    117145func 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)
     146        rows, err := db.DB.Query(`
     147                SELECT c.id, c.symbol, c.name
     148                  FROM watchlist_items wi
     149                  JOIN crypto c ON c.id = wi.crypto_id
     150                 WHERE wi.watchlist_id = $1
     151                 ORDER BY c.symbol`, wlID)
    124152        if err != nil {
    125153                fmt.Println("Error:", err)
    126154                return
    127155        }
    128         n, _ := res.RowsAffected()
    129         if n == 0 {
    130                 fmt.Println("Not in watchlist.")
     156        type option struct{ id, symbol, name string }
     157        var list []option
     158        for rows.Next() {
     159                var o option
     160                if err := rows.Scan(&o.id, &o.symbol, &o.name); err != nil {
     161                        rows.Close()
     162                        fmt.Println("scan error:", err)
     163                        return
     164                }
     165                list = append(list, o)
     166        }
     167        rows.Close()
     168        if len(list) == 0 {
     169                fmt.Println("Your watchlist is empty.")
    131170                return
    132171        }
    133         fmt.Println("Removed.")
     172        fmt.Println()
     173        fmt.Printf("  %-4s  %-8s  %s\n", "#", "Symbol", "Name")
     174        fmt.Println("  ------------------------------")
     175        for i, o := range list {
     176                fmt.Printf("  %-4d  %-8s  %s\n", i+1, o.symbol, o.name)
     177        }
     178        k, err := pickNumber("Crypto # to remove: ", len(list))
     179        if err != nil {
     180                fmt.Println(err)
     181                return
     182        }
     183        if _, err := db.DB.Exec(
     184                `DELETE FROM watchlist_items WHERE watchlist_id = $1 AND crypto_id = $2`,
     185                wlID, list[k].id,
     186        ); err != nil {
     187                fmt.Println("Error:", err)
     188                return
     189        }
     190        fmt.Printf("Removed %s.\n", list[k].symbol)
    134191}
Note: See TracChangeset for help on using the changeset viewer.