source: docs/P4-Prototype/UseCase0007Implementation.md@ 8b447ef

main
Last change on this file since 8b447ef was b715712, checked in by Stefan <trsunovstefan@…>, 8 weeks ago

Add the server side and configuration

  • Property mode set to 100644
File size: 1.3 KB
Line 
1# Use-case 0007 Implementation — Watchlist
2
3**Initiating actor:** Trader. **Source file:** `server/watchlist.go`.
4
5## Scenario (implemented)
6
71. **User** chooses `[8] Manage watchlist`.
82. **System** ensures a default watchlist exists:
9
10 ```sql
11 SELECT id FROM watchlists WHERE user_id = $1 ORDER BY created_at LIMIT 1;
12 -- else
13 INSERT INTO watchlists (user_id, name) VALUES ($1, 'Favorites') RETURNING id;
14 ```
15
163. **System** offers the submenu: List, Add, Remove, Back.
17
18### List
19
20```sql
21SELECT c.symbol, c.name, COALESCE(lp.price, 0)
22 FROM watchlist_items wi
23 JOIN crypto c ON c.id = wi.crypto_id
24 LEFT JOIN markets m ON m.crypto_id = c.id AND m.quote_currency = 'USD'
25 LEFT JOIN v_latest_prices lp ON lp.market_id = m.id
26 WHERE wi.watchlist_id = $1
27 ORDER BY c.symbol;
28```
29
30![Adding DOGE to the watchlist, then listing it](screenshots/uc0007_watchlist.png)
31
32### Add
33
34```sql
35SELECT id FROM crypto WHERE upper(symbol) = upper($1);
36
37INSERT INTO watchlist_items (watchlist_id, crypto_id)
38VALUES ($watchlist_id, $crypto_id)
39ON CONFLICT (watchlist_id, crypto_id) DO NOTHING;
40```
41
42Re-adding the same symbol is a no-op thanks to the unique constraint + `ON CONFLICT`.
43
44### Remove
45
46```sql
47DELETE FROM watchlist_items
48 WHERE watchlist_id = $1
49 AND crypto_id = (SELECT id FROM crypto WHERE upper(symbol) = upper($2));
50```
Note: See TracBrowser for help on using the repository browser.