A FastAPI-based social media backend API built according to the Product Requirements Document. This API allows users to create, retrieve, update, and delete posts; add comments; and like/unlike posts.
- Posts Management: Create, read, update, and delete posts
- Comments System: Add, read, update, and delete comments on posts
- Likes System: Like and unlike posts
- SQLite Database: Persistent storage with automatic initialization
- OpenAPI Documentation: Auto-generated Swagger UI and ReDoc
- CORS Support: Enabled for all origins for development
python/
├── main.py # FastAPI application entry point
├── models.py # SQLModel data models and schemas
├── database.py # Database configuration and session management
├── api.py # API route handlers and business logic
├── sns_api.db # SQLite database file (created automatically)
├── test_api.sh # Comprehensive API test script
├── .venv/ # Python virtual environment
└── README.md # This file
-
Virtual Environment: Already set up with
uvsource .venv/bin/activate -
Dependencies: Installed via
uv- FastAPI
- SQLModel (includes SQLAlchemy and Pydantic)
- Uvicorn (ASGI server)
# Activate virtual environment
source .venv/bin/activate
# Start the server
uvicorn main:app --host 0.0.0.0 --port 8000
# Or with auto-reload for development
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadThe API will be available at: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
GET /api/posts- List all postsPOST /api/posts- Create a new postGET /api/posts/{post_id}- Get a specific postPATCH /api/posts/{post_id}- Update a postDELETE /api/posts/{post_id}- Delete a post
GET /api/posts/{post_id}/comments- List comments for a postPOST /api/posts/{post_id}/comments- Create a commentGET /api/posts/{post_id}/comments/{comment_id}- Get a specific commentPATCH /api/posts/{post_id}/comments/{comment_id}- Update a commentDELETE /api/posts/{post_id}/comments/{comment_id}- Delete a comment
POST /api/posts/{post_id}/likes- Like a postDELETE /api/posts/{post_id}/likes- Unlike a post
curl -X POST http://localhost:8000/api/posts \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"content": "Just had an amazing hike in the mountains! #outdoorlife"
}'curl -X POST http://localhost:8000/api/posts/{post_id}/comments \
-H "Content-Type: application/json" \
-d '{
"username": "jane_smith",
"content": "Great photo! Where was this taken?"
}'curl -X POST http://localhost:8000/api/posts/{post_id}/likes \
-H "Content-Type: application/json" \
-d '{
"username": "mike_wilson"
}'Run the comprehensive test script:
./test_api.shThis script tests all major API functionality including creating posts, comments, and likes.
- Type: SQLite
- File:
sns_api.db - Initialization: Automatic on application startup
- Schema: Defined using SQLModel with proper relationships and constraints
- Framework: FastAPI with SQLModel
- Database ORM: SQLAlchemy (via SQLModel)
- Validation: Pydantic (via SQLModel)
- ASGI Server: Uvicorn
- CORS: Enabled for all origins
- Documentation: Auto-generated OpenAPI 3.1.0 spec
This implementation strictly follows the provided OpenAPI specification and Product Requirements Document:
- ✅ All endpoints implemented as specified
- ✅ Proper HTTP status codes (200, 201, 204, 400, 404, 500)
- ✅ JSON request/response format
- ✅ Data validation and error handling
- ✅ SQLite database with automatic initialization
- ✅ CORS enabled for all origins
- ✅ Port 8000 as specified
- ✅ Swagger UI and OpenAPI documentation
- ✅ No authentication (as specified in requirements)