How kaushikrajghimire/book-management-system works
Project Type & Purpose
• This is a backend API service built with FastAPI.
• Its purpose is to provide endpoints for user registration, authentication via JWT tokens, and CRUD operations for books.
• The system uses MongoDB as its database, storing user credentials (with hashed passwords) and book records.
• Authentication (login, token generation) and data validation (using Pydantic via schemas/models) are core features indicated by both the file structure and the README description.
File Structure Analysis
• Top-Level:
• The project contains a main FastAPI application entry point (app/main.py).
• The “app” directory is subdivided into:
▪ api – Contains files for the API endpoints that handle authentication (app/api/auth.py) and book operations (app/api/books.py).
▪ core – Houses the key security and JWT authentication functions located in app/core/auth.py and app/core/security.py.
▪ db – Contains the database connection logic (app/db/database.py) which sets up the MongoDB connection.
▪ schemas – Provides Pydantic models that handle data validation for books (app/schemas/book.py) and users (app/schemas/user.py). (Note: the README mentions a “models” directory, but the file tree shows “schemas”. This is likely just a naming difference.)
• There are configuration files like app/config.py and pyproject.toml for dependency and project management.
• Configuration and Build Scripts:
• .gitignore for version control management.
• The use of uvicorn for running the server and uv dependency management is highlighted in the README.
Key Components to Highlight
a. The Client / API Consumer:
• Represents the user or application making HTTP requests (e.g., via web browsers or tools like Postman).
b. FastAPI Application:
• Entry point of the application (app/main.py).
• Routes are included from sub-modules.
c. API Layer:
• Authentication API (app/api/auth.py): Exposes endpoints such as POST /register and POST /token.
• Book CRUD API (app/api/books.py): Exposes endpoints for adding, updating, getting, and deleting books.
d. Core Security/Authentication Module:
• JWT & password handling located in app/core/auth.py and app/core/security.py.
• Functions for creating hashed passwords, verifying passwords, and generating JWT tokens are critical.
e. Database Layer:
• MongoDB access is encapsulated in app/db/database.py.
• This layer interacts with the “users_collection” and “books_collection”.
f. Data Validation:
• Pydantic models found in app/schemas (representing users and books) ensure input meets defined constraints.
Relationships & Data Flow
• Client → FastAPI Application: HTTP requests are directed to the FastAPI endpoint.
• FastAPI Application: Routes requests to the appropriate API modules.
• For user registration/login, the API calls the authentication module (core/auth.py) to handle hashing and JWT token creation.
• For any operation on books, after ensuring the user is authenticated (through JWT tokens), the API calls the book endpoint methods.
• API Modules ↔ Database Layer: Both user and book operations interact with MongoDB through the database module.
• Data validation occurs at the API layer using Pydantic models from the schemas folder, ensuring errors are caught early.
Architectural Patterns & Design Principles
• Separation of Concerns is evident:
• API endpoints, core authentication/authorization logic, and database access are separated into distinct directories/modules.
• Modular Design:
• Each directory is tasked with a specific responsibility (e.g., security in core, routes in api, database interactions in db, data validation in schemas).
• Use of JWT for secure, stateless session management.
• Emphasis on secure password handling (hashing and verification) and role-based endpoint access.