Ignore:
Timestamp:
09/24/26 17:43:19 (6 days ago)
Author:
Stefan <trsunovstefan@…>
Branches:
main
Children:
0cee8ec
Parents:
a531b45
Message:

Wiki docs, phase 6 and phase 7 added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/P4-Prototype/UseCase0003Implementation.md

    ra531b45 ref1c1c7  
    1 # Use-case 0003 Implementation — Deposit
     1# Use-case 0003 Implementation - Deposit virtual funds
    22
    3 **Initiating actor:** Trader. **Source file:** `server/account.go`, function `Deposit`.
     3**Initiating actor:** Trader
    44
    5 ## Scenario (implemented)
     5**Other actors:** —
    66
    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:
     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:
    1144
    1245   ```sql
    1346   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
    2056   COMMIT;
    2157   ```
    2258
    23    ![Depositing 2500 USD, then checking the balance](screenshots/uc0003_deposit.png)
     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.
    2465
    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   ![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)
    2684
    2785## Verification
    2886
    29 Right after the deposit, `[1] View balance` runs:
     87Right after the deposit the Trader chooses `[1] View balance` (function
     88`ShowBalance`), which runs (`$1` = user id):
    3089
    3190```sql
    32 SELECT available_balance, invested_balance FROM users WHERE id = $1;
     91SELECT available_balance, invested_balance FROM users WHERE id = $1
    3392```
    3493
    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.
     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 TracChangeset for help on using the changeset viewer.