A RESTful API built with pure PHP and SQLite, designed to manage tasks efficiently. The API is documented using Swagger/OpenAPI and containerized with Docker for easy deployment and scalability.
- Features
- Architecture
- Prerequisites
- Installation
- Running the API with Docker
- API Documentation
- Testing the Endpoints
- Project Structure
- Troubleshooting
- Contributing
- License
- CRUD Operations: Create, Read, Update, and Delete tasks.
- Swagger Documentation: Interactive API documentation available via Swagger UI.
- Dockerized: Easily deployable using Docker and Docker Compose.
- SQLite Database: Lightweight and serverless database solution.
- Modular Architecture: Organized codebase following best practices for maintainability and scalability.
The project follows a Modular Architecture, separating concerns across different layers:
- Controllers: Handle incoming HTTP requests and delegate actions to services.
- Services: Contain business logic and interact with repositories.
- Repositories: Manage data persistence and retrieval from the SQLite database.
- Router: Directs incoming requests to the appropriate controller actions.
- Documentation: Uses Swagger/OpenAPI annotations to generate interactive API documentation.
- Configuration: Centralized configuration for database paths and other settings.
-
Docker: Ensure Docker is installed on your machine. Download Docker
-
Docker Compose: Typically comes bundled with Docker Desktop. Verify installation with:
docker-compose --version
bash git clone https://github.com/yourusername/task-manager-api.git
bash cd task-manager-api
Ensure you have Composer installed locally. If not, you can use the Docker container to install dependencies.
bash docker run --rm -v $(pwd):/app -w /app composer install
Alternatively, if Composer is installed locally:
bash composer install
The project uses Docker Compose to manage the application container.
bash docker-compose up -d --build
-d: Runs containers in detached mode. --build: Rebuilds the Docker image.
bash docker-compose ps
Expected Output:
Name Command State Ports
--------------------------------------------------------------------------------
php_sqlite_api "php -S 0.0.0.0:8000…" Up 0.0.0.0:8000->8000/tcpThe API will be accessible at: http://localhost:8000
You can test the API endpoints using cURL. Below are examples for each endpoint.
Request:
curl -X GET http://localhost:8000/tasksResponse (200 OK):
[
{
"id": 1,
"title": "Document API",
"due_date": "2025-12-31",
"description": "Add Swagger documentation",
"status": "pending",
"assigned_to": "John",
"created_at": "2024-12-30T16:30:00Z",
"updated_at": "2024-12-30T16:30:00Z"
}
]Request:
curl -X POST http://localhost:8000/tasks \
-H "Content-Type: application/json" \
-d '{
"title": "Implement Authentication",
"due_date": "2025-01-15",
"description": "Add JWT-based authentication",
"status": "pending",
"assigned_to": "Alice"
}'Response (201 Created):
{
"id": 2,
"title": "Implement Authentication",
"due_date": "2025-01-15",
"description": "Add JWT-based authentication",
"status": "pending",
"assigned_to": "Alice",
"created_at": "2024-12-30T17:00:00Z",
"updated_at": "2024-12-30T17:00:00Z"
}
Response for Invalid Data (422 Unprocessable Entity):
{
"error": "title and due_date are required"
}Request:
curl -X GET http://localhost:8000/tasks/2Response (200 OK):
{
"id": 2,
"title": "Implement Authentication",
"due_date": "2025-01-15",
"description": "Add JWT-based authentication",
"status": "pending",
"assigned_to": "Alice",
"created_at": "2024-12-30T17:00:00Z",
"updated_at": "2024-12-30T17:00:00Z"
}Response if Task Not Found (404 Not Found):
{
"error": "Task not found"
}Request:
curl -X PUT http://localhost:8000/tasks/2 \
-H "Content-Type: application/json" \
-d '{
"status": "in_progress",
"assigned_to": "Bob"
}'Response (200 OK):
{
"id": 2,
"title": "Implement Authentication",
"due_date": "2025-01-15",
"description": "Add JWT-based authentication",
"status": "in_progress",
"assigned_to": "Bob",
"created_at": "2024-12-30T17:00:00Z",
"updated_at": "2024-12-30T17:15:00Z"
}Response for Invalid Data (422 Unprocessable Entity):
{
"error": "title is required"
}Response if Task Not Found (404 Not Found):
{
"error": "Task not found"
}Request:
curl -X DELETE http://localhost:8000/tasks/2Response (204 No Content):
No body.Response if Task Not Found (404 Not Found):
{
"error": "Task not found"
}Here's an overview of the project's folder structure and key files:
task-manager-api/
├── composer.json
├── composer.lock
├── docker-compose.yml
├── Dockerfile
├── generate-docs.php
├── public/
│ ├── index.php
│ ├── openapi.json
│ └── docs/
│ ├── index.html
│ ├── swagger-ui.css
│ ├── swagger-ui-bundle.js
│ ├── swagger-ui-standalone-preset.js
│ ├── favicon-16x16.png
│ ├── favicon-32x32.png
│ └── ... (other Swagger UI assets)
├── src/
│ ├── Config/
│ │ └── config.php
│ ├── Controllers/
│ │ └── TaskController.php
│ ├── Database/
│ │ └── DB.php
│ ├── Docs/
│ │ ├── api.php
│ │ └── schemas.php
│ ├── Repositories/
│ │ ├── Contracts/
│ │ │ └── TaskRepositoryInterface.php
│ │ └── Sqlite/
│ │ └── SqliteTaskRepository.php
│ ├── Router/
│ │ └── Router.php
│ └── Services/
│ ├── Contracts/
│ │ └── TaskServiceInterface.php
│ └── TaskService.php
├── tests/
│ └── ... (unit and integration tests)
└── vendor/
└── ... (Composer dependencies)composer.json&composer.lock: Define project dependencies and autoloading configurations.Dockerfile: Builds the PHP application container, installs dependencies, and generates API documentation.docker-compose.yml: Configures and manages Docker services.generate-docs.php: Script to generateopenapi.jsonfrom OpenAPI annotations in the code.public/:index.php: Entry point for all API requests.openapi.json: Generated OpenAPI specification file.docs/: Contains Swagger UI assets for interactive API documentation.
src/:Config/: Configuration files.Controllers/: Handle HTTP requests and responses.Database/: Database connection and management.Docs/: OpenAPI annotations and schema definitions.Repositories/: Data persistence logic.Router/: Routes HTTP requests to appropriate controllers.Services/: Business logic.
tests/: Contains test cases (optional but recommended).vendor/: Composer-managed dependencies.