| 1 | # Use-case 0003 Implementation - Deposit virtual funds
|
|---|
| 2 |
|
|---|
| 3 | **Initiating actor:** Trader
|
|---|
| 4 |
|
|---|
| 5 | **Other actors:** —
|
|---|
| 6 |
|
|---|
| 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:
|
|---|
| 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.
|
|---|
| 63 | 6. **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 | 
|
|---|
| 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 | 
|
|---|
| 84 |
|
|---|
| 85 | ## Verification
|
|---|
| 86 |
|
|---|
| 87 | Right after the deposit the Trader chooses `[1] View balance` (function
|
|---|
| 88 | `ShowBalance`), which runs (`$1` = user id):
|
|---|
| 89 |
|
|---|
| 90 | ```sql
|
|---|
| 91 | SELECT available_balance, invested_balance FROM users WHERE id = $1
|
|---|
| 92 | ```
|
|---|
| 93 |
|
|---|
| 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.
|
|---|