| 1 | = Use-case 0007 — Manage watchlist =
|
|---|
| 2 |
|
|---|
| 3 | '''Initiating actor:''' Trader
|
|---|
| 4 |
|
|---|
| 5 | '''Other actors:''' —
|
|---|
| 6 |
|
|---|
| 7 | A Trader keeps a list of crypto assets they want to monitor. Adding an asset that is already on the list is a no-op (idempotent).
|
|---|
| 8 |
|
|---|
| 9 | == Scenario ==
|
|---|
| 10 |
|
|---|
| 11 | 1. Trader chooses "Manage watchlist".
|
|---|
| 12 | 2. System ensures the Trader has a default watchlist named "Favorites":
|
|---|
| 13 |
|
|---|
| 14 | {{{
|
|---|
| 15 | SELECT id FROM project.watchlists
|
|---|
| 16 | WHERE user_id = $1
|
|---|
| 17 | ORDER BY created_at LIMIT 1;
|
|---|
| 18 |
|
|---|
| 19 | -- if no row:
|
|---|
| 20 | INSERT INTO project.watchlists (user_id, name)
|
|---|
| 21 | VALUES ($1, 'Favorites')
|
|---|
| 22 | RETURNING id;
|
|---|
| 23 | }}}
|
|---|
| 24 |
|
|---|
| 25 | 3. System shows the sub-menu: List / Add / Remove / Back.
|
|---|
| 26 |
|
|---|
| 27 | === List items ===
|
|---|
| 28 |
|
|---|
| 29 | {{{
|
|---|
| 30 | SELECT c.symbol, c.name, COALESCE(lp.price, 0)
|
|---|
| 31 | FROM project.watchlist_items wi
|
|---|
| 32 | JOIN project.crypto c ON c.id = wi.crypto_id
|
|---|
| 33 | LEFT JOIN project.markets m
|
|---|
| 34 | ON m.crypto_id = c.id AND m.quote_currency = 'USD'
|
|---|
| 35 | LEFT JOIN project.v_latest_prices lp
|
|---|
| 36 | ON lp.market_id = m.id
|
|---|
| 37 | WHERE wi.watchlist_id = $1
|
|---|
| 38 | ORDER BY c.symbol;
|
|---|
| 39 | }}}
|
|---|
| 40 |
|
|---|
| 41 | === Add a crypto ===
|
|---|
| 42 |
|
|---|
| 43 | The Trader picks the crypto by its number from the listed cryptos that are not on the watchlist yet:
|
|---|
| 44 |
|
|---|
| 45 | {{{
|
|---|
| 46 | -- 1. list, numbered, the cryptos not yet on the watchlist
|
|---|
| 47 | SELECT c.id, c.symbol, c.name
|
|---|
| 48 | FROM project.crypto c
|
|---|
| 49 | WHERE NOT EXISTS (SELECT 1 FROM project.watchlist_items wi
|
|---|
| 50 | WHERE wi.watchlist_id = $1 AND wi.crypto_id = c.id)
|
|---|
| 51 | ORDER BY c.symbol;
|
|---|
| 52 |
|
|---|
| 53 | -- 2. insert the chosen crypto ($2 = its id from the list); do nothing if it's already there
|
|---|
| 54 | INSERT INTO project.watchlist_items (watchlist_id, crypto_id)
|
|---|
| 55 | VALUES ($1, $2)
|
|---|
| 56 | ON CONFLICT (watchlist_id, crypto_id) DO NOTHING;
|
|---|
| 57 | }}}
|
|---|
| 58 |
|
|---|
| 59 | === Remove a crypto ===
|
|---|
| 60 |
|
|---|
| 61 | The Trader picks the crypto by its number from the listed cryptos on the watchlist:
|
|---|
| 62 |
|
|---|
| 63 | {{{
|
|---|
| 64 | -- 1. list, numbered, the cryptos on the watchlist
|
|---|
| 65 | SELECT c.id, c.symbol, c.name
|
|---|
| 66 | FROM project.watchlist_items wi
|
|---|
| 67 | JOIN project.crypto c ON c.id = wi.crypto_id
|
|---|
| 68 | WHERE wi.watchlist_id = $1
|
|---|
| 69 | ORDER BY c.symbol;
|
|---|
| 70 |
|
|---|
| 71 | -- 2. delete the chosen crypto ($2 = its id from the list)
|
|---|
| 72 | DELETE FROM project.watchlist_items
|
|---|
| 73 | WHERE watchlist_id = $1 AND crypto_id = $2;
|
|---|
| 74 | }}}
|
|---|
| 75 |
|
|---|
| 76 | If the entered number is not one of the listed numbers, system shows "Invalid choice, enter a number from 1 to N." and nothing is changed.
|
|---|