This project is a money transfer application designed to allow users to send money to other users, manage their accounts, and establish connections with other users for easy transaction management.
This document outlines the database structure, the relationships between tables, and constraints to ensure data integrity within the system.
The physical data model (MPD) describes the tables, columns, and relationships between the main entities in the application. The following tables are included in the database design:
This table stores information about users in the application.
- Columns:
id(Primary Key)username(Unique)email(Unique)password
This table records the details of money transactions.
- Columns:
id(Primary Key)sender_id(Foreign Key, referencesUser(id))receiver_id(Foreign Key, referencesUser(id))descriptionamount(Positive Value)
This table represents the many-to-many relationship between users, allowing them to connect with each other.
- Columns:
user_id(Foreign Key, referencesUser(id))connection_id(Foreign Key, referencesUser(id))- Primary Key: Combination of
user_idandconnection_id
The key relationships between the tables are outlined below:
-
pbm_user ↔ pbm_transaction: One-to-many relationship (1:N)
- A user can send and receive multiple transactions.
Transaction.sender_idandTransaction.receiver_idare foreign keys referencingUser.id.
-
pbm_user ↔ pbm_user_connections: Many-to-many relationship (M:N)
- A user can connect with multiple other users, and vice versa.
- The
pbm_user_Connectionstable represents this relationship, where each entry contains a pair ofuser_idandconnection_idreferencing theUser.id.
- The foreign keys ensure referential integrity between the tables:
sender_idandreceiver_idin the pbm_transaction table refer toUser.id.user_idandconnection_idin the pbm_user_connections table refer toUser.id.
- The primary key for pbm_user_connections is a composite key consisting of
user_idandconnection_id.
The following constraints have been applied to the database to maintain data integrity:
-
Unique Email:
Each user’s email address must be unique. This is enforced by a unique constraint on the
emailcolumn in the pbm_user table. -
Unique Username:
Each user’s username must be unique. This is enforced by a unique constraint on the
usernamecolumn in the pbm_user table. -
Positive Transaction Amount:
The amount of a transaction must always be positive. This constraint ensures that no transaction can have a negative or zero value in the pbm_transaction table.
-
No Self-Connections:
A user cannot connect to themselves. This is ensured by a check constraint or validation logic to prevent entries where
user_idis equal toconnection_idin the pbm_user_connections table. -
Primary Keys:
- The primary key for the pbm_user table is
id. - The primary key for the pbm_transaction table is
id. - The primary key for the pbm_user_connections table is the combination of
user_idandconnection_id.
- The primary key for the pbm_user table is
-
Foreign Keys:
- In the pbm_transaction table, both
sender_idandreceiver_idare foreign keys referring to the pbm_user table. - The pbm_user_connections table contains foreign keys,
user_idandconnection_id, both referring toUser.id.
- In the pbm_transaction table, both
To run the application locally, you need to activate the local profile. The application uses two separate configuration files: application-local.properties and application-prod.properties.
-
Activate the Local Profile
When starting the application, make sure to use the
localprofile to configure the application in development mode. This profile will create and use the H2 database. -
Encryption Key
The encryption key is used to encrypt the password of the user. This key is configured in the
application.propertiesfile.You need to configure one environment variable for the encryption key:
ENCRYPTION_KEY: The encryption key for the password encoder.
Or it's possible to change the key in the
application.propertiesfile:encryption.key=your_encryption_key -
Using Test Users
To use test users with default credentials, you need to load the necessary test data and configure the encryption key. Here's how to set it up:
- Load test data :
You need to ensure that the data-h2.sql file is loaded automatically when the application starts. This file contains predefined test data, including users with default passwords. To load the data, uncomment and configure the following line in the
application-local.propertiesfile:spring.sql.init.data-locations=classpath:/data-h2.sql- Configure the encryption key :
For testing purposes, you can use the following encryption key:
encryption.key=ThisIsASecureKeyForProtectPasswordThis key will be used to encode the passwords for test users. By default, the password for all test users is set to 123.
Now, once the application starts, the test data will be loaded, and users will be able to log in with the default password 123.
-
Start the Application
You can start the application with the
localprofile by running the following command:java -Dspring.profiles.active=local -jar PayMyBuddy.jar
To run the application in a production environment, follow the steps below:
-
Create the Database
Before starting the application, ensure that a MySQL database namedpaymybuddyis created. -
Set Environment Variables
You need to configure two environment variables on your machine to provide the database connection credentials:DB_USERNAME: The username for the database.DB_PASSWORD: The password associated with the username.
Additionally, you need to configure one environment variable for the encryption key:
ENCRYPTION_KEY: The encryption key for the password encoder.
These environment variables are required to ensure both secure database connections and proper encryption for sensitive data within the application.
Example for setting environment variables:
-
Linux/macOS:
export DB_USERNAME=your_db_username export DB_PASSWORD=your_db_password export ENCRYPTION_KEY=your_secure_encryption_key
-
Windows:
set DB_USERNAME=your_db_username set DB_PASSWORD=your_db_password set ENCRYPTION_KEY=your_secure_encryption_key
-
Start the Application
After the database is created and the environment variables are set, you can start the application. Spring Boot will automatically read these variables to establish the database connection and the encryption key and run the application properly.To start the application in production, use the following command:
java -Dspring.profiles.active=prod -jar PayMyBuddy.jar
