|
| 1 | + |
| 2 | + |
| 3 | +# Digital Market Place API |
| 4 | + |
| 5 | +A Node.js REST API for a digital marketplace, structured according to Uncle Bob's Clean Architecture principles. This project demonstrates separation of concerns, testability, and scalability by organizing code into distinct layers: Enterprise Business Rules, Application Business Rules, Interface Adapters, and Frameworks & Drivers. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | +- [Introduction](#introduction) |
| 9 | +- [Architecture Overview](#architecture-overview) |
| 10 | +- [Features](#features) |
| 11 | +- [Getting Started](#getting-started) |
| 12 | +- [Project Structure](#project-structure) |
| 13 | +- [API Endpoints](#api-endpoints) |
| 14 | +- [Testing](#testing) |
| 15 | +- [Linting & Formatting](#linting--formatting) |
| 16 | +- [Docker & Docker Compose](#docker--docker-compose) |
| 17 | +- [CI/CD Workflow](#cicd-workflow) |
| 18 | +- [Troubleshooting](#troubleshooting) |
| 19 | +- [License](#license) |
| 20 | + |
| 21 | +## Introduction |
| 22 | +This backend API allows users to register, authenticate, and interact with products, blogs, and ratings. It is designed for maintainability and extensibility, following Clean Architecture best practices. |
| 23 | + |
| 24 | +## Architecture Overview |
| 25 | +The project is organized into the following layers: |
| 26 | + |
| 27 | +- **Enterprise Business Rules**: Core business logic and domain models (`enterprise-business-rules/`). |
| 28 | +- **Application Business Rules**: Use cases and application-specific logic (`application-business-rules/`). |
| 29 | +- **Interface Adapters**: Controllers, database access, adapters, and middlewares (`interface-adapters/`). |
| 30 | +- **Frameworks & Drivers**: Express.js, MongoDB, and other external libraries. |
| 31 | + |
| 32 | +``` |
| 33 | +enterprise-business-rules/ |
| 34 | + entities/ # Domain models (User, Product, Rating, Blog) |
| 35 | + validate-models/ # Validation logic for domain models |
| 36 | +application-business-rules/ |
| 37 | + use-cases/ # Application use cases (products, user) |
| 38 | +interface-adapters/ |
| 39 | + controllers/ # Route controllers for products, users |
| 40 | + database-access/ # DB connection and data access logic |
| 41 | + adapter/ # Adapters (e.g., request/response) |
| 42 | + middlewares/ # Auth, logging, error handling |
| 43 | +routes/ # Express route definitions |
| 44 | +public/ # Static files and HTML views |
| 45 | +``` |
| 46 | + |
| 47 | +## Features |
| 48 | +- User registration and authentication (JWT) |
| 49 | +- Product CRUD operations |
| 50 | +- Blog and rating management |
| 51 | +- Role-based access control (admin, blocked users) |
| 52 | +- Input validation and error handling |
| 53 | +- Modular, testable codebase |
| 54 | + |
| 55 | +## Getting Started |
| 56 | + |
| 57 | +### Prerequisites |
| 58 | +- Node.js (v18+ recommended) |
| 59 | +- MongoDB instance (local or cloud) |
| 60 | + |
| 61 | +### Installation |
| 62 | +1. Clone the repository: |
| 63 | + ```bash |
| 64 | + git clone <repo-url> |
| 65 | + cd Clean-code-arch-REST-API |
| 66 | + ``` |
| 67 | +2. Install dependencies: |
| 68 | + ```bash |
| 69 | + yarn install |
| 70 | + ``` |
| 71 | +3. Create a `.env` file in the root with your environment variables (see `.env.example` if available): |
| 72 | + ```env |
| 73 | + PORT=5000 |
| 74 | + MONGODB_URI=mongodb://localhost:27017/your-db |
| 75 | + JWT_SECRET=your_jwt_secret |
| 76 | + ``` |
| 77 | +4. Start the server: |
| 78 | + ```bash |
| 79 | + yarn dev |
| 80 | + # or |
| 81 | + yarn start |
| 82 | + ``` |
| 83 | + |
| 84 | +The server will run at [http://localhost:5000](http://localhost:5000). |
| 85 | + |
| 86 | +## Project Structure |
| 87 | +- `index.js` - Main entry point, sets up Express, routes, and middleware |
| 88 | +- `routes/` - Express route definitions for products, users, blogs |
| 89 | +- `interface-adapters/` - Controllers, DB access, adapters, and middleware |
| 90 | +- `application-business-rules/` - Use cases for products and users |
| 91 | +- `enterprise-business-rules/` - Domain models and validation logic |
| 92 | +- `public/` - Static HTML views (landing page, 404) |
| 93 | + |
| 94 | +## API Endpoints |
| 95 | + |
| 96 | +### Products |
| 97 | +- `POST /products/` - Create a new product |
| 98 | +- `GET /products/` - Get all products |
| 99 | +- `GET /products/:productId` - Get a product by ID |
| 100 | +- `PUT /products/:productId` - Update a product |
| 101 | +- `DELETE /products/:productId` - Delete a product |
| 102 | +- `POST /products/:productId/:userId/rating` - Rate a product |
| 103 | + |
| 104 | +### Users & Auth |
| 105 | +- `POST /users/register` - Register a new user |
| 106 | +- `POST /users/login` - User login |
| 107 | +- `GET /users/profile` - Get user profile (auth required) |
| 108 | + |
| 109 | +### Blogs |
| 110 | +- `GET /blogs/` - Get all blogs |
| 111 | +- `POST /blogs/` - Create a new blog |
| 112 | + |
| 113 | +> More endpoints and details can be found in the route files under `routes/`. |
| 114 | +
|
| 115 | +## Testing |
| 116 | + |
| 117 | +- Tests are written using [Jest](https://jestjs.io/) and [Supertest](https://github.com/visionmedia/supertest). |
| 118 | +- To run all tests: |
| 119 | + ```bash |
| 120 | + yarn test |
| 121 | + ``` |
| 122 | +- Test files are located in the `tests/` directory. |
| 123 | + |
| 124 | +## Linting & Formatting |
| 125 | + |
| 126 | +- Lint your code: |
| 127 | + ```bash |
| 128 | + yarn lint |
| 129 | + ``` |
| 130 | +- Format your code: |
| 131 | + ```bash |
| 132 | + yarn format |
| 133 | + ``` |
| 134 | +- Prettier and ESLint are enforced on pre-push via Husky and lint-staged. |
| 135 | + |
| 136 | +## Docker & Docker Compose |
| 137 | + |
| 138 | +- Build and run the app with MongoDB using Docker Compose: |
| 139 | + ```bash |
| 140 | + docker-compose up --build |
| 141 | + ``` |
| 142 | +- The app will be available at [http://localhost:5000](http://localhost:5000). |
| 143 | +- The MongoDB service runs at `mongodb://localhost:27017/cleanarchdb`. |
| 144 | +- To stop and remove containers, networks, and volumes: |
| 145 | + ```bash |
| 146 | + docker-compose down -v |
| 147 | + ``` |
| 148 | + |
| 149 | +## CI/CD Workflow |
| 150 | + |
| 151 | +- GitHub Actions workflow is set up in `.github/workflows/ci-cd.yml`. |
| 152 | +- On push to `main`, the workflow: |
| 153 | + - Installs dependencies |
| 154 | + - Lints and formats code |
| 155 | + - Runs tests |
| 156 | + - Builds a Docker image |
| 157 | + - Pushes the image to Docker Hub (update credentials and repo in workflow and GitHub secrets) |
| 158 | + |
| 159 | +## Troubleshooting |
| 160 | + |
| 161 | +- Common issues and solutions are documented in [troubleshooting.md](./troubleshooting.md). |
| 162 | +- Please add new issues and solutions as you encounter them. |
| 163 | + |
| 164 | +## License |
| 165 | + |
| 166 | +This project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details. |
0 commit comments