source: docs/P4-Prototype/UseCase0003Implementation.md

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

Wiki docs, phase 6 and phase 7 added

  • Property mode set to 100644
File size: 4.5 KB
Line 
1# Use-case 0003 Implementation - Deposit virtual funds
2
3**Initiating actor:** Trader
4
5**Other actors:** —
6
7A logged-in Trader tops up their virtual cash balance in USD. This is a
8simulation-only operation: no real money changes hands, the amount is simply added to
9the Trader's `available_balance`. Every deposit is also recorded in the ledger
10(`transactions`) so that it appears in the transaction history
11([UseCase0006](UseCase0006Implementation.md)). The operation writes to two tables —
12the user row and the ledger — inside a single database transaction, so either both
13changes are stored or neither is. Non-numeric, zero or negative amounts are rejected
14before the database is touched.
15
16Original use-case description (P3): [UseCase0003](../P3-UseCaseModel/UseCase0003.md).
17Implementation: [`server/account.go`](../../server/account.go), function `Deposit`;
18the verification uses `ShowBalance` from the same file.
19
20Precondition: the Trader is logged in ([UseCase0002](UseCase0002Implementation.md));
21in the run below as `alice`, who starts from the seed state (available 8250.00 USD,
22invested 1750.00 USD).
23
24## Scenario
25
261. **Trader** chooses `[2] Deposit virtual funds` in the authenticated menu
27 (types `2`).
282. **System** prints `-- Deposit virtual funds --` and asks `Amount (USD):`.
29
30 The screenshot shows steps 1–2: the login as alice, the authenticated menu, the
31 choice `2` and the amount prompt waiting for input.
32
33 ![UC0003 steps 1-2: Trader chooses Deposit, system asks for the amount](screenshots/uc0003_1_2_deposit.png)
34
353. **Trader** enters an amount: `500`.
364. **System** validates the input in Go, without accessing the database: the text
37 must parse as a number (`strconv.ParseFloat`) and be greater than 0 (see
38 alternate flow 4a).
395. **System** opens one database transaction (`db.DB.Begin()`), increments the
40 balance, writes the ledger row and commits. Both statements run in this single
41 transaction; if either fails, the deferred `tx.Rollback()` undoes everything.
42 `BEGIN` and `COMMIT` are issued by Go's `Begin()` / `Commit()`; the two
43 statements are sent exactly as in the code:
44
45 ```sql
46 BEGIN;
47
48 UPDATE users
49 SET available_balance = available_balance + $1,
50 updated_at = now()
51 WHERE id = $2;
52
53 INSERT INTO transactions (user_id, type, amount, currency, description)
54 VALUES ($1, 'deposit', $2, 'USD', 'Virtual deposit');
55
56 COMMIT;
57 ```
58
59 Parameters: placeholders are numbered per statement. In the `UPDATE`, `$1` is the
60 amount (`500`) and `$2` the user id (`amt, s.UserID`); in the `INSERT` it is the
61 other way round, `$1` is the user id and `$2` the amount (`s.UserID, amt`),
62 matching the column order.
636. **System** confirms `Deposited 500.0000 USD.` and returns to the authenticated
64 menu.
65
66 The screenshot shows steps 3–6: the entered amount `500`, the confirmation and the
67 authenticated menu again.
68
69 ![UC0003 steps 3-6: amount deposited](screenshots/uc0003_3_6_deposited.png)
70
71The statements run on the `project` schema (the connection sets
72`search_path=project,public`), so `users` and `transactions` mean `project.users`
73and `project.transactions`.
74
75### Alternate flow 4a — invalid amount
76
77If the amount is not a number, or is zero or negative, the system prints
78`Invalid amount.`; no transaction is started and nothing is written. In the run the
79Trader first entered `-50`. In the prototype the system then shows the
80authenticated menu again and the Trader chooses `[2] Deposit virtual funds` once
81more, which returns the scenario to step 2.
82
83![UC0003 alternate flow 4a: invalid amount](screenshots/uc0003_4a_invalid.png)
84
85## Verification
86
87Right after the deposit the Trader chooses `[1] View balance` (function
88`ShowBalance`), which runs (`$1` = user id):
89
90```sql
91SELECT available_balance, invested_balance FROM users WHERE id = $1
92```
93
94It prints `Available: 8750.0000 USD`, `Invested : 1750.0000 USD`,
95`Total : 10500.0000 USD`. The available balance grew from the seed value 8250.00
96by exactly the 500.00 deposited (the rejected `-50` changed nothing), and
97`invested_balance` is untouched.
98
99![UC0003 verification: balance after the deposit](screenshots/uc0003_verify_balance.png)
100
101## How to reproduce
102
103```sh
104./eduberza -init
105./eduberza
106# [2] Login: alice / test123
107# [2] Deposit virtual funds: -50 -> Invalid amount.
108# [2] Deposit virtual funds: 500 -> Deposited 500.0000 USD.
109# [1] View balance -> Available: 8750.0000 USD
110```
111
112All four screenshots come from one real run of exactly these inputs.
Note: See TracBrowser for help on using the repository browser.