This project is a Social Media Application for an Automated Trading System implemented using FastAPI, which includes various social media features like user authentication, post creation, follow system, and a voting system for posts. Built with SQLModel and MySQL, the application is designed to manage various CRUD operations securely and efficiently, aiming to provide a robust user experience.
- Users can register, log in, and manage their accounts.
- Secure password hashing and token-based authentication are implemented for secure access.
- Securely register and authenticate users, with JWT-based authentication.
- Only authenticated users can access certain features.
- Users can create, update, and delete their posts.
- Posts include fields like title, content, timestamps, and are linked to the user who created them.
- Users can vote on posts (upvote or downvote).
- A user cannot vote more than once on the same post.
- Constraints ensure that if a user has already upvoted a post, they cannot downvote and vice versa.
- A vote choice is required from the user before a vote is saved.
- Database models use SQLModel, with relationships and constraints set up between models.
- Composite keys and foreign key constraints ensure data integrity for related entities.
- Alembic is used for database migrations.
- Supports pagination for efficient data retrieval using offset/limit and cursors.
- Sensitive information (e.g., database URL, JWT secrets) is managed using environment variables for security.
- FastAPI: For building the backend APIs.
- SQLModel: ORM to define and interact with the database.
- MySQL: Primary database for the application.
- Pydantic: Data validation and parsing.
- JWT & OAUTH2: Token Creation and Authentication.
- Alembic: Database migrations.
- Postman: API Endpoint testing.
- Python 3.9+
- MySQL database setup
- Install dependencies listed in
requirements.txt
Create a .env file in the root directory with the following variables:
host=host_name
user=root_name
database=database_name
password=your_password
secret_key=your_secret_key
algorithm=HS256
access_token_expire_minutes=30-
Clone the Repository
git clone https://github.com/Josue-Castellanos/FastAPI-Trading_System.git cd FastAPI-Trading_System -
Create and activate a virtual environment
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Run database migrations with Alembic:
alembic upgrade head
-
Start the FastAPI server:
uvicorn app.main:app --reload
-
POST /register - Register a new user. Requires
username,email, andpasswordfields. -
POST /login - Authenticate and obtain a JWT token. Requires
usernameandpassword. -
POST /posts - Create a new post (authenticated). Requires
titleandcontentfields. -
GET /posts - Retrieve all posts. Accepts optional query parameters like
limitandoffsetfor pagination. -
GET /posts/{post_id} - Retrieve a single post by its ID.
-
DELETE /posts/{post_id} - Delete a post by its ID (authenticated and only if the post belongs to the current user).
-
POST /posts/{post_id}/vote - Vote on a post (authenticated). Requires
vote_type(upvoteordownvote). Prevents duplicate or conflicting votes.
For full API details and additional endpoints, see the OpenAPI documentation at http://localhost:8000/docs.