This project demonstrates a simplified cash balance application using reactive Java Spring Boot and Spring Data MongoDB. It includes:
- CRUD operations with
ReactiveMongoRepository
- CRUD operations with
ReactiveMongoTemplate
- Multi-document transaction handling
- Java 17
- Spring Boot Starter Webflux 3.2.3
- Spring Boot Starter Reactive Data Mongodb 3.2.3
- Gradle 7.5.1
- Creation of a bank account with a unique
accountNum
, initializing with a $0 balance. - Storage of accounts and their balances in the "accounts" collection.
- Debit operations to increase the account balance.
- Credit operations to decrease the account balance.
- Transfer operations to move funds from one account to another.
- Transaction lifecycle:
- Operations are initially recorded in the "transactions" collection with a "PENDING" status.
- Account balances are updated.
- Transaction status is updated to "SUCCESS".
- Rollback transactions and mark as "FAILED" if:
- Insufficient balance for debit operations.
- Account number is invalid.
- Ensure a MongoDB cluster is available.
- Execute schema validation to enforce a non-negative balance:
mongosh "<MongoDB connection string>" --file setup.js
- Create an
application.properties
file in theresources
directory:
spring.data.mongodb.uri=<MongoDB connection string>
spring.data.mongodb.database=txn-demo
- Compile the application:
./gradlew clean build
- Run the application:
./gradlew bootRun
POST /account
Request Body:
{
"accountNum": "<String>",
"balance": "<Number>"
}
Example:
curl --location 'localhost:8080/account' \
--header 'Content-Type: application/json' \
--data '{
"accountNum": "111111"
}'
GET /account/{accountNum}
Example:
curl --location 'localhost:8080/account/111111'
POST /account/{accountNum}/debit
Request Body:
{
"amount": "<Number>"
}
Example:
curl --location 'localhost:8080/account/111111/debit' \
--header 'Content-Type: application/json' \
--data '{
"amount": 1000
}'
POST /account/{accountNum}/credit
Request Body:
{
"amount": "<Number>"
}
Example:
curl --location 'localhost:8080/account/111111/credit' \
--header 'Content-Type: application/json' \
--data '{
"amount": 10000
}'
POST /account/{accountNum}/transfer
Request Body:
{
"to": "<String>",
"amount": "<Number>"
}
Example:
curl --location 'localhost:8080/account/123456/transfer' \
--header 'Content-Type: application/json' \
--data '{
"to": "1111111",
"amount": 500
}'
For functional testing, use the provided Postman collection to ensure API functionality:
- Import the collection into Postman.
- Set environment variables "host" and "port" in Postman.
- Reset the database:
mongosh "<MongoDB connection string>" --file setup.js
- Use the "Run collection" feature in Postman to execute all API calls sequentially and validate test results.
For local testing, the Rapid API Client can be utilized. Make sure the application is running locally and configure the client accordingly.