# ayanahm4d/golang-bookstore-management-api architecture

> Architecture diagram of the GitHub repository ayanahm4d/golang-bookstore-management-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ayanahm4d/golang-bookstore-management-api
- Repository: https://github.com/ayanahm4d/golang-bookstore-management-api

## Overview

##### Main Components

a) Client Layer:
- External API consumers
- Supported HTTP methods (GET, POST, PUT, DELETE)

b) API Layer:
- HTTP Server (using Gorilla Mux)
- Authentication Middleware
- Route Handlers (auth_handler, book_handler)

c) Business Logic Layer:
- Authentication Service (JWT)
- Book Management Service
- User Management Service

d) Data Access Layer:
- Repository Layer (book_repository, user_repository)
- Database (MySQL)

**[Client Layer]**
- ↓
**[API Gateway/Load Balancer]**
- ↓
**[API Layer]**
- ↓
**[Business Logic Layer]**
- ↓
**[Data Access Layer]**
- ↓
**[Database Layer]**

##### Component Relationships

- HTTP requests flow from clients through the API layer
- Authentication flow through middleware
- Data flow between repositories and database
- JWT token validation path

##### Detailed Component Breakdown

API Layer (Controllers):
**/api**
- Auth Routes
- POST /register
- POST /login
- Book Routes (Protected)
- GET /books
- GET /books/{id}
- POST /books
- PUT /books/{id}
- DELETE /books/{id}

Middleware Layer:
- Authentication Middleware (JWT Validation)
- Request/Response Handling

Business Logic:
- User Management
- Book Management
- JWT Token Generation/Validation

Data Layer:
- User Repository
- Book Repository
- MySQL Database (with tables: users, books)

##### Additional Elements

- Environment Configuration (.env)
- External Dependencies:
- jwt-go
- go-sql-driver/mysql
- gorilla/mux
- godotenv
- crypto (for password hashing)

##### Special Features to Highlight

- Pagination handling
- Search and filtering capabilities
- Sort functionality
- Security measures (password hashing, JWT)

##### Technical Details to Note

Database Schema:
**[Users Table]**
- id (BIGINT)
- username (VARCHAR)
- password (VARCHAR)
- email (VARCHAR)
- created_at (TIMESTAMP)

**[Books Table]**
- id (BIGINT)
- title (VARCHAR)
- author (VARCHAR)
- price (DECIMAL)
- stock (INT)
- created_at (TIMESTAMP)
- updated_at (TIMESTAMP)

##### Implementation Notes

- Clear separation of concerns between layers
- Where configuration is loaded
- Protected routes and authentication flow
- Error handling paths
- Where middleware intercepts requests

This project follows a clean architecture pattern, so emphasize the separation of concerns and the unidirectional flow of dependencies from external layers (API) to internal layers (business logic and data access).

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        C1[External API Consumers]:::client
    end

    subgraph API_Gateway
        LB[API Gateway/Load Balancer]:::gateway
    end

    subgraph API_Layer
        Server[HTTP Server]:::api
        click Server "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/cmd/main.go"
        
        AuthHandler[Authentication Handler]:::handler
        click AuthHandler "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/internal/handlers/auth_handler.go"
        
        BookHandler[Book Management Handler]:::handler
        click BookHandler "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/internal/handlers/book_handler.go"
        
        AuthMW[Authentication Middleware]:::middleware
        click AuthMW "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/internal/middleware/auth_middleware.go"
    end

    subgraph Business_Logic
        AuthService[JWT Authentication Service]:::auth
        click AuthService "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/internal/auth/jwt.go"
        
        BookService[Book Management Service]:::service
        UserService[User Management Service]:::service
        
        Models[Domain Models]:::model
        click Models "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/tree/main/internal/models/"
    end

    subgraph Data_Access
        BookRepo[Book Repository]:::repo
        click BookRepo "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/internal/repository/book_repository.go"
        
        UserRepo[User Repository]:::repo
        click UserRepo "https://github.com/ayanAhm4d/Golang-Bookstore-Management-API/blob/main/internal/repository/user_repository.go"
    end

    subgraph Database
        DB[(MySQL Database)]:::database
    end

    C1 --> LB
    LB --> Server
    Server --> AuthMW
    AuthMW --> AuthHandler & BookHandler
    
    AuthHandler --> AuthService
    BookHandler --> BookService
    AuthHandler --> UserService
    
    AuthService --> UserRepo
    BookService --> BookRepo
    UserService --> UserRepo
    
    BookRepo --> DB
    UserRepo --> DB

    %% API Routes
    Server --> |"POST /register"|AuthHandler
    Server --> |"POST /login"|AuthHandler
    Server --> |"GET /books"|BookHandler
    Server --> |"POST /books"|BookHandler
    Server --> |"PUT /books/{id}"|BookHandler
    Server --> |"DELETE /books/{id}"|BookHandler

    %% Styling
    classDef client fill:#e6e6fa,stroke:#333,stroke-width:2px
    classDef gateway fill:#87ceeb,stroke:#333,stroke-width:2px
    classDef api fill:#add8e6,stroke:#333,stroke-width:2px
    classDef handler fill:#90ee90,stroke:#333,stroke-width:2px
    classDef middleware fill:#ffd700,stroke:#333,stroke-width:2px
    classDef service fill:#98fb98,stroke:#333,stroke-width:2px
    classDef auth fill:#f0e68c,stroke:#333,stroke-width:2px
    classDef repo fill:#deb887,stroke:#333,stroke-width:2px
    classDef database fill:#d3d3d3,stroke:#333,stroke-width:2px
    classDef model fill:#ffa07a,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1[Client]:::client
        L2[Gateway]:::gateway
        L3[API]:::api
        L4[Handler]:::handler
        L5[Middleware]:::middleware
        L6[Service]:::service
        L7[Auth]:::auth
        L8[Repository]:::repo
        L9[Database]:::database
        L10[Model]:::model
    end
```
