Skip to content

Latest commit

 

History

History

bank-services

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

MongoDB Banking Application

Setup

1. Prerequisites

  • Java 11 or higher
  • Apache Maven
  • MongoDB cluster
  • Temporal CLI

2. Environment Variables

Set the MongoDB connection string as an environment variable. This is likely to be mongodb://127.0.0.1:27017 for a local MongoDB cluster.

Linux/MacOS

export MONGO_CONNECTION_STRING="your_connection_string_here"

Windows (Command Prompt)

set MONGO_CONNECTION_STRING=your_connection_string_here

Windows (PowerShell)

$env:MONGO_CONNECTION_STRING="your_connection_string_here"

Compiling and Running the Application

1. Compile the Application

Use Maven to compile the code:

mvn compile

2. Start the Backend Service and GUI

mvn exec:java -Dexec.mainClass="org.mongodb.banking.Main"

This starts the REST API server on http://localhost:8480. It also launches a GUI that you can use to view the available bank accounts and control whether each of them will accept requests.


API Endpoints

Base URL

All endpoints are available at http://localhost:8480.

Create Account

Creates a new bank account.

Endpoint:

GET /api/createBank?bankName={name}&initialBalance={balance}

Example:

curl -X GET "http://localhost:8480/api/createBank?bankName=Maria&initialBalance=1000"

Response:

{
  "status": "SUCCESS",
  "message": "Bank created successfully"
}

Get Balance

Retrieve the balance of a bank account.

Endpoint:

GET /api/balance?bankName={name}

Example:

curl -X GET "http://localhost:8480/api/balance?bankName=Maria"

Response:

{
  "status": "SUCCESS",
  "balance": 1000
}

Deposit

Deposit money into a specific bank.

Endpoint:

GET /api/deposit?bankName={name}&amount={amount}&idempotencyKey={key}

Example:

curl -X GET "http://localhost:8480/api/deposit?bankName=Maria&amount=500&idempotencyKey=key123"

Response:

{
  "status": "SUCCESS",
  "transaction-id": "D123456789"
}

Withdraw

Withdraw money from a specific bank.

Endpoint:

GET /api/withdraw?bankName={name}&amount={amount}&idempotencyKey={key}

Example:

curl -X GET "http://localhost:8480/api/withdraw?bankName=Maria&amount=200&idempotencyKey=key456"

Response:

{
  "status": "SUCCESS",
  "transaction-id": "W987654321"
}

Testing the Application

  1. Start the Backend Service:

    mvn exec:java -Dexec.mainClass="org.mongodb.banking.Main"
  2. Test API Endpoints:

    • Use the provided curl commands to interact with the service.
    • Verify the expected responses.
  3. Simulate Downtime:

    • Stop a bank in the GUI (Stop button).
    • Attempt a transaction and observe that it fails due to the bank being offline.
  4. Add and Remove Banks:

    • Add new banks using the createBank API.

    • Remove a bank directly from the MongoDB database:

      use bankingdemo
      db.accounts.deleteOne({ bankName: "Maria" })
    • Verify that the UI updates to reflect the changes.


Troubleshooting

Environment Variable Not Set

If the application cannot connect to MongoDB, ensure the MONGO_CONNECTION_STRINGenvironment variable is set correctly.

Service Unavailable

If the service is unreachable, ensure the backend server is running on http://localhost:8480.

MongoDB Issues

Ensure the MongoDB instance is running and accessible from the application.