Skip to content

Robust Parking Management REST API with Django 5.2 & DRF. Features Clean Architecture (Service Layer), 96% Test Coverage, RQL Filtering, and Dockerized deployment.

License

Notifications You must be signed in to change notification settings

CFBruna/parking_service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

34 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Parking Service API

Python Django Django REST Framework PostgreSQL Docker Code style: ruff Test Coverage CI

Project Status: βœ… Version 1.0 (Complete) βœ…

A robust RESTful API for parking service management, built with Django and Django REST Framework. The project is fully containerized with Docker and follows best development practices, including automated tests with high coverage, code linting, and formatting.

πŸš€ Features

Core Functionality

  • Customer Management (/api/v1/customers): Complete CRUD operations for customer registration
  • Vehicle Management (/api/v1/vehicles): CRUD operations for vehicles, associating them with customers
  • Vehicle Types (/api/v1/vehicles/type): Management of vehicle categories (e.g., Car, Motorcycle)
  • Parking Control:
    • Parking Spots (/api/v1/parking/spots): Management of parking spots
    • Parking Records (/api/v1/parking/records): System for registering vehicle entry and exit, with automatic update of spot occupancy status
  • Authentication: JWT-based authentication system to protect API endpoints
  • Optimized Admin: Customized administration interface with intelligent vehicle data auto-fill functionality to streamline workflow

API Documentation

  • Swagger UI: Interactive API documentation at /api/v1/docs/
  • ReDoc: Alternative API documentation at /api/v1/redoc/
  • OpenAPI Schema: Available at /api/v1/schema/

πŸ—οΈ Architecture Decisions

Vehicle Data Generation

