Nest JS -Clean code using Hexagonal Architecture
But I have translated the TypeScript code to the Python - this should ease the pain of understanding Hexagonal Architecture, while reading this article.
The project is a dummy Ticket system:
- You can create a Ticket with description and priority
- And you can retrieve all tickets
We are not using real database and persistence. The Repository here is just plain InMemory.
The project has 2 main dependencies:
You can read it from original author:
The Pattern: Ports and Adapters
- Create virtualenv:
virtualenv venv
source venv/bin/activate
- Install requirements:
pip install -r requirements.txt
- Run the application:
From the project directory:
export FLASK_APP=src.adapters.api.application
export FLASK_ENV=development
flask run
List the tickets:
http://127.0.0.1:5000/tickets
[
{
"created_at": "Sun, 25 Sep 2022 20:00:38 GMT",
"description": "awesome",
"id_": "1722d89e-6146-4ca5-bd7f-33c25e14f2ec",
"priority": 1,
"status": "OPEN",
"updated_at": "Sun, 25 Sep 2022 20:00:38 GMT"
},
{
"created_at": "Sun, 25 Sep 2022 20:00:40 GMT",
"description": "awesome",
"id_": "db571e71-d976-4d57-81bb-40c953d4440c",
"priority": 1,
"status": "OPEN",
"updated_at": "Sun, 25 Sep 2022 20:00:40 GMT"
},
{
"created_at": "Sun, 25 Sep 2022 20:00:45 GMT",
"description": "awesome",
"id_": "427d46ca-2b7b-40b8-8390-56552a4d16f2",
"priority": 1,
"status": "OPEN",
"updated_at": "Sun, 25 Sep 2022 20:00:45 GMT"
}
]
Create the ticket:
cURL example:
curl -X POST --location "http://127.0.0.1:5000/tickets" \
-H "Content-Type: application/json" \
-d "{
\"description\": \"awesome\",
\"priority\": 1
}"
Response:
{
"created_at": "Sun, 25 Sep 2022 20:00:45 GMT",
"description": "awesome",
"id_": "427d46ca-2b7b-40b8-8390-56552a4d16f2",
"priority": 1,
"status": "OPEN",
"updated_at": "Sun, 25 Sep 2022 20:00:45 GMT"
}
Currently covered 100%
pytest -svv tests --cov=src --cov-report=html