-
The filtering logic is implemented in the TaskFilter class located in the App\Http\Filters namespace. This class extends the AbstractFilter class and defines specific filter methods for each parameter (title, description, status, priority, created_at, completed_at).
-
These methods manipulate the query builder instance to apply the corresponding filters.
POST /tasks?title=query&status=todo&priority=desc&created_at=asc
[
{
"id": 1,
"title": "Sample Task",
"description": "Task Description",
"status": "todo",
"priority": 3,
"created_at": "2023-11-08T12:00:00Z",
"updated_at": "2023-11-08T13:00:00Z",
"completed_at": null,
"user_id": 1,
"parent_id": null
}
]
The API allows creating new tasks by sending a POST request to the /tasks/store endpoint. Include the task details such as title, description, status, priority, user_id, and optionally, parent_id for subtasks. Validation rules ensure the request contains valid data.
-
The CreateTaskRequest class in the App\Http\Requests namespace defines the validation rules for creating a task. It ensures the request contains necessary fields such as title, description, user_id, status, and priority.
-
The TaskController class in the App\Http\Controllers namespace handles the creation logic. When a valid request is received, it creates a TaskDTO object, passing the received data, and calls the createTask method of the TaskService class.
POST /tasks/store
Content-Type: application/json
{
"title": "New Task",
"description": "Task Description",
"status": "todo",
"priority": 2,
"user_id": 1,
"parent_id": null
}
{
"id": 2,
"title": "New Task",
"description": "Task Description",
"status": "todo",
"priority": 2,
"created_at": "2023-11-08T14:00:00Z",
"updated_at": null,
"completed_at": null,
"user_id": 1,
"parent_id": null
}
Existing tasks can be updated using a PUT or PATCH request to the /tasks/{id} endpoint. Provide the id of the task to be updated and the updated task details. Only the task owner can edit the task.
-
The UpdateTaskRequest class in the App\Http\Requests namespace defines the validation rules for updating a task. It ensures the request contains necessary fields such as title, description, user_id, status, and priority.
-
The TaskController class in the App\Http\Controllers namespace handles the update logic. When a valid request is received, it creates a TaskDTO object, passing the received data, and calls the updateTask method of the TaskService class.
PUT /tasks/2
Content-Type: application/json
{
"title": "Updated Task",
"description": "Updated Description",
"status": "done",
"priority": 1,
"user_id": 1
}
{
"id": 2,
"title": "Updated Task",
"description": "Updated Description",
"status": "done",
"priority": 1,
"created_at": "2023-11-08T14:00:00Z",
"updated_at": "2023-11-08T15:00:00Z",
"completed_at": "2023-11-08T15:00:00Z",
"user_id": 1,
"parent_task_id": null
}
A task can be marked as done by sending a PUT or PATCH request to the /tasks/{id}/done endpoint. The task status will be updated to done, and the completed_at timestamp will be set to the current time. Only tasks without uncompleted subtasks can be marked as done.
- The TaskService class contains the markTaskAsDone method. This method checks if the task has uncompleted subtasks before marking it as done. If the task has uncompleted subtasks, an exception is thrown.
- The TaskController class in the App\Http\Controllers namespace handles the marking as done logic. When a valid request is received, it calls the markTaskAsDone method of the TaskService class.
PUT /tasks/2/done
{
"id": 2,
"title": "Updated Task",
"description": "Updated Description",
"status": "done",
"priority": 1,
"created_at": "2023-11-08T14:00:00Z",
"updated_at": "2023-11-08T15:00:00Z",
"completed_at": "2023-11-08T15:00:00Z",
"user_id": 1,
"parent_task_id": null
}
A task can be deleted by sending a DELETE request to the /tasks/{id} endpoint. Only tasks that are not done can be deleted. If the task is done, an exception is thrown.
- The TaskService class contains the deleteTask method. This method checks if the task is done before allowing deletion. If the task is done, an exception is thrown
- The TaskController class in the App\Http\Controllers namespace handles the deletion logic. When a valid request is received, it calls the deleteTask method of the TaskService class.
DELETE /tasks/2
Content-Type: application/json
{
"user_id": 1
}
{
"message": "Task deleted successfully."
}
Get Tasks
POSt /api/tasks?status=todo&priority=3&title=example&sort=priority desc,createdAt asc
Create Task
POST /api/tasks/store
Edit Task
PUT /api/tasks/{taskId}
Mark Task as Completed
PUT /api/tasks/{taskId}/done
Delete Task
DELETE /api/tasks/{taskId}
composer require laravel/ui
composer require laravel/sanctum
Clone the repository:
git clone https://github.com/nazarskrinskyi/todo_list_api
Navigate to the project directory:
cd todo-list-api
Install dependencies:
composer install
npm install
Build the Docker containers:
./vendor/bin/sail build
Run docker:
./vendor/bin/sail up -d
./vendor/bin/sail bash
Run migrations:
php artisan migrate
Seed the database:
php artisan db:seed
Start the development server:
npm run dev
The API will be accessible at http://localhost.