source: docs/P3-UseCaseModel/UseCase0007.md@ 0cee8ec

main
Last change on this file since 0cee8ec was ef1c1c7, checked in by Stefan <trsunovstefan@…>, 6 days ago

Wiki docs, phase 6 and phase 7 added

  • Property mode set to 100644
File size: 2.2 KB
Line 
1# Use-case 0007 — Manage watchlist
2
3**Initiating actor:** Trader
4
5**Other actors:** —
6
7A 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
111. Trader chooses "Manage watchlist".
122. System ensures the Trader has a default watchlist named "Favorites":
13
14 ```sql
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 ```
243. System shows the sub-menu: List / Add / Remove / Back.
25
26### List items
27
28```sql
29SELECT c.symbol, c.name, COALESCE(lp.price, 0)
30 FROM project.watchlist_items wi
31 JOIN project.crypto c ON c.id = wi.crypto_id
32 LEFT JOIN project.markets m
33 ON m.crypto_id = c.id AND m.quote_currency = 'USD'
34 LEFT JOIN project.v_latest_prices lp
35 ON lp.market_id = m.id
36 WHERE wi.watchlist_id = $1
37 ORDER BY c.symbol;
38```
39
40### Add a crypto
41
42The Trader picks the crypto by its number from the listed cryptos that are not on the watchlist yet:
43
44```sql
45-- 1. list, numbered, the cryptos not yet on the watchlist
46SELECT c.id, c.symbol, c.name
47 FROM project.crypto c
48 WHERE NOT EXISTS (SELECT 1 FROM project.watchlist_items wi
49 WHERE wi.watchlist_id = $1 AND wi.crypto_id = c.id)
50 ORDER BY c.symbol;
51
52-- 2. insert the chosen crypto ($2 = its id from the list); do nothing if it's already there
53INSERT INTO project.watchlist_items (watchlist_id, crypto_id)
54VALUES ($1, $2)
55ON CONFLICT (watchlist_id, crypto_id) DO NOTHING;
56```
57
58### Remove a crypto
59
60The Trader picks the crypto by its number from the listed cryptos on the watchlist:
61
62```sql
63-- 1. list, numbered, the cryptos on the watchlist
64SELECT c.id, c.symbol, c.name
65 FROM project.watchlist_items wi
66 JOIN project.crypto c ON c.id = wi.crypto_id
67 WHERE wi.watchlist_id = $1
68 ORDER BY c.symbol;
69
70-- 2. delete the chosen crypto ($2 = its id from the list)
71DELETE FROM project.watchlist_items
72 WHERE watchlist_id = $1 AND crypto_id = $2;
73```
74
75If 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.
Note: See TracBrowser for help on using the repository browser.