This is a FastAPI application that serves as a backend for a social networking platform. It provides endpoints for user management, friendship management, and post management.
- User registration and authentication
- Create, read, update, and delete posts
- Manage friendships between users
- Retrieve mutual friends and suggested friends
- CORS support for frontend applications
- Python 3.7 or higher
- PostgreSQL database
- FastAPI
- psycopg2
- pydantic
- python-dotenv
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install fastapi psycopg2 python-dotenv
-
Set up your PostgreSQL database and create the necessary tables. You can use the following SQL commands as a reference:
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, password VARCHAR(100) NOT NULL, bio TEXT ); CREATE TABLE posts ( id SERIAL PRIMARY KEY, user_id INT REFERENCES users(id), content TEXT NOT NULL, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE friendship ( user1_id INT REFERENCES users(id), user2_id INT REFERENCES users(id), PRIMARY KEY (user1_id, user2_id) );
-
Create a
.envfile in the root directory of the project with the following content:user=<your_db_user> password=<your_db_password> host=<your_db_host> port=<your_db_port> dbname=<your_db_name>
To run the FastAPI application, use the following command:
uvicorn api:app --reloadYou can access the API documentation at http://127.0.0.1:8000/docs.
-
User Management
POST /users/- Create a new userGET /users/- Get all users except the specified userGET /user/- Get user data by name and passwordPOST /bio/- Edit user bioGET /bio/- Get user bio
-
Post Management
POST /posts/- Create a new postGET /posts/- Get posts by user ID
-
Friendship Management
POST /friend/{user_id1}/{user_id2}- Create a friendshipDELETE /friend/{user_id1}/{user_id2}- Delete a friendshipGET /friends/{user_id}- Get all friends of a userGET /mutual-friends/{user_id1}/{user_id2}- Get mutual friends of two usersGET /suggested-friends/{user_id}- Get suggested friends for a user
This project is licensed under the MIT License - see the LICENSE file for details.