A common requirement in parking systems is the automatic filling of vehicle information (brand, model, color) from the license plate. However, there are no public, free, and legal APIs in Brazil for this purpose. Solutions based on web scraping of government portals were discarded as they are unstable, violate terms of service, and present risks related to LGPD (Brazil's General Data Protection Law).

As a pragmatic and professional solution, a POST endpoint (/api/v1/vehicles/get-by-plate/) was implemented that acts as a simulated data provider:

  1. When a new license plate is provided, the system uses the Faker library to generate fictional data for brand, model, and color
  2. This data is saved in the database
  3. In future queries for the same plate, the previously saved consistent data is returned

This approach demonstrates the ability to work around real-world limitations, ensuring a smooth user experience and allowing the system to function completely and independently, without depending on external services.

Service Layer Architecture

With the goal of following software design best practices, such as the Single Responsibility Principle (SRP), the business logic for vehicle querying and creation was extracted from the API layer (views.py) and isolated in a dedicated service layer (services.py). This refactoring made the code cleaner, more reusable, and much easier to test, demonstrating a commitment to long-term project maintainability.

πŸ› οΈ Tech Stack

The project was built with a focus on quality, maintainability, and modern development practices.

Backend & Database

  • Framework: Django 5.2, Django REST Framework 3.16
  • Database: PostgreSQL 16
  • Authentication: djangorestframework-simplejwt for JWT tokens

Development Tools

  • Containerization: Docker, Docker Compose
  • Dependency Management: pip and pip-tools (pip-compile)
  • Testing: pytest, pytest-django, pytest-cov (with 96% test coverage)
  • Code Quality: Ruff for linting and formatting, pre-commit for automated quality checks
  • API Documentation: drf-spectacular for automatic OpenAPI 3 schema generation
  • Admin Interface: django-jazzmin for enhanced admin UI
  • Query Filtering: django-rql for advanced query filtering

Additional Libraries

  • Faker: For generating realistic test data
  • WhiteNoise: For static file serving
  • Gunicorn: Production WSGI server

πŸ“‹ Prerequisites

  • Docker (version 20.10 or higher)
  • Docker Compose (version 2.0 or higher)

πŸš€ Getting Started

The project is fully containerized, so all you need is Docker and Docker Compose.

1. Clone the Repository

git clone https://github.com/CFBruna/parking_service.git
cd parking_service

2. Configure Environment Variables

Create a .env.docker file in the root directory with the following variables:

# Database
DATABASE_URL=postgresql://postgres:postgres@db:5432/parking_db

# Django
SECRET_KEY=your-secret-key-here
DEBUG=True
ALLOWED_HOSTS=127.0.0.1,localhost

# Static Files
STATIC_ROOT=/usr/src/app/staticfiles

Note: For development, you can use the default values. Adjust them as needed for production.

3. Build and Start Containers

docker-compose up --build

The --build flag is important the first time to build the Docker image.

4. Run Migrations

In a new terminal, with containers running:

docker-compose exec web python manage.py migrate

5. (Optional) Create a Superuser

To access the Django admin interface:

docker-compose exec web python manage.py createsuperuser

6. Access the Application

  • API Base URL: http://127.0.0.1:8000/api/v1/
  • Admin Interface: http://127.0.0.1:8000/admin/
  • API Documentation (Swagger): http://127.0.0.1:8000/api/v1/docs/
  • API Documentation (ReDoc): http://127.0.0.1:8000/api/v1/redoc/

πŸ§ͺ Running Tests

The automated test suite is fundamental to ensure the quality and stability of the project.

Run All Tests

With containers running:

docker-compose exec web pytest

Run Tests with Coverage Report

To see the coverage report in the terminal:

docker-compose exec web pytest --cov

Generate HTML Coverage Report

To generate a detailed HTML coverage report:

docker-compose exec web pytest --cov --cov-report=html

Open the htmlcov/index.html file in your browser to explore the report.

Current Test Coverage: 96% βœ…

πŸ“š API Endpoints

Authentication

  • POST /api/v1/authentication/token/ - Obtain JWT token
  • POST /api/v1/authentication/token/refresh/ - Refresh JWT token
  • POST /api/v1/authentication/token/verify/ - Verify JWT token

Customers

  • GET /api/v1/customers/ - List all customers
  • POST /api/v1/customers/ - Create a new customer
  • GET /api/v1/customers/{id}/ - Retrieve a customer
  • PUT /api/v1/customers/{id}/ - Update a customer
  • PATCH /api/v1/customers/{id}/ - Partially update a customer
  • DELETE /api/v1/customers/{id}/ - Delete a customer

Vehicles

  • GET /api/v1/vehicles/ - List all vehicles
  • POST /api/v1/vehicles/ - Create a new vehicle
  • GET /api/v1/vehicles/{id}/ - Retrieve a vehicle
  • PUT /api/v1/vehicles/{id}/ - Update a vehicle
  • PATCH /api/v1/vehicles/{id}/ - Partially update a vehicle
  • DELETE /api/v1/vehicles/{id}/ - Delete a vehicle
  • POST /api/v1/vehicles/get-by-plate/ - Get or create vehicle data by license plate
  • GET /api/v1/vehicles/type/ - List all vehicle types
  • POST /api/v1/vehicles/type/ - Create a new vehicle type
  • GET /api/v1/vehicles/type/{id}/ - Retrieve a vehicle type
  • PUT /api/v1/vehicles/type/{id}/ - Update a vehicle type
  • PATCH /api/v1/vehicles/type/{id}/ - Partially update a vehicle type
  • DELETE /api/v1/vehicles/type/{id}/ - Delete a vehicle type

Parking

  • GET /api/v1/parking/spots/ - List all parking spots
  • POST /api/v1/parking/spots/ - Create a new parking spot
  • GET /api/v1/parking/spots/{id}/ - Retrieve a parking spot
  • PUT /api/v1/parking/spots/{id}/ - Update a parking spot
  • PATCH /api/v1/parking/spots/{id}/ - Partially update a parking spot
  • DELETE /api/v1/parking/spots/{id}/ - Delete a parking spot
  • GET /api/v1/parking/records/ - List all parking records
  • POST /api/v1/parking/records/ - Create a new parking record (entry)
  • GET /api/v1/parking/records/{id}/ - Retrieve a parking record
  • PUT /api/v1/parking/records/{id}/ - Update a parking record
  • PATCH /api/v1/parking/records/{id}/ - Partially update a parking record (exit)
  • DELETE /api/v1/parking/records/{id}/ - Delete a parking record

Advanced Querying

The API supports RQL (Resource Query Language) for advanced filtering. For example:

GET /api/v1/vehicles/?filter=license_plate=like=ABC*
GET /api/v1/parking/records/?filter=entry_time=ge=2024-01-01T00:00:00Z

See the django-rql documentation for more details.

πŸ”’ Authentication

The API uses JWT (JSON Web Tokens) for authentication. To authenticate:

  1. Obtain a token by making a POST request to /api/v1/authentication/token/ with your credentials:

    {
      "username": "your_username",
      "password": "your_password"
    }
  2. Include the token in subsequent requests using the Authorization header:

    Authorization: Bearer <your_token>
  3. Refresh your token using /api/v1/authentication/token/refresh/ when it expires.

πŸ—οΈ Project Structure

parking_service/
β”œβ”€β”€ authentication/      # Authentication app
β”œβ”€β”€ customers/           # Customer management app
β”œβ”€β”€ vehicles/            # Vehicle management app
β”œβ”€β”€ parking/             # Parking management app
β”œβ”€β”€ parking_service/     # Main project settings
β”œβ”€β”€ static/              # Static files (images, JS)
β”œβ”€β”€ docker-compose.yml   # Docker Compose configuration
β”œβ”€β”€ Dockerfile           # Docker image definition
β”œβ”€β”€ requirements.txt     # Python dependencies
└── pytest.ini          # Pytest configuration

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“ License

This project is licensed under the terms specified in the LICENSE file.

πŸ‘€ Author

CFBruna


Made with ❀️ using Django and Django REST Framework

About

Robust Parking Management REST API with Django 5.2 & DRF. Features Clean Architecture (Service Layer), 96% Test Coverage, RQL Filtering, and Dockerized deployment.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published