A RESTful API built with FastAPI that demonstrates core backend development concepts including user authentication, database operations, and a voting system.
This is a social media backend where users can:
- Create an account and log in
- Create, read, update, and delete posts
- Vote (like) on posts
- View posts with their vote counts
- FastAPI - Modern Python web framework
- PostgreSQL - Database
- SQLAlchemy - Database ORM (Object Relational Mapper)
- Alembic - Database migrations
- JWT - Secure authentication tokens
- Pytest - Testing framework
ORM_lesson2/
├── app/
│ ├── main.py # Main application file
│ ├── models.py # Database models (User, Post, Vote)
│ ├── schemas.py # Data validation
│ ├── database.py # Database connection
│ ├── oauth2.py # Authentication logic
│ └── routers/ # API endpoints
│ ├── auth.py # Login
│ ├── post.py # Post operations
│ ├── user.py # User operations
│ └── vote.py # Voting
├── tests/ # Test files
├── alembic/ # Database migration files
└── requirements.txt # Python dependencies
pip install -r requirements.txtCreate a PostgreSQL database and configure your .env file:
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=fastapi_learn
DATABASE_USER=postgres
DATABASE_PASSWORD=your_password
SECRET_KEY=your_secret_key
ALGORITHM=HS256
EXPIRE_IN=60alembic upgrade headuvicorn app.main:app --reloadVisit http://localhost:8000/docs to see the interactive API documentation.
POST /users- Create a new accountPOST /login- Login and get access token
GET /posts- Get all postsGET /posts/{id}- Get a specific postPOST /posts- Create a new postPUT /posts/{id}- Update your postDELETE /posts/{id}- Delete your post
POST /vote- Vote on a post (use direction: 1 to like, 0 to remove like)
GET /users/{id}- Get user information
The app uses three main tables:
Users - Stores user accounts
- id, username, email, password, created_at
Posts - Stores user posts
- id, title, content, published, rating, created_at, user_id
Votes - Stores post likes
- post_id, user_id (combination is unique)
pytestTests cover:
- User registration and authentication
- Creating, reading, updating, and deleting posts
- Voting functionality
- Authorization checks
Build and run with Docker:
docker build -t fastapi-app .
docker run -p 8000:8000 --env-file .env fastapi-appThis project demonstrates:
- Building REST APIs with FastAPI
- Database design and relationships
- User authentication with JWT tokens
- Password hashing for security
- Database migrations with Alembic
- Writing tests for APIs
- Deploying with Docker
API Documentation: Once running, visit /docs for interactive API documentation where you can test all endpoints.