Enhance Task Manager API with root endpoint and validation
This commit is contained in:
parent
3be833dc43
commit
97f1aebefd
10
README.md
10
README.md
@ -9,6 +9,8 @@ A RESTful API for managing tasks built with FastAPI and SQLite.
|
|||||||
- Task pagination
|
- Task pagination
|
||||||
- Automatic API documentation
|
- Automatic API documentation
|
||||||
- Health check endpoint
|
- Health check endpoint
|
||||||
|
- Root endpoint with API information
|
||||||
|
- Task status and priority validation using Enums
|
||||||
|
|
||||||
## Project Structure
|
## Project Structure
|
||||||
|
|
||||||
@ -90,6 +92,10 @@ FastAPI generates interactive API documentation:
|
|||||||
|
|
||||||
## API Endpoints
|
## API Endpoints
|
||||||
|
|
||||||
|
### Root Endpoint
|
||||||
|
|
||||||
|
- `GET /` - Returns information about the API including the title, documentation links, and health check endpoint
|
||||||
|
|
||||||
### Health Check
|
### Health Check
|
||||||
|
|
||||||
- `GET /health` - Check if the API is running
|
- `GET /health` - Check if the API is running
|
||||||
@ -108,8 +114,8 @@ FastAPI generates interactive API documentation:
|
|||||||
{
|
{
|
||||||
"title": "string",
|
"title": "string",
|
||||||
"description": "string",
|
"description": "string",
|
||||||
"status": "string",
|
"status": "string", // One of: "pending", "in_progress", "completed", "cancelled"
|
||||||
"priority": "string",
|
"priority": "string", // One of: "low", "medium", "high", "urgent"
|
||||||
"due_date": "datetime",
|
"due_date": "datetime",
|
||||||
"completed": false
|
"completed": false
|
||||||
}
|
}
|
||||||
|
@ -1 +1,5 @@
|
|||||||
# Import all the models to ensure they are registered with SQLAlchemy
|
# Import all the models to ensure they are registered with SQLAlchemy
|
||||||
|
from app.database.base_class import Base # noqa
|
||||||
|
from app.models.task import Task # noqa
|
||||||
|
|
||||||
|
# Add new models here as they are created
|
||||||
|
@ -1,14 +1,31 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class TaskStatus(str, Enum):
|
||||||
|
"""Valid status values for tasks."""
|
||||||
|
PENDING = "pending"
|
||||||
|
IN_PROGRESS = "in_progress"
|
||||||
|
COMPLETED = "completed"
|
||||||
|
CANCELLED = "cancelled"
|
||||||
|
|
||||||
|
|
||||||
|
class TaskPriority(str, Enum):
|
||||||
|
"""Valid priority values for tasks."""
|
||||||
|
LOW = "low"
|
||||||
|
MEDIUM = "medium"
|
||||||
|
HIGH = "high"
|
||||||
|
URGENT = "urgent"
|
||||||
|
|
||||||
|
|
||||||
class TaskBase(BaseModel):
|
class TaskBase(BaseModel):
|
||||||
"""Base Task schema with shared attributes."""
|
"""Base Task schema with shared attributes."""
|
||||||
title: str
|
title: str
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
status: Optional[str] = "pending"
|
status: TaskStatus = TaskStatus.PENDING
|
||||||
priority: Optional[str] = "medium"
|
priority: TaskPriority = TaskPriority.MEDIUM
|
||||||
due_date: Optional[datetime] = None
|
due_date: Optional[datetime] = None
|
||||||
completed: Optional[bool] = False
|
completed: Optional[bool] = False
|
||||||
|
|
||||||
@ -22,8 +39,8 @@ class TaskUpdate(BaseModel):
|
|||||||
"""Schema for updating an existing task."""
|
"""Schema for updating an existing task."""
|
||||||
title: Optional[str] = None
|
title: Optional[str] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
status: Optional[str] = None
|
status: Optional[TaskStatus] = None
|
||||||
priority: Optional[str] = None
|
priority: Optional[TaskPriority] = None
|
||||||
due_date: Optional[datetime] = None
|
due_date: Optional[datetime] = None
|
||||||
completed: Optional[bool] = None
|
completed: Optional[bool] = None
|
||||||
|
|
||||||
|
14
main.py
14
main.py
@ -1,6 +1,7 @@
|
|||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
from app.api.v1.router import api_router
|
from app.api.v1.router import api_router
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
@ -22,8 +23,21 @@ app.add_middleware(
|
|||||||
app.include_router(api_router)
|
app.include_router(api_router)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/", tags=["root"])
|
||||||
|
async def root():
|
||||||
|
"""Root endpoint returning information about the API."""
|
||||||
|
return JSONResponse(
|
||||||
|
content={
|
||||||
|
"title": settings.PROJECT_NAME,
|
||||||
|
"documentation": "/docs",
|
||||||
|
"health_check": "/health"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health", tags=["health"])
|
@app.get("/health", tags=["health"])
|
||||||
async def health():
|
async def health():
|
||||||
|
"""Health check endpoint."""
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user