This is a REST API for a blogging platform with user authentication and role-based access control. The platform allows users to register, log in, create posts, and comment on posts. The API is built using Flask, SQLAlchemy, and PostgreSQL.
- User Registration & Login: Users can register and log in to access the platform.
- Role-Based Access Control: Different roles ("admin", "author", "reader") with varying levels of access.
- Post Management: Authenticated users with appropriate roles can create and manage blog posts.
- Commenting System: Users can comment on blog posts.
- Pagination: Supports pagination for retrieving posts.
- Python 3.x
- PostgreSQL
- pip (Python package installer)
-
Clone the repository:
git clone https://github.com/yourusername/blogging-platform-api.git cd blogging-platform-api
-
Install dependencies:
pip install -r requirements.txt
-
Set up PostgreSQL:
Create a PostgreSQL database named
blog_platform
.CREATE DATABASE blog_platform;
-
Configure the Database:
Update the database connection string in
config.py
:SQLALCHEMY_DATABASE_URI = 'postgresql://username:password@localhost/blog_platform'
-
Run Database Migrations:
Initialize the database tables:
flask db upgrade
-
Run the application:
python app.py
The API will be available at
http://127.0.0.1:5000/
.
-
Register:
POST /api/register
- Requires
username
,email
, andpassword
. - Hashes the password before storing it in the database.
- Requires
-
Login:
POST /api/login
- Requires
username
andpassword
. - Returns a JWT token upon successful authentication.
- Requires
-
Endpoint:
POST /api/posts
-
Description: Allows an authenticated user with the role of "author" or "admin" to create a post.
-
Headers: Requires JWT token for authentication.
-
Request Body:
{ "title": "My First Post", "content": "This is the content of my first post." }
- Endpoint:
GET /api/posts
- Description: Retrieves a list of all posts, optionally filtered by author using a query parameter (
?author=username
). - Pagination: Implemented with a page size of 2 posts per page.
-
Endpoint:
POST /api/posts/{post_id}/comments
-
Description: Allows an authenticated user to comment on a post.
-
Headers: Requires JWT token for authentication.
-
Request Body:
{ "content": "Great post!" }
You can use Postman or curl to test the API endpoints.
-
User Registration:
curl -X POST \ http://127.0.0.1:5000/api/register \ -H 'Content-Type: application/json' \ -d '{"username": "johndoe", "email": "john@example.com", "password": "securepassword"}'
-
User Login:
curl -X POST \ http://127.0.0.1:5000/api/login \ -H 'Content-Type: application/json' \ -d '{"username": "johndoe", "password": "securepassword"}'
-
Create a Post:
curl -X POST \ http://127.0.0.1:5000/api/posts \ -H 'Authorization: Bearer YOUR_JWT_TOKEN' \ -H 'Content-Type: application/json' \ -d '{"title": "My First Post", "content": "This is the content of my first post."}'
Feel free to open issues or submit pull requests with improvements.
This project is licensed under the MIT License - see the LICENSE file for details.
This is a simple REST API for managing user expenses across different bank accounts. The system is built using Flask, MongoDB, and PyMongo. It allows users to add and track expenses associated with their bank accounts.
- User Management: Manage users who have multiple bank accounts.
- Expense Tracking: Track expenses per account for each user.
- NoSQL Database: MongoDB is used to store users, accounts, and expenses.
- Python 3.x
- MongoDB
- pip (Python package installer)
-
Clone the repository:
git clone https://github.com/yourusername/expense-management-system.git cd expense-management-system
-
Install dependencies:
pip install -r requirements.txt
-
Set up MongoDB:
Make sure MongoDB is running locally on
mongodb://localhost:27017/expense_manager
. -
Configuration:
You can configure the MongoDB connection string in
config.py
.MONGO_URI = 'mongodb://localhost:27017/expense_manager'
-
Run the application:
python app.py
The API will be available at
http://127.0.0.1:5000/
.
- Endpoint:
POST /api/users/{user_id}/accounts/{account_id}/expenses
- Description: Adds a new expense to a specified account for a user.
{
"description": "Groceries",
"amount": 50
}
{
"message": "Expense added successfully",
"expense": {
"_id": "64edbd1f3c2a7c0c7d7d",
"description": "Groceries",
"amount": 50,
"date": "2024-09-03"
}
}
- Endpoint:
GET /api/users/{user_id}/accounts/{account_id}/expenses
- Description: Retrieves all expenses for a specified account of a user.
{
"account": "64edbd1f3c2a7c0c7d7d",
"expenses": [
{
"_id": "64edbd1f3c2a7c0c7d7e",
"description": "Groceries",
"amount": 50,
"date": "2024-09-03"
}
]
}
You can use Postman or curl to test the API endpoints.
-
Add an Expense:
curl -X POST \ http://127.0.0.1:5000/api/users/{user_id}/accounts/{account_id}/expenses \ -H 'Content-Type: application/json' \ -d '{"description": "Dinner", "amount": 30}'
-
Get All Expenses:
curl -X GET \ http://127.0.0.1:5000/api/users/{user_id}/accounts/{account_id}/expenses
For detailed API documentation, refer to the following Postman links:
Feel free to open issues or submit pull requests with improvements.
This project is licensed under the MIT License - see the LICENSE file for details.