How sabit303/qr-code-based-library-manager works
1. Repository shape
- This is a two-part monorepo: api/ is a Node.js/TypeScript backend, and ui/ is a Flutter client that targets mobile and other Flutter platforms.
- The architecture is centered on a REST API consumed by the Flutter app; there is no shared runtime between the two.
2. Backend entry and request pipeline
- The API starts at src/server.ts, which boots the Express app defined in src/app.ts.
- Requests flow through middleware first: request logging, authentication, role authorization, and centralized error handling.
- The API exposes route modules by domain: auth, books, students, librarians, and borrowing.
3. Layered backend design
- The backend follows a clear controller → service → repository split.
- Controllers in src/Controller adapt HTTP requests/responses.
- Services in src/Services hold business rules like login, book operations, student management, borrowing logic, and QR code generation.
- Repositories in src/Repositories isolate MySQL access behind interfaces in src/Interfaces.
4. Domain model and transport layer
- Core entities are Book, Student, Librarian, and Transaction.
- DTOs define request/response shapes for login, logged-in state, books, students, and librarians, which keeps HTTP payloads separate from persistence models.
- This separation matters for diagramming because data changes shape as it moves from client payloads to service logic to database records.
5. Authentication and authorization boundary
- Authentication is JWT-based, handled by the auth controller/service pair and enforced by authMiddleware.
- authorizeRoleMiddleware adds role-based access control, splitting student actions from librarian actions.
- Password hashing is handled in src/Helper/passHash.ts, so raw credentials never reach storage.
6. Borrowing and transaction flow
- Borrowing is its own subsystem, not just a book update.
- The borrow service and borrow repository manage loan lifecycle events and persist them as transactions.
- The transaction entity is the audit trail for active borrowings, returns, and history queries.
7. QR code subsystem
- QR code support is a cross-cutting feature rather than a separate app.
- On the backend, QRCodeService generates QR content, likely for student identification.
- On the client, scanner views use the device camera to read QR codes for student or book workflows.
- This creates an interaction loop between physical items/IDs and API-driven records.
8. Database and persistence
- The backend uses MySQL through mysql2, configured in src/config/database.ts.
- src/config/init-db.sql and src/init-database.ts indicate that schema initialization is part of the backend workflow.
- The repository layer is the only part that should touch SQL, which keeps schema concerns out of controllers and services.
9. Error handling and operational concerns
- Errors are standardized through AppError and the errorHandler middleware.
- Logging is separated into requestLogger and src/utils/logger.ts, which is important for tracing API activity.
- These concerns sit around the main request path, not inside domain logic.
10. Backend deployment and environment
- The API is container-ready with Dockerfile and docker-compose.yml.
- Runtime behavior is environment-driven via .env variables for port, database, JWT, CORS, and optional Cloudflare image credentials.
- Cloudflare Images integration appears to be an external service used for media uploads or image delivery.
11. Flutter client structure
- The Flutter app is organized by feature: screens, widgets, data, and core.
- data/services handles API calls, data/providers manages state, and data/models mirrors backend payloads.
- core contains shared constants, theming, and utility functions such as image upload or response parsing helpers.
12. Client-side user flows
- The UI splits into two role-based experiences: librarian and student.
- Librarian screens include dashboards, books, students, transactions, and scanner views.
- Student screens include dashboard, books, borrowing history, profile, and QR code display.
- This mirrors the backend RBAC model, so the diagram should show two client branches sharing the same API.
13. Client-to-server data flow
- The Flutter app authenticates against the API, stores session state locally, and attaches bearer tokens to requests.
- It consumes book, student, and borrowing endpoints through service classes, with providers bridging the network layer to the UI.
- QR scanning actions from the UI typically trigger API lookups or transaction operations on the backend.
14. Cross-platform and tooling
- The client includes Android, iOS, web, and Windows platform scaffolding, so the UI is a single codebase with multiple targets.
- A GitHub Actions workflow exists at the repo root, indicating CI automation alongside Docker-based backend deployment.
- The documentation files in api/ suggest the backend has dedicated setup, error-handling, and implementation notes that support the architecture.
Main entry points: data Data layer · Routes Routes · Services Domain svcs · librarian Librarian UI
Components 24
Backend API
Node.js/TypeScript REST backend
- Server · Bootstraps the Express application
api/src/server.ts - Express app · Wires middleware and routes
api/src/app.ts - Middleware · Logging, auth, role checks, and error handling
api/src/middlewares - Routes · Domain route modules exposed by the API
api/src/Routes - Auth ctrl · Login and session endpoints
api/src/Controller/authController.ts - Domain ctrls · HTTP adapters for books, students, librarians, and borrowing
api/src/Controller - Login svc · JWT login and session logic
api/src/Services/LoginService.ts - Domain svcs · Books, students, librarians, borrowing, and QR code rules
api/src/Services - Repositories · MySQL persistence implementations
api/src/Repositories - Interfaces · Repository abstractions used by services
api/src/Interfaces - DTOs · Request and response shapes for HTTP payloads
api/src/DTOs - Entities · Book, student, librarian, and transaction records
api/src/Entities - MySQL · Relational persistence for library data
api/src/config/database.ts - DB init · Schema bootstrap and initialization workflow
api/src/init-database.ts - Errors · AppError and centralized HTTP error responses
api/src/errors/AppError.ts - Utilities · Logging, hashing, and Cloudflare image helpers
api/src/utils
Flutter UI
Cross-platform client app
- App entry · Flutter application entry point
ui/lib/main.dart - Core · Constants, theme, and utility helpers
ui/lib/core - Data layer · Models, providers, and API service wrappers
ui/lib/data - Auth flow · Login and session entry experience
ui/lib/screens/auth/login_screen.dart - Librarian UI · Dashboard, books, students, transactions, and scanners
ui/lib/screens/librarian - Student UI · Dashboard, books, history, profile, and QR display
ui/lib/screens/student - Widgets · Reusable UI components and dialogs
ui/lib/widgets
Ops
CI and deployment automation
- CI workflow · GitHub Actions pipeline for repo checks
.github/workflows/main.yml
Connections 29
- CI workflow → Server: builds · CI covers backend and app checks
- CI workflow → App entry: builds · CI covers Flutter app checks
- Server → Express app: boots · Server starts the Express app
- Express app → Middleware: uses · Request pipeline applies cross-cutting middleware
- Express app → Routes: mounts · App registers domain route modules
- Routes → Auth ctrl: dispatches · Auth routes reach auth controller
- Routes → Domain ctrls: dispatches · Domain routes reach feature controllers
- Auth ctrl → Login svc: calls · Controller delegates auth logic
- Domain ctrls → Domain svcs: calls · HTTP layer delegates business rules
- Domain svcs → Interfaces: depends on · Services use repository contracts
- Interfaces → Repositories: implemented by · MySQL repositories fulfill interfaces
- Repositories → MySQL: queries · Persistence layer talks to MySQL
- MySQL → DB init: initialized by · Schema bootstrap prepares database
- Domain svcs → DTOs: maps · Services transform HTTP payloads and responses
- Domain svcs → Entities: persists · Domain logic works with core records
- Middleware → Errors: handles · Errors are normalized centrally
- Utilities → Login svc: supports · Password hashing supports authentication
- App entry → Core: loads · App initializes shared theme and constants
- App entry → Data layer: uses · App wires data layer and state
- Data layer → Express app: calls API · Client services consume REST endpoints
- Data layer → Auth flow: drives · Auth provider/service backs login flow
- Data layer → Librarian UI: drives · Providers and services back librarian screens
- Data layer → Student UI: drives · Providers and services back student screens
- Librarian UI → Widgets: reuses · Role screens share common widgets
- Student UI → Widgets: reuses · Role screens share common widgets
- Student UI → Core: uses · Student flows use core utilities such as QR helpers
- Librarian UI → Core: uses · Librarian flows use shared utilities
- Librarian UI → Routes: scans/queries · Librarian scanner and detail views trigger backend lookups
- Student UI → Routes: scans/queries · Student QR and history flows trigger backend requests