Skip to content

Commit d214b8f

Browse files
committed
chore:refactoring and adding docker file
1 parent 05fab7a commit d214b8f

48 files changed

Lines changed: 6163 additions & 2466 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
{
2-
"extends": [
3-
"prettier"
2+
"env": {
3+
"node": true,
4+
"es2021": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:prettier/recommended"
9+
],
10+
"plugins": [
11+
"prettier"
12+
],
13+
"rules": {
14+
"prettier/prettier": "error",
15+
"indent": [
16+
"error",
17+
2
418
],
5-
"rules": {
6-
"indent": "error"
19+
"no-unused-vars": "warn",
20+
"no-console": "off"
721
}
8-
}
22+
}

.github/workflows/ci-cd.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
build-test-push:
11+
runs-on: ubuntu-latest
12+
services:
13+
mongo:
14+
image: mongo:6.0
15+
ports:
16+
- 27017:27017
17+
options: >-
18+
--health-cmd "mongosh --eval 'db.adminCommand('ping')'" --health-interval 10s --health-timeout 5s --health-retries 5
19+
env:
20+
MONGODB_URI: mongodb://localhost:27017/cleanarchdb
21+
JWT_SECRET: test_jwt_secret
22+
PORT: 5000
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Set up Node.js
28+
uses: actions/setup-node@v4
29+
with:
30+
node-version: 18
31+
32+
- name: Install dependencies
33+
run: yarn install
34+
35+
- name: Lint
36+
run: yarn lint
37+
38+
- name: Format
39+
run: yarn format
40+
41+
- name: Run tests
42+
run: yarn test
43+
44+
- name: Build Docker image
45+
run: docker build -t your-dockerhub-username/your-repo-name:latest .
46+
47+
- name: Log in to Docker Hub
48+
uses: docker/login-action@v3
49+
with:
50+
username: ${{ secrets.DOCKERHUB_USERNAME }}
51+
password: ${{ secrets.DOCKERHUB_TOKEN }}
52+
53+
- name: Push Docker image
54+
run: |
55+
IMAGE=your-dockerhub-username/your-repo-name
56+
docker tag $IMAGE:latest $IMAGE:${{ github.sha }}
57+
docker push $IMAGE:latest
58+
docker push $IMAGE:${{ github.sha }}

.husky/pre-push

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
yarn lint && yarn format && yarn test

.lintstagedrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"*.js": [
3+
"prettier --write",
4+
"eslint --fix"
5+
]
6+
}

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"tabWidth": 2,
5+
"trailingComma": "es5",
6+
"printWidth": 100
7+
}

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Use official Node.js LTS image
2+
FROM node:18-alpine
3+
4+
# Set working directory
5+
WORKDIR /usr/src/app
6+
7+
# Copy package.json and yarn.lock
8+
COPY package.json yarn.lock ./
9+
10+
# Install dependencies
11+
RUN yarn install --production
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Expose the port the app runs on
17+
EXPOSE 5000
18+
19+
# Set environment variables
20+
ENV NODE_ENV=production
21+
22+
# Start the app
23+
CMD ["yarn", "start"]

README.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
![Clean Architecture Diagram](public/clean-architecture.png)
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.
Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
const {
2-
createProductUseCase,
3-
findOneProductUseCase,
4-
findAllProductsUseCase,
5-
deleteProductUseCase,
6-
updateProductUseCase,
7-
rateProductUseCase
8-
} = require("./product-handlers");
2+
createProductUseCase,
3+
findOneProductUseCase,
4+
findAllProductsUseCase,
5+
deleteProductUseCase,
6+
updateProductUseCase,
7+
rateProductUseCase,
8+
} = require('./product-handlers');
99

1010
const {
11-
makeProductModelHandler,
12-
makeProductRatingModelHandler
13-
} = require("../../../enterprise-business-rules/entities");
14-
15-
const productValidation = require("../../../enterprise-business-rules/validate-models/product-validation-fcts")();
11+
makeProductModelHandler,
12+
makeProductRatingModelHandler,
13+
} = require('../../../enterprise-business-rules/entities');
1614

15+
const productValidation =
16+
require('../../../enterprise-business-rules/validate-models/product-validation-fcts')();
1717

1818
const createProductUseCaseHandler = createProductUseCase({ makeProductModelHandler });
1919
const findOneProductUseCaseHandler = findOneProductUseCase({ productValidation });
2020
const findAllProductUseCaseHandler = findAllProductsUseCase();
2121
const deleteProductUseCaseHandler = deleteProductUseCase({ productValidation });
22-
const updateProductUseCaseHandler = updateProductUseCase({ makeProductModelHandler, productValidation });
22+
const updateProductUseCaseHandler = updateProductUseCase({
23+
makeProductModelHandler,
24+
productValidation,
25+
});
2326
const rateProductUseCaseHandler = rateProductUseCase({ makeProductRatingModelHandler });
2427

2528
module.exports = {
26-
createProductUseCaseHandler,
27-
findOneProductUseCaseHandler,
28-
findAllProductUseCaseHandler,
29-
deleteProductUseCaseHandler,
30-
updateProductUseCaseHandler,
31-
rateProductUseCaseHandler
32-
}
29+
createProductUseCaseHandler,
30+
findOneProductUseCaseHandler,
31+
findAllProductUseCaseHandler,
32+
deleteProductUseCaseHandler,
33+
updateProductUseCaseHandler,
34+
rateProductUseCaseHandler,
35+
};

0 commit comments

Comments
 (0)