Skip to content

Repository files navigation

TradingSystem

Overview

TradingSystem is a modular, layered e-commerce platform implemented in Java using Spring Boot. It supports persistent data storage using ORM (JPA/Hibernate) and PostgreSQL, robust recovery, and a clean separation of application, domain, infrastructure, and persistence layers.


System Initialization & Configuration

1. Main Configuration: application.properties

  • Located at: src/main/resources/application.properties
  • Controls the active Spring profile and global settings.
  • Example:
    spring.profiles.active=prod
  • For database and ORM settings, see application-prod.properties.

2. Database Initialization: DbDataInitializer.java

  • Located at: src/main/java/com/TradingSystem/TradingSystem/Config/DbDataInitializer.java
  • Purpose: On application startup (when the prod profile is active), this component loads initial data into the system from a JSON file.
  • How it works:
    • Reads init-state.json from the resources folder.
    • Executes a sequence of commands (register users, open stores, add products, assign roles, etc.) to set up the initial state.
    • Each command is defined by an action and a list of args.
  • Customization:
    • To change the initial state, edit init-state.json.
    • To add new types of initialization actions, extend the logic in DbDataInitializer.java.

3. Initial State File: init-state.json

  • Located at: src/main/resources/init-state.json
  • Format: An array of command objects, each with an action and args.
  • Example:
    [
      { "action": "registerUser", "args": ["u1@example.com", "Password123!", "ADMIN"] },
      { "action": "openStore", "args": ["s1", "u2@example.com"] },
      { "action": "addProduct", "args": ["u2@example.com", "s1", "Bamba", "30", "20", "Delicious snack", "Snacks"] }
    ]
  • Supported actions:
    • registerUser, openStore, addProduct, appointManager, appointOwner, logout, login, sendMessage, suspendMember, resumeMember, addSettings, addProductToCart, removeProductFromCart, updateProductQuantity, markAsRead, rankStore, rankProduct, closeStore, reopenStore, removeManager, placeBid, offerBid, acceptBid, rejectBid, endAuction (see DbDataInitializer.java for full list and argument details).

Persistence & Database

  • ORM: JPA/Hibernate is used for all persistent entities (annotated with @Entity).
  • Database: PostgreSQL is the default database for both production and testing.
  • Configuration: All DB and ORM settings are in src/main/resources/application-prod.properties and application-test.properties.
  • Recovery: Data is never lost on restart; the system recovers from the database.
  • State Initialization: The system can be started with a specific state using the mechanism above.

Example Entity

@Entity
@Table(name = "stores")
public class Store {
    @Id
    private String id;
    // ... other fields ...
}

Configuration

Database (PostgreSQL)

Edit src/main/resources/application-prod.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/trading_system
spring.datasource.username=YOUR_DB_USER
spring.datasource.password=YOUR_DB_PASS
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

Profiles

  • Production: Uses application-prod.properties (default).
  • Development: Uses application-dev.properties.
  • Testing: Uses application-test.properties.

Switch profiles by editing spring.profiles.active in application.properties.


Running the Application

  1. Install PostgreSQL and create a database named trading_system.

  2. Configure your DB credentials in application-prod.properties.

  3. Edit init-state.json to define the initial state you want for your system.

  4. Build and run:

    ./mvnw spring-boot:run

    The app will start on http://localhost:8080.

  5. Verify DB connection: On startup, the app checks and logs DB connectivity.

  6. Initial Data: On first run (with prod profile), the system will initialize itself using init-state.json.


REST API

The system exposes a REST API for all major operations (user registration, login, store management, product management, purchases, etc.).

  • Example endpoint: POST /api/users/register
  • See ApplicationLayer/restControllers/ for all endpoints.

Example: System Startup State

You can initialize the system with a specific state by:

  • Editing init-state.json to include the users, stores, products, and relationships you want.
  • The system will execute these commands on startup (see above for supported actions).

Testing

Integration & Persistence Tests

  • Located in src/test/java/com/TradingSystem/TradingSystem/WhiteBoxTests/ and BlackBoxTests/.
  • Use a real PostgreSQL test database (application-test.properties).
  • Cover persistence, recovery, and all major business flows.

Example Test

@DataJpaTest
public class MemberPersistenceIntegrationTest {
    @Autowired
    private UserJpaRepository userJpaRepository;
    // ...
}

Running Tests

./mvnw test

Error Handling & Recovery

  • The system logs DB connection errors and fails gracefully if the DB is unavailable.
  • All critical operations are transactional and recoverable.

Usage Examples

Register a User (REST API)

curl -X POST http://localhost:8080/api/users/register \
  -H 'Content-Type: application/json' \
  -d '{"email": "user@example.com", "password": "StrongPass123"}'

Open a Store

curl -X POST http://localhost:8080/api/users/openStore \
  -d 'sessionToken=USER_TOKEN&storeName=MyStore'

Contributing

  • Follow the layered architecture.
  • Do not access the persistence layer directly from the domain layer.
  • Add tests for all new features, especially those involving persistence.

FAQ

  • Q: How do I change the database?
    A: Edit the relevant application-*.properties file.
  • Q: How do I reset the DB?
    A: Use spring.jpa.hibernate.ddl-auto=create-drop for dev/testing.
  • Q: How do I run with a specific startup state?
    A: Edit init-state.json and restart the application.

Contact

For questions or support, open an issue or contact the maintainers.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages