# phuccodee/luffysbookstore architecture

> Architecture diagram of the GitHub repository phuccodee/luffysbookstore, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/phuccodee/luffysbookstore
- Repository: https://github.com/phuccodee/luffysbookstore

## Overview

##### Main Components
- Frontend: Represent the React application’s structure. Include key elements like:
- UI/Components (BookCard, BookCover, Cart, etc.)
- Pages (Home, BookSearch, Cart, Checkout)
- Context and Hooks (CartContext, custom hooks for cart, books, checkout, etc.)
- Service Layer (for interacting with backend APIs: bookService, cartService, orderService)
- Backend: Illustrate the Spring Boot application’s layered structure:
- Controllers: e.g., BookController, CartController, CustomerController, CustomerOrderController.
- Services: e.g., BookService, CartService, CustomerService, OrderItemsService.
- Repositories: Data access objects managing persistence (BookRepository, CartRepository, etc.)
- Configuration: SecurityConfig and WebConfig that configure the behavior of the Spring Boot API.
- Utility: Any shared components such as SQL scripts for database schema and data.
- Database: MySQL as the persistence layer, with SQL scripts for schema and seed data. It’s used for storing books, customers, orders, cart items, etc.

##### Relationships and Data Flow
- The frontend sends HTTP requests (via RESTful APIs) to endpoints exposed by the backend.
- The backend then processes requests by routing them through controllers to services, which interact with the database via repositories.
- Frontend and Backend (via REST API calls).
- Backend and Database (for data persistence).
- Optionally, include external services (if any exist today or have been planned, such as external payment gateways) even if they are simulated.

##### Important Architectural Patterns and Design Principles
- MVC Pattern in the Backend: Separate the presentation (controllers), business logic (services), and data access (repositories).
- Component-Based Architecture in the Frontend: Divide the user interface into reusable components and hooks.
- RESTful API Communication: The backend exposes endpoints that the frontend consumes via the services layer.
- Clear separation of concerns: Each directory and component is responsible for a particular part of the operation (e.g., cart management, book browsing, order handling).

##### Including Technologies, Frameworks, or Libraries
- Backend Technologies: Java 21, Spring Boot, Spring Data JPA, Maven (as seen with mvnw scripts), and MySQL.
- Frontend Technologies: React.js, React Router, CSS3 (BEM methodology for styles), and JavaScript libraries utilized in hooks and context management.
- Additionally, note the use of configuration files (such as application.properties and SQL scripts) that are crucial to setting up the runtime environment.

