Blog-Posts-Backend

Blog API with Flask & SQLAlchemy

This project is a simple backend API for managing blog posts built using Python’s Flask framework and SQLAlchemy. It provides CRUD (Create, Read, Update, Delete) operations for blog posts and can serve as a starting point for more advanced blog or content management systems.


Table of Contents

  1. Overview
  2. Features
  3. Architecture
  4. Tech Stack
  5. Installation & Setup
  6. Usage
  7. Testing
  8. Deployment
  9. Future Enhancements
  10. Troubleshooting & FAQ
  11. Contributing
  12. License

Overview

The Blog API is a RESTful service that allows clients to manage blog posts. It supports the following operations:

The API is built using Flask for the web framework and SQLAlchemy for ORM (Object Relational Mapping) with a SQLite database for data persistence.


Features


Architecture

System Overview

Component Breakdown

  1. API Endpoints:
    The application defines endpoints for:
    • Listing all posts
    • Getting a single post
    • Creating a new post
    • Updating a post
    • Deleting a post
  2. Model Definition:
    The Post model defines the structure of a blog post, including fields for title, content, and timestamps for creation and updates.

  3. Data Serialization:
    Each post is converted to a dictionary format via the to_dict() method before being sent as JSON in the response.

Tech Stack


Installation & Setup

Prerequisites

Steps

  1. Clone the Repository

    git clone https://github.com/your-username/blog-api.git
    cd blog-api
    
  2. Create a Virtual Environment (Optional but Recommended)

    python3 -m venv venv
    source venv/bin/activate    # On Windows: venv\Scripts\activate
    
  3. Install Dependencies

    pip install flask flask_sqlalchemy
    
  4. Run the Application

    The application will automatically create the SQLite database and required tables on the first run.

    python app.py
    

    The API will now be accessible at http://localhost:5000.


Usage

API Endpoints

1. Get All Posts

2. Get a Single Post

3. Create a New Post

4. Update an Existing Post

5. Delete a Post


Testing

Unit Testing

Example with pytest

  1. Install pytest:

    pip install pytest
    
  2. Create a test file (e.g., test_app.py) and write tests:

    import json
    from app import app, db, Post
    
    def test_get_posts():
        with app.test_client() as client:
            response = client.get('/posts')
            assert response.status_code == 200
    
    def test_create_post():
        with app.test_client() as client:
            data = {
                "title": "Test Post",
                "content": "This is a test."
            }
            response = client.post('/posts', json=data)
            assert response.status_code == 201
            json_data = json.loads(response.data)
            assert json_data['title'] == "Test Post"
    
  3. Run the tests:

    pytest
    

Deployment

Docker

Create a Dockerfile in the project root:

FROM python:3.9-slim

WORKDIR /app

# Copy requirements file and install dependencies
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Copy the rest of the application code
COPY . .

EXPOSE 5000

CMD ["python", "app.py"]

Build and run the Docker container:

docker build -t blog-api .
docker run -d -p 5000:5000 blog-api

Cloud Deployment


Future Enhancements


Troubleshooting & FAQ


Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/my-feature).
  3. Commit your changes.
  4. Push the branch (git push origin feature/my-feature).
  5. Open a pull request with a detailed description of your changes.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Enjoy using the Blog API, and feel free to extend it further to suit your needs!