[Feature] POST /packs/buy — Purchase Packs with Coins #2
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Overview
Allow the player to spend coins from their club balance to purchase a pack from the store. This is the primary coin sink in FUT.
Prerequisites
GET /packs/store(already implemented in Phase 15)GET /clubalready returns thecoinsbalanceImplementation Steps
1. Add
PackPurchaseRequesttosrc/models/packs.rs2. Validate pack exists in
src/services/packs.rsLoad from
data/packs.json; return404 Not Foundif the pack_id is not recognised.3. Validate player has enough coins
SELECT coins FROM clubs WHERE id = 1club.coins < pack.price→ return400 Bad Request:4. Deduct coins atomically in a transaction
Use
AND coins >= ?as the atomic guard so there is no double-spend race.5. Open the pack (reuse existing pack-open logic)
Call the existing service function that picks random cards from the pack definition and adds them to the collection.
6. Return response
7. Add
pack_historymigrationCreate
migrations/004_pack_history.sql:8. Register route in
src/routes/packs.rs9. Write integration test
POST /packs/buywith validpack_id→ 200, coins deducted, items returnedPOST /packs/buyagain when balance is too low → 400insufficient_coinsAcceptance Criteria
POST /packs/buydeducts coins from club balanceinsufficient_coinson low balancepack_historytablecargo clippy -- -D warningspasses