##### Additional Notes for Precision
- Don’t shy away from decomposing the backend into its various packages (demo, book, cart, customer, customerOrder, config) to illustrate internal modularity.
- Also, highlight integration points where frontend service calls interact with backend APIs.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        FE1["UI/Components (BookCard, BookCover, Cart, etc.)"]:::frontend
        FE2["Pages (Home, BookSearch, Cart, Checkout)"]:::frontend
        FE3["Context (CartContext)"]:::frontend
        FE4["Hooks (useBooks, useCartState, useCheckoutForm)"]:::frontend
        FE5["Service Layer (bookService, cartService, orderService)"]:::frontend
    end

    %% Frontend internal relationships
    FE2 -->|"renders"| FE1
    FE2 -->|"usesContext"| FE3
    FE1 -->|"consumesHook"| FE4
    FE2 -->|"callsAPI"| FE5

    %% Backend Layer
    subgraph "Backend Layer"
        %% Controllers
        subgraph "Controllers"
            BE_C1["BookController"]:::backend
            BE_C2["CartController"]:::backend
            BE_C3["CustomerController"]:::backend
            BE_C4["CustomerOrderController"]:::backend
        end

        %% Services
        subgraph "Services"
            BE_S1["BookService"]:::backend
            BE_S2["CartService"]:::backend
            BE_S3["CustomerService"]:::backend
            BE_S4["OrderItemsService"]:::backend
            BE_S5["CustomerOrderService"]:::backend
        end

        %% Repositories
        subgraph "Repositories"
            BE_R1["BookRepository"]:::backend
            BE_R2["CartRepository"]:::backend
            BE_R3["CartItemsRepository"]:::backend
            BE_R4["CustomerRepository"]:::backend
            BE_R5["CustomerOrderRepository"]:::backend
            BE_R6["OrderItemsRepository"]:::backend
        end

        %% Configuration
        subgraph "Configuration"
            BE_CFG1["SecurityConfig"]:::config
            BE_CFG2["WebConfig"]:::config
        end

        %% SQL Scripts
        subgraph "SQL Scripts"
            BE_SQL1["Schema SQL"]:::config
            BE_SQL2["Data SQL"]:::config
        end
    end

    %% Database Layer
    subgraph "Database"
        DB["MySQL Database"]:::db
    end

    %% Frontend to Backend (REST API calls)
    FE5 -->|"RESTcall"| BE_C1
    FE5 -->|"RESTcall"| BE_C2
    FE5 -->|"RESTcall"| BE_C3
    FE5 -->|"RESTcall"| BE_C4

    %% Backend: Controllers call Services
    BE_C1 -->|"calls"| BE_S1
    BE_C2 -->|"calls"| BE_S2
    BE_C3 -->|"calls"| BE_S3
    BE_C4 -->|"calls"| BE_S5
    BE_C4 -->|"calls"| BE_S4

    %% Backend: Services call Repositories
    BE_S1 -->|"queries"| BE_R1
    BE_S2 -->|"queries"| BE_R2
    BE_S2 -->|"queries"| BE_R3
    BE_S3 -->|"queries"| BE_R4
    BE_S5 -->|"queries"| BE_R5
    BE_S4 -->|"queries"| BE_R6

    %% Backend: Configuration configures Controllers
    BE_CFG1 -->|"configures"| BE_C1
    BE_CFG1 -->|"configures"| BE_C2
    BE_CFG1 -->|"configures"| BE_C3
    BE_CFG1 -->|"configures"| BE_C4
    BE_CFG2 -->|"configures"| BE_C1
    BE_CFG2 -->|"configures"| BE_C2
    BE_CFG2 -->|"configures"| BE_C3
    BE_CFG2 -->|"configures"| BE_C4

    %% Repositories interact with Database
    BE_R1 --> DB
    BE_R2 --> DB
    BE_R3 --> DB
    BE_R4 --> DB
    BE_R5 --> DB
    BE_R6 --> DB

    %% SQL Scripts feed into Database
    BE_SQL1 --> DB
    BE_SQL2 --> DB

    %% Click Events for Frontend
    click FE1 "https://github.com/phuccodee/luffysbookstore/tree/main/frontend/bookstore/src/components"
    click FE2 "https://github.com/phuccodee/luffysbookstore/tree/main/frontend/bookstore/src/pages"
    click FE3 "https://github.com/phuccodee/luffysbookstore/blob/main/frontend/bookstore/src/context/CartContext.jsx"
    click FE4 "https://github.com/phuccodee/luffysbookstore/tree/main/frontend/bookstore/src/hooks"
    click FE5 "https://github.com/phuccodee/luffysbookstore/tree/main/frontend/bookstore/src/services"

    %% Click Events for Backend Controllers
    click BE_C1 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/book/BookController.java"
    click BE_C2 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/cart/CartController.java"
    click BE_C3 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customer/CustomerController.java"
    click BE_C4 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customerOrder/CustomerOrderController.java"

    %% Click Events for Backend Services
    click BE_S1 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/book/BookService.java"
    click BE_S2 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/cart/CartService.java"
    click BE_S3 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customer/CustomerService.java"
    click BE_S4 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customerOrder/OrderItemsService.java"
    click BE_S5 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customerOrder/CustomerOrderService.java"

    %% Click Events for Backend Repositories
    click BE_R1 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/book/BookRepository.java"
    click BE_R2 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/cart/CartRepository.java"
    click BE_R3 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/cart/CartItemsRepository.java"
    click BE_R4 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customer/CustomerRepository.java"
    click BE_R5 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customerOrder/CustomerOrderRepository.java"
    click BE_R6 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/customerOrder/OrderItemsRepository.java"

    %% Click Events for Backend Configuration
    click BE_CFG1 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/config/SecurityConfig.java"
    click BE_CFG2 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/java/bookstore/demo/config/WebConfig.java"

    %% Click Events for SQL Scripts
    click BE_SQL1 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/resources/bookstore_schema.sql"
    click BE_SQL2 "https://github.com/phuccodee/luffysbookstore/blob/main/backend/demo/src/main/resources/bookstore_data.sql"

    %% Styling
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef backend fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef config fill:#FFDEAD,stroke:#000,stroke-width:1px;
    classDef db fill:#FFCC99,stroke:#000,stroke-width:1px;
```
