Skip to content

Sumonta056/dev-stories-backend

Repository files navigation

Dev Stories Backend 🚀

https://dev-stories-backend.onrender.com https://dev-stories-backend.onrender.com License Version

Dev Stories Backend is a full-featured blogging platform API with authentication, story management, comments, likes, AI-powered features, search, pagination, and robust validation checks. Built with Node.js, PostgreSQL, Knex.js, and Docker.

📑 Table of Contents

✨ Features

  • 🔹 User Management (CRUD)
  • 🔐 JWT-based Authentication
  • 📖 Story Management (CRUD)
  • 🛡️ Middleware for Role-based Authorization
  • 💬 Comments & Likes System
  • 🤖 AI-powered Story Summarization & Q&A
  • 🔎 Search by Username, Story Title, or Description
  • 📌 Pagination, Sorting, Filtering for Users & Stories
  • 🛠️ Global Error Handling & Validation
  • 🧑‍💻 Api Documentation using Swagger.js
  • 🧪 Unit & Integration Testing
  • 🐳 Dockerized Deployment

⚙️ Installation

Prerequisites

Ensure you have the following installed:

Steps

  1. Clone the repository:
    git clone https://github.com/Sumonta056/dev-stories-backend.git
    cd dev-stories-backend
  2. Install dependencies:
    npm install
  3. Configure environment variables:
    cp .env.example .env
    # Update .env file with your configurations

Troubleshooting

  • If you encounter issues during installation, ensure that your Node.js and PostgreSQL versions are compatible with the project requirements.

🔧 Configuration

Update the .env file with the following variables:

PORT=3000
DB_HOST=localhost
DB_USER=your_postgres_user
DB_PASSWORD=your_postgres_password
DB_NAME=your_database_name
DB_PORT=5432
JWT_SECRET=your_jwt_secret
BCRYPT_SALT_ROUNDS=10
GOOGLE_GEMINI_API_KEY=your_gemini_api_key

📂 Database Migrations

Run database migrations using Knex:

npx knex migrate:latest

🚀 Running the Server

Start the server:

npm start

🧑‍💻 API Documentation

Visit this link

http://localhost:3000/api-docs/#/

🐳 Docker Support

To run the backend using Docker:

docker compose up --build

📡 API Endpoints

User Management

  • Register User: POST /api/users
  • Get All Users: GET /api/users
  • Get User By Id: GET /api/users/:id
  • Get User By Username: GET /api/users/username/{username}
  • Update User: PUT /api/users/:id
  • Delete User: DELETE /api/users/:id
  • User Profile: GET /api/users/profile/{username}

Authentication

  • Login: POST /api/auth

Story Management

  • Create Story: POST /api/stories
  • Get All Stories: GET /api/stories
  • Get Story by ID: GET /api/stories/:id
  • Update Story: PUT /api/stories/:id
  • Delete Story: DELETE /api/stories/:id
  • All Stories By Username: GET /api/stories/user/{username}

Comments & Likes

  • Add Comment: POST /api/stories/:storyId/comments
  • Get Comments: GET /api/stories/:storyId/comments
  • Update Comment: PUT /api/stories/{storyid}/comments/{commentId}
  • Delete Comment: DELETE /api/stories/{storyid}/comments/{commentId}
  • Like a Story: POST /api/stories/:storyId/likes
  • All Commented Stories by Username: GET /api/stories/commented-stories/all{username}
  • All Likes Stories by Username: GET /api/stories/liked-stories{username}

AI Features

  • Summarize Story: POST /api/stories/ai/summary
  • Ask AI Question: POST /api/stories/ai/ask

Search & Pagination

  • Search Stories: GET /api/stories/search?id=keyword&page=1&limit=10
  • Paginated Users: GET /api/users?page=1&limit=5

Filter & Sorting

  • Sorting Stories: GET /api/stories?page=1&sortBy=created_at&sortOrder=desc
  • Filter: GET /api/stories?page=1&limit=3&filter=mostliked

Newsletter Subscription

  • Subscribe to Newsletter: POST /api/newsletter/subscribe
  • Send Mail to all Subscriber: POST /api/newsletter/send
  • Get List of all subscriber: GET /api/newsletter/subscribers
  • Remove a subscriber: DELETE /api/newsletter/subscribers/{subscriberId}

🛡️ Middleware

The following middleware functions are used to enhance security and enforce business logic:

  • 🔑 Authentication Middleware (authenticateToken.ts): Ensures requests are authenticated with a valid JWT token.
  • 🛂 Authorization Middleware (authorizeRoles.ts): Restricts access based on user roles (Admin, Author, User).
  • ⚠️ Error Handling Middleware (errorHandler.ts): Handles global error responses in a structured manner.
  • 📦 Content Negotiation Middleware: Supports JSON, XML, and YAML responses.

✅ Validation Rules

To maintain data integrity and security, the following validation rules are enforced:

  • User Registration & Update:
    • username, name, and email must be strings.
    • email must be a valid email format.
    • password must be at least 6 characters long.
  • Story Creation & Update:
    • title and description are required fields.
    • title must be unique.
  • Pagination & Search:
    • page and limit must be positive integers.
    • key (search parameter) must be a string.
  • Comment & Like Actions:
    • Users must be logged in to comment or like a story.
    • Only the comment author, story author, or admin can update/delete comments.

🧪 Testing

Run unit and integration tests:

npm test

📁 File Structure

📂 dev-stories-backend
┣ 📂 src
┃ ┣ 📂 controllers
┃ ┣ 📂 services
┃ ┣ 📂 routes
┃ ┣ 📂 middlewares
┃ ┣ 📂 models
┃ ┣ 📂 config
┃ ┣ 📂 utils
┃ ┗ 📜 index.ts
┣ 📜 Dockerfile
┣ 📜 docker-compose.yml
┣ 📜 .env.example
┣ 📜 package.json
┣ 📜 README.md
┗ 📜 knexfile.js

🤝 Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change. Please ensure your code adheres to the following standards:

  • Follow the existing code style.
  • Write tests for new features.
  • Update documentation as necessary.

📜 License

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

Releases

No releases published

Packages

No packages published