Changeset ef1c1c7 for docs/P4-Prototype/UseCase0003Implementation.md
- Timestamp:
- 09/24/26 17:43:19 (6 days ago)
- Branches:
- main
- Children:
- 0cee8ec
- Parents:
- a531b45
- File:
-
- 1 edited
-
docs/P4-Prototype/UseCase0003Implementation.md (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
docs/P4-Prototype/UseCase0003Implementation.md
ra531b45 ref1c1c7 1 # Use-case 0003 Implementation — Deposit1 # Use-case 0003 Implementation - Deposit virtual funds 2 2 3 **Initiating actor:** Trader . **Source file:** `server/account.go`, function `Deposit`.3 **Initiating actor:** Trader 4 4 5 ## Scenario (implemented) 5 **Other actors:** — 6 6 7 1. **User** chooses `[2] Deposit virtual funds`. 8 2. **System** prompts for an amount in USD. 9 3. **User** enters `2500`. 10 4. **System** opens a database transaction and runs: 7 A logged-in Trader tops up their virtual cash balance in USD. This is a 8 simulation-only operation: no real money changes hands, the amount is simply added to 9 the 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 — 12 the user row and the ledger — inside a single database transaction, so either both 13 changes are stored or neither is. Non-numeric, zero or negative amounts are rejected 14 before the database is touched. 15 16 Original use-case description (P3): [UseCase0003](../P3-UseCaseModel/UseCase0003.md). 17 Implementation: [`server/account.go`](../../server/account.go), function `Deposit`; 18 the verification uses `ShowBalance` from the same file. 19 20 Precondition: the Trader is logged in ([UseCase0002](UseCase0002Implementation.md)); 21 in the run below as `alice`, who starts from the seed state (available 8250.00 USD, 22 invested 1750.00 USD). 23 24 ## Scenario 25 26 1. **Trader** chooses `[2] Deposit virtual funds` in the authenticated menu 27 (types `2`). 28 2. **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  34 35 3. **Trader** enters an amount: `500`. 36 4. **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). 39 5. **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: 11 44 12 45 ```sql 13 46 BEGIN; 14 UPDATE users 15 SET available_balance = available_balance + $1, 16 updated_at = now() 17 WHERE id = $2; 18 INSERT INTO transactions (user_id, type, amount, currency, description) 19 VALUES ($2, 'deposit', $1, 'USD', 'Virtual deposit'); 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 20 56 COMMIT; 21 57 ``` 22 58 23  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. 63 6. **System** confirms `Deposited 500.0000 USD.` and returns to the authenticated 64 menu. 24 65 25 5. **System** confirms: `Deposited 2500.0000 USD.` 66 The screenshot shows steps 3–6: the entered amount `500`, the confirmation and the 67 authenticated menu again. 68 69  70 71 The statements run on the `project` schema (the connection sets 72 `search_path=project,public`), so `users` and `transactions` mean `project.users` 73 and `project.transactions`. 74 75 ### Alternate flow 4a — invalid amount 76 77 If 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 79 Trader first entered `-50`. In the prototype the system then shows the 80 authenticated menu again and the Trader chooses `[2] Deposit virtual funds` once 81 more, which returns the scenario to step 2. 82 83  26 84 27 85 ## Verification 28 86 29 Right after the deposit, `[1] View balance` runs: 87 Right after the deposit the Trader chooses `[1] View balance` (function 88 `ShowBalance`), which runs (`$1` = user id): 30 89 31 90 ```sql 32 SELECT available_balance, invested_balance FROM users WHERE id = $1 ;91 SELECT available_balance, invested_balance FROM users WHERE id = $1 33 92 ``` 34 93 35 In the screenshot above, alice starts from the seed state (available 8250.00, 36 invested 1750.00) and ends at available **10750.00** — increased by exactly the 37 2500.00 deposited, with `invested_balance` untouched. 94 It prints `Available: 8750.0000 USD`, `Invested : 1750.0000 USD`, 95 `Total : 10500.0000 USD`. The available balance grew from the seed value 8250.00 96 by exactly the 500.00 deposited (the rejected `-50` changed nothing), and 97 `invested_balance` is untouched. 98 99  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 112 All four screenshots come from one real run of exactly these inputs.
Note:
See TracChangeset
for help on using the changeset viewer